Web.py is a lightweight Python web framework.
I'll use the web.py framework to create a simple example of an HTML page.
1. Create a Web site root directory exweb2
mkdir EXWEB2
2. Create a virtual environment in the root directory of the Web site
CD EXWEB2
Virtualenv uniqueenv
3. Installing Flask
Uniqueenv/bin/pip Install Lpthw.web
4.web.py use, the app.py code is as follows
#-*-coding:utf-8-*-ImportWeb URLs=( '/','Index') App=web.application (Urls,globals ())#Base as Base template pageRender = Web.template.render ('templates/', base="Base")classIndex (object):#Get method commits, if the URL parameter is not empty, jump directly to the display interface, otherwise jump to the input information interface defGET (self): form= Web.input (name="", greet="") ifform.greet!="" andform.name!="": Greeting="%s,%s"%(Form.greet,form.name)returnRender.index (greeting=greeting)returnRender.hello_form ()#Post method to submit the form, defPOST (self): form= Web.input (name="Nobody", greet="Hello") Greeting="%s,%s"%(Form.greet,form.name)returnRender.index (greeting=greeting)if __name__=="__main__": App.run ()
5. Then create the template page, and the template is placed under the Templates folder.
mkdir templates
First create a base template page base.html
$def with (content) $:content </body>
Enter information page hello_form.html
"/"Method="POST">A Greeting:<input type="text"Name="greet"><br/>Your Name:<input type="text"Name="name"><br/><input type="Submit"></form>
Display Information page index.html
$def with (greeting) $if greeting: <em style="color: Green Font-size:2em; "> $greeting </em>. $Else: <em>hello</em>, world!
7. The project directory structure is as follows:
Exweb2\
Uniqueenv\
app.py
Templates\
Base.html
Hello_form.html
Index.html
8. Run: Uniqueenv/bin/python app.py
Note that the Python interpreter in the virtual directory is used when running
9. Results:
(1) Get mode
(2) Post mode
python--web.py use