Flask
| Introduction | |
| Articles about Flask | |
| Installation | |
| An example of a web application that shows «Hello World!»: | |
| The simplest web server | |
| Articles about Python |
Flask
Introduction
Flask is a framework for creating web applications in the Python programming language,
using the Werkzeug toolkit as well as the Jinja2 template.
Refers to the category of so-called microframeworks - minimalistic frameworks
Web applications that knowingly provide only the most basic features.
Installation supported by PyPI package manager, version 1.0 compatible
Python 2.7, Python 3.3 and higher.
The creator and main author - Austrian programmer Armin Ronacher, began work
on the project in 2010.
| Launching Flask on hosting | |
| First project on Flask | |
| Running Flask on a Linux server | |
| Flask FAQ | |
| Web Forms | |
| Errors |
Installation Flask
Installation Flask depends on your surroundings. If you work in a PyCharm-type IDE, a virtual environment
Your project has already been completed (see para.
here
).
If you are in a «clean» console, then be sure to create a virtual environment for the project on
Flask. You can read about how to do this in a series of articles.
«Virtual environment in Python»
Python, Pip, and virtualenv are already installed.
Create a new virtual environment, activate it and execute it.
python -m pip install flask
Hello World
By default, Flask responds to GET.
Create a server.py file with the following code
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
Don’t call your file flask.py this will lead to an error.
If you see a manual without specifying the method, keep in mind that either the answer will only be on GET, or the author has changed the default settings.
You can start the server.py file with command.
python server.py
But the traditional use of flask run, which I will talk about in the lesson. «The first project on Flask» .
If there is a Flask in your surroundings, you will see
* Serving Flask app "server" (lazy loading) * 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)
You can open your browser and enter http://127.0.0.1:5000/
It should be on the screen.
Hello Flask!
And it should be in the console.
127.0.0.1 - - [15/Jun/2020 16:19:06] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [15/Jun/2020 16:19:07] "GET /favicon.ico HTTP/1.1" 404 -
404 - since I didn't add a favicon.ico file
Simple web server on Flask
Create such an elementary server that will respond to requests
It is useful when you need to quickly test the application, which is previously
Send some important information that requests the status of the server.
You can answer 200 and listen to what the app sends.
Similar functionality can be implemented in a huge number of ways,
for example
Soap UI
from flask import request
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello world'
@app.route('/bestws')
def cakes():
return 'HeiHei.ru!'
@app.route('/heiheiru/holidays', methods = ['GET'])
def heiheiru_holidays():
timestamp = request.args.get('timestamp')
print(f"Someone made a GET request to heihei.ru and passed the time {timestamp}")
return '200'
@app.route('/heiheiru/fests', methods = ['GET','PUT', 'POST'])
def heiheiru_fests():
epc = request.args.get('epc')
print(epc)
antenna = request.args.get('antenna')
print(antenna)
timestamp = request.args.get('timestamp')
print(timestamp)
userMemory = request.args.get('userMemory')
print(userMemory)
return '200'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
Article author: Andrei Olegovich