1, Required module declaration
1. Flask module
Flask it's a lightweight Web application framework, using Python writing. it provides some tools and libraries for development Web applications have become easier and faster. here is a simple example Flask application example :
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__== '__main__':
app.run()
"""
this application defines a named Flask instance and use a decorator to bind the function to the root URL up. when users access the root URL when, the function will be called and return"Hello, World!"string. app@app.route('/')hello_world()hello_world()
Flask it also provides many other features, such as template engine, form processing, database integration, and so on. it can be achieved through Flask learn more from official documents.
"""
2, Complete code
1. Running functions in Python
from flask import Flask,render_template #render: make
from random import randint
hero=['the daughter of darkness','berserker','the giant statue of justice','han xin','li bai','number 10','shi hao','chen ping'an','yang jian','chen kaige','wang xuan']
app = Flask(__name__) #create an external application
@app.route('/') #using decorators to bind functions to roots URL note that passing functions to web pages does not need to be in the root directory'/'add something below
def index():
return render_template('index.html',hero = hero) #previous hero indicating in index.html name in
@app.route('/choujiang') #the functions that the webpage needs to execute need to be declared in the root directory, that is'/choujiang'
def choujiang():
num = randint(0,len(hero)-1) #(0,9)
return render_template('index.html',hero=hero,h=hero[num])
app.run(debug=True) #debug=True convenient for subsequent development, eliminating the need for repeated application restarts when rewriting code in the future
"""
http://127.0.0.1:5000 http://127.0.0.1: accessing our own laptop, : 5000 indicates positioning to this point Flask come up with this service
"""
2. HTML webpage
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ hero }}<br/>
<a href="/choujiang"> random extraction </a><br/>
you have drawn{ { h }}
</body>
</html>
3. Overall Tour

3, Running results

