Web Framework Introduction
Web Framework Introduction
Learning objectives
if __name__ == '__main__':
app.run()
How to run
Double click on the file hello.py to start the server
if __name__ == '__main__’:
app.run()
Formatting Web pages
We can now format our web pages using HTML:
from flask import Flask
app = Flask(__name__)
@app.route('/hello')
def hello():
return """<html><body><h1>Hello World
</h1></body></html>"""
@app.route('/bye')
def bye():
return """<html><body><h1>Goodbye
</h1</body></html>"""
if __name__ == '__main__':
app.run()
Creating html pages
For the example we used very simple html code, but this could get
very messy if we have more html code.
What we can do is create separate html files that makes organisation
of different parts of our application much easier
In the hello folder create a new sub folders called templates
It must be called templates because Flask looks in this folder to
find the HTML files.
Creating html pages
Create two new html files hello.html and bye.html
hello.html bye.html
<html> <html>
<body> <body>
<h1> Hello World </h1> <h1> Goodbye </h1>
</body> </body>
</html> </html>
Render Template
Now replace the return statements with
from flask import Flask, render_template render_template
app = Flask(__name__)
Restart the server and run the code.
@app.route('/hello')
The output should not change.
def hello():
return render_template("hello.html") Our HTML has been separated from our
Python code
@app.route('/bye’)
def bye():
return render_template("bye.html")
if __name__ == '__main__’:
app.run()
CSS Frameworks
At the moment our web pages look quite bland, but we can jazz them
up by using CSS frameworks
Bootstrap https://getbootstrap.com
w3.css https://www.w3schools.com/w3css/
Add another HTML page with a different message. Create the HTML
file and modify the Python Flask file.
Use different w3.css (or bootstrap) classes to change the font, text
and background colour (see https://www.w3schools.com/w3css/ )
Creating interactive web pages
Our pages are static, but now we are going to create interactive web pages
Create a new page HTML with a form called name.html
Add the following code to name.html
<html>
<head>
<link rel="stylesheet" href="{{ url_for('static',
filename='w3.css') }}">
</head>
<body>
<h1 class="w3-sans-serif w3-text-teal">
Please enter your name:
</h1>
<form action="" method="post">
<input type="text" name="name" value="{{ request.form.name }}">
<input type="submit" value="Sign in">
</form>
</body>
</html>
Modify hello.html to include a variable {{ name }}
<html>
<head>
<link rel="stylesheet" href="{{ url_for('static',
filename='w3.css') }}">
</head>
<body>
<h1 class="w3-sans-serif w3-text-teal">
Hello {{ name }}
</h1>
</body>
</html>
Complete code listing for hello.py
from flask import Flask, render_template, request
Add the highlighted code app = Flask(__name__)
to hello.py
@app.route('/', methods=['GET', 'POST'])
When the form button is def name():
pressed the whatever is if request.method == 'POST':
written in the box is return render_template("hello.html",name=request.form['name’] )
“posted” and is the name return render_template("name.html")
variable for hello.html
@app.route('/hello’)
def hello():
return render_template("hello.html")
@app.route('/bye’)
def bye():
return render_template("goodbye.html")
if __name__ == '__main__’:
app.run(debug=True)
Running the code