GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python Flask - web applications in Python with Flask Python Flasklast modified January 29, 2024 Python Flask tutorial shows how to use Python Flask module to create Python web applications. FlaskFlask is Python micro web framework. It includes only a core library. Most of the functionality is available as extensions including validation, form handling, object-relational mappers, or authentication. Flask is based on Werkzeug WSGI toolkit and Jinja2 template engine. The Web Server Gateway Interface (WSGI) is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python programming language. Flask Jinja2Jinja2 is a Flask templating engine. A template engine or template processor is a library designed to combine templates with a data model to produce documents. Template engines are often used to generate large amounts of emails, in source code preprocessing, or producing dynamic HTML pages. Learn more about Jinja2 with the Jinja tutorial. URLA Uniform Resource Locator (URL), is a reference to a web resource that specifies its location on a computer network and a mechanism for retrieving it. A web resource is any data that can be obtained via web, such as HTML documents, PDF files, PNG images, JSON data, or plain text. A generic URL has the following form: scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]
The parts between the Flask installation$ pip3 install -U Flask
We use the Setting up virtual environmentMore complex applications use virtual environments during development. $ python3 -m venv venv . venv/bin/activate These commands create a new virtual environment on Linux. > py -3 -m venv venv > venv\Scripts\activate These commands create a new virtual environment on Windows. Running Flask applicationThe recommended way to run Flask applications is with the help of environment variables. $ export FLASK_APP=hello.py $ flask run
We set the $ export FLASK_ENV=development
To run Flask application in development mode, we also set the
Flask simple exampleIn the following example, we create a simple Flask application. $ mkdir hello $ cd hello We create a directory for the application. hello.py
#!/usr/bin/python
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello there!'
Inside a directory, we create a Python file from flask import Flask
We import the @app.route('/')
def hello():
return 'Hello there!'
With the $ flask run * Serving Flask app "hello.py" * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
We set the $ curl localhost:5000 Hello there!
We create a request with the Flask query parameters
Query string is a part of the URL which is used to add some criteria to the
request for the resource. It is often a sequence of key/value pairs.
It follows the path and starts with the app.py
#!/usr/bin/python
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def index():
return 'Home page'
@app.route('/greet', methods=['GET'])
def greet():
name = request.args.get('name', 'Guest')
msg = f'Hello {name}'
return msg, 200, {'Content-Type': 'text/plain; charset=utf-8'}
The application creates and sends a message to the client. It uses
the value from the from flask import Flask, request
We import the @app.route('/greet', methods=['GET'])
def greet():
...
The name = request.args.get('name', 'Guest')
We get the query parameter from the msg = f'Hello {name}'
We include the value of the return msg, 200, {'Content-Type': 'text/plain; charset=utf-8'}
We return the message, status code, and the response content type to the client. $ export FLASK_APP=app.py $ flask run We run the application. $ curl -i localhost:5000/greet?name=Lucia HTTP/1.0 200 OK Content-Type: text/plain; charset=utf-8 Content-Length: 11 Server: Werkzeug/0.16.0 Python/3.8.0 Date: Thu, 16 Jan 2020 15:50:58 GMT Hello Lucia
We create a GET request to the application. With the $ curl localhost:5000/greet Hello Guest When no query parameter is set, the default value is used. Flask path parameters
Values can be send to the web application via query parameters or path
parameters. The path parameter is specified between angle brackets:
app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Home page'
@app.route("/greet/<name>/")
def greet(name):
msg = f'Hello {name}'
return msg, 200, {'Content-Type': 'text/plain; charset=utf-8'}
The application returns a greeting to the user, whose name is specified as a path parameter. @app.route("/greet/<name>/")
def greet(name):
The path parameter is defined here: $ curl localhost:5000/greet/Robert/ Hello Robert Flask make_response
With the app.py
#!/usr/bin/python
from flask import Flask, make_response
app = Flask(__name__)
@app.route('/')
def hello():
return 'Home page'
@app.route('/users/<name>', methods=['POST'])
def create_user(name):
msg = f'user {name} created'
return make_response(msg, 201)
@app.route('/users/<name>', methods=['GET'])
def get_user(name):
msg = f'Hello {name}'
return make_response(msg, 200)
The application consists of one file: @app.route('/')
def hello():
return 'Home page'
For the home page, we return a simple text message. @app.route('/users/<name>', methods=['POST'])
def create_user(name):
msg = f'user {name} created'
return make_response(msg, 201)
The @app.route('/users/<name>', methods=['GET'])
def get_user(name):
msg = f'Hello {name}'
return make_response(msg, 200)
The $ export FLASK_APP=app.py $ flask run We run the application. $ curl -i localhost:5000/users/Peter/ HTTP/1.0 200 OK Content-Type: text/html; charset=utf-8 Content-Length: 11 Server: Werkzeug/0.16.0 Python/3.8.0 Date: Thu, 16 Jan 2020 15:31:37 GMT Hello Peter We create a GET request to the application. $ curl -X POST localhost:5000/users/Peter/ user Peter created A POST request is created. Flask send_file
The app.py
#!/usr/bin/python
from flask import Flask, send_file
app = Flask(__name__)
@app.route('/image')
def get_image():
filename = 'sid.png'
return send_file(filename, mimetype='image/png')
The example sends an image to the client. return send_file(filename, mimetype='image/png') We specify the filename and the content type. Flask JSONJSON is a lightweight data-interchange format. It is easy for humans to read and machines to parse and generate. Web applications often consume and produce JSON data.
When returning a Python dictionary to the client, Flask automatically transforms
it to JSON. Other objects can be transformed to JSON with the app.py
from flask import Flask, jsonify, render_template
import random
app = Flask(__name__)
movies = {1: 'Toy story', 2: 'The Raid', 3: 'Hero',
4: 'Ip Man', 5: 'Kung Fu Panda'}
@app.route('/movies')
def get_movies():
return movies
@app.route('/rmovie')
def random_movie():
movie = random.choice(list(movies.items()))
return jsonify(movie)
In the example, we have two functions. One returns a dictionary, the other one a random pair from the dictionary. @app.route('/movies')
def get_movies():
return movies
Flask transforms the @app.route('/rmovie')
def random_movie():
movie = random.choice(list(movies.items()))
return jsonify(movie)
This function returns a random movie from the dictionary; it is a Python tuple.
The tuple is then transformed into JSON with the $ curl localhost:5000/movies
{"1":"Toy story","2":"The Raid","3":"Hero","4":"Ip Man","5":"Kung Fu Panda"}
Here we get all movies as JSON data. $ curl localhost:5000/rmovie [2,"The Raid"] Here we get a random movie. Flask render_template
The app.py
templates
index.html
These are the contents of the project directory. app.py
#!/usr/bin/python
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/greet/<name>/')
def greet(name):
msg = f'Hello {name}'
return render_template('index.html', name=name)
In the example, we read a value from a path parameter and send it to the
return render_template('index.html', name=name)
The first parameter of the templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Greeting</title>
</head>
<body>
<p>
Hello {{ name }}
</p>
</body>
</html>
This is the <p>
Hello {{ name }}
</p>
With the $ curl localhost:5000/greet/Peter/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Greeting</title>
</head>
<body>
<p>
Hello Peter
</p>
</body>
</html>
Flask serve static files
Static files are files that do not change. They include CSS files, JavaScript
files and images; also HTML files which do not contain template directives.
Flask has a default app.py
static
about.html
sid.jpg
templates
index.html
This is the project structure. app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/about')
def about():
return app.send_static_file('about.html')
We have two routes in the application. The home page renders a template, which
refers to an image. The about page returns a static HTML file with the
static/about.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>About</title>
</head>
<body>
<p>
About page
</p>
</body>
</html>
The templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home page</title>
</head>
<body>
<img src="/static/sid.jpg" alt="Sid">
<img src="{{ url_for('static', filename='sid.jpg') }}" alt="Sid">
</body>
</html>
In the <img src="/static/sid.jpg" alt="Sid">
<img src="{{ url_for('static', filename='sid.jpg') }}" alt="Sid">
We include an JPEG image into the file. The usage of the Flask custom 404 pageThe HTTP 404, 404 Not Found, 404, Page Not Found error message is a Hypertext Transfer Protocol (HTTP) standard response code used in web communication. It indicates that the browser was able to communicate with a given server, but the server could not find what was requested.
Flask has a built-in app.py
#!/usr/bin/python
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def hello():
return 'Home page'
@app.route('/about')
def about():
return 'About page'
@app.errorhandler(404)
def not_found_error(error):
return render_template('404.html'), 404
In the example, we register an error handler function for the 404 not found error.
It renders the templates/404.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>404 error</title>
</head>
<body>
<p>
404 - page not found
</p>
</body>
</html>
This is the error page that is shown to the user. Flask SQLite exampleIn the following example, we send data from an SQLite database. We use SQLAlchemy. app.py data cities.sql test.db This is the project structure. cities.sql
BEGIN TRANSACTION;
DROP TABLE IF EXISTS cities;
CREATE TABLE cities(id INTEGER PRIMARY KEY, name TEXT, population INTEGER);
INSERT INTO cities(name, population) VALUES('Bratislava', 432000);
INSERT INTO cities(name, population) VALUES('Budapest', 1759000);
INSERT INTO cities(name, population) VALUES('Prague', 1280000);
INSERT INTO cities(name, population) VALUES('Warsaw', 1748000);
INSERT INTO cities(name, population) VALUES('Los Angeles', 3971000);
INSERT INTO cities(name, population) VALUES('New York', 8550000);
INSERT INTO cities(name, population) VALUES('Edinburgh', 464000);
INSERT INTO cities(name, population) VALUES('Berlin', 3671000);
COMMIT;
We use this data. $ sqlite3 test.db SQLite version 3.27.2 2019-02-25 16:06:06 Enter ".help" for usage hints. sqlite> .read cities.sql sqlite> select * from cities; 1|Bratislava|432000 2|Budapest|1759000 3|Prague|1280000 4|Warsaw|1748000 5|Los Angeles|3971000 6|New York|8550000 7|Edinburgh|464000 8|Berlin|3671000
We load the data into the app.py
#!/usr/bin/python
from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data/test.db'
db = SQLAlchemy(app)
class City(db.Model):
__tablename__ = 'cities'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
population = db.Column(db.Integer)
def serialize(self):
return {
'id': self.id,
'name': self.name,
'population': self.population,
}
@app.route('/cities')
def all():
cities = City.query.all()
return jsonify(cities=[city.serialize() for city in cities])
The application sends all rows from the from flask_sqlalchemy import SQLAlchemy
To work with data, we use the app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
We do not use the app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data/test.db'
With the db = SQLAlchemy(app)
An class City(db.Model):
__tablename__ = 'cities'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
population = db.Column(db.Integer)
def serialize(self):
return {
'id': self.id,
'name': self.name,
'population': self.population,
}
This is the @app.route('/cities')
def all():
cities = City.query.all()
return jsonify(cities=[city.serialize() for city in cities])
For the $ export FLASK_APP=app.py
$ curl localhost:5000/cities
{"cities":[{"id":1,"name":"Bratislava","population":432000},
{"id":2,"name":"Budapest","population":1759000},
{"id":3,"name":"Prague","population":1280000},
{"id":4,"name":"Warsaw","population":1748000},
{"id":5,"name":"Los Angeles","population":3971000},
{"id":6,"name":"New York","population":8550000},
{"id":7,"name":"Edinburgh","population":464000},
{"id":8,"name":"Berlin","population":3671000}]}
SourceIn this article we have introduced the Python Flask module, which is used to develop web applications. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |