Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending file html ?
#1
Hi.

How to make wsgi send an index.html to the browser client ?
Reply
#2
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)
Reply
#3
(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
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb Python Obstacles | Kung-Fu | Full File HTML Document Scrape and Store it in MariaDB BrandonKastning 5 2,819 Dec-29-2021, 02:26 AM
Last Post: BrandonKastning
  show csv file in flask template.html rr28rizal 8 34,524 Apr-12-2021, 09:24 AM
Last Post: adamabusamra
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,532 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  Open and read a tab delimited file from html using python cgi luffy 2 2,633 Aug-24-2020, 06:25 AM
Last Post: luffy
  Python3 + BeautifulSoup4 + lxml (HTML -> CSV) - How to loop to next HTML/new CSV Row BrandonKastning 0 2,329 Mar-22-2020, 06:10 AM
Last Post: BrandonKastning
  Reading a html file peterl 4 4,491 Aug-20-2018, 03:16 PM
Last Post: peterl
  Problem parsing website html file thefpgarace 2 3,167 May-01-2018, 11:09 AM
Last Post: Standard_user
  bs4 : output html content into a txt file smallabc 2 23,147 Jan-02-2018, 04:18 PM
Last Post: snippsat
  How to print particular text areas fron an HTML file (not site) Chris 10 6,971 Dec-11-2017, 09:20 AM
Last Post: j.crater
  read text file using python and display its output to html using django amit 0 18,262 Jul-23-2017, 06:14 AM
Last Post: amit

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020