Python Forum

Full Version: Sending file html ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi.

How to make wsgi send an index.html to the browser client ?
Do you really want to use WSGI directly?
The reason i ask this is that take lot more work and low level understanding on how to use.
Usually only people who want to make yet another Python web-frame,dig into WSGI specifications.
You can have look here here an overview how is done The Python WSGI Interface

The more normal way is to use Flask,which is a layer above WSGi.
If send html directly to client.
from flask import Flask

app = Flask(__name__)
@app.route('/')
def index():
   return '<html><body><h1>Hello World</h1></body></html>'

if __name__ == '__main__':
   app.run(debug = True)
However this is not the normal way of doing it,Flask has a render_template() function.
This take advantage of Jinja2 template engine which is a part of Flask,
so Instead of returning hardcode HTML from the function,a HTML file can be rendered(Jinja2) by the render_template() function an send to client.
from flask import Flask

app = Flask(__name__)
@app.route('/')
def index():
   return render_template('hello.html')

if __name__ == '__main__':
   app.run(debug = True)
(Sep-06-2019, 08:50 AM)snippsat Wrote: [ -> ]Do you really want to use WSGI directly? The reason i ask this is that take lot more work and low level understanding on how to use. Usually only people who want to make yet another Python web-frame,dig into WSGI specifications. You can have look here here an overview how is done The Python WSGI Interface The more normal way is to use Flask,which is a layer above WSGi. If send html directly to client.
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return '<html><body><h1>Hello World</h1></body></html>' if __name__ == '__main__': app.run(debug = True)
However this is not the normal way of doing it,Flask has a render_template() function. This take advantage of Jinja2 template engine which is a part of Flask, so Instead of returning hardcode HTML from the function,a HTML file can be rendered(Jinja2) by the render_template() function an send to client.
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return render_template('hello.html') if __name__ == '__main__': app.run(debug = True)

I need to do without using third party framework
(Sep-06-2019, 01:31 PM)JohnnyCoffee Wrote: [ -> ]I need to do without using third party framework
Why?

Something here you can look at. here, here.
WSGI middleware is the more correct way,this stuff is not easy do right,that's why we have framework.
Look at how Werkzeug dos it,a even thinner layer above WSGI,Flask has a lot werkzeug stuff in bottom.
Serve Shared Static Files, Github.