Difference between revisions of "Flask"
Jump to navigation
Jump to search
Line 98: | Line 98: | ||
=Generate web pages= | =Generate web pages= | ||
− | https://www.idkrtm.com/creating-a-webpage-using-python-and-flask/ | + | https://flask.palletsprojects.com/en/1.1.x/patterns/templateinheritance/#template-inheritance |
+ | |||
+ | |||
+ | [https://www.idkrtm.com/creating-a-webpage-using-python-and-flask/ Source for this basic app] | ||
+ | |||
+ | Put a index.html in <app-base>/templates/ | ||
+ | |||
+ | <syntaxhighlight lang=python> | ||
+ | #!/usr/bin/env python3 | ||
+ | |||
+ | from flask import Flask, render_template | ||
− | + | print(__name__) | |
+ | app = Flask(__name__) | ||
+ | |||
+ | @app.route('/html') | ||
+ | def static_page(): | ||
+ | return render_template('index.html') | ||
+ | |||
+ | if __name__ == '__main__': | ||
+ | app.run(host='0.0.0.0',port=5000,debug=True) | ||
+ | |||
+ | </syntaxhighlight> | ||
=Microservices and/or more applications on 1 web-server= | =Microservices and/or more applications on 1 web-server= |
Revision as of 18:07, 22 February 2020
Basics
Check this[1]
Hello world app:
#!/usr/bin/env python3
from flask import Flask
print(__name__)
app = Flask(__name__)
@app.route('/')
def welcome():
return 'Welcome'
if __name__ == '__main__': # When executed on the commandline (build in webserver)
app.run(host='0.0.0.0',port=5000) # Listen to localhost port 5000, you can also use flask run for options
#app.run(host='0.0.0.0',port=8080,Debug=True) # Run in debug mode, listen to all hosts on port 8080
Run the build-in server listening to all
export FLASK_APP=welcome_app.py
flask run --host=0.0.0.0
welcome_app * Serving Flask app "welcome_app" * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
Now you can browse to <serverIP>:5000
Integrate with nginx
Mayor part is from this site [2].
- Install uwgi-emperor and it's python plugin
Configuration files I used.
- Generic configuration file (/etc/uwsgi-emperor/emperor.ini):
[uwsgi] # try to autoload appropriate plugin if "unknown" option has been specified autoload = true # enable master process manager master = true # spawn 2 uWSGI emperor worker processes workers = 2 # automatically kill workers on master's death no-orphans = true # place timestamps into log log-date = true # user identifier of uWSGI processes (same as who runs the web-server) uid = www-data # group identifier of uWSGI processes (same as who runs the web-server) gid = www-data # vassals directory emperor = /etc/uwsgi-emperor/vassals plugins = python3
- Application configuration file (/etc/uwsgi-emperor/vassals/welcome.ini)
[uwsgi] #application's base folder base = /var/www/flask #python module to import app = welcome module = %(app) pythonpath = %(base) #socket file's location socket = /var/local/uwsgi/%n.sock #permissions for the socket file chmod-socket = 664 #the variable that holds a flask application inside the module imported at line #6 callable = app #location of log files logto = /var/log/uwsgi/%n.log plugins = python3
- Create a virtual webserver (port 80 is save enough if you have a secure front-end server)
server {
listen 80 ;
listen [::]:80 ;
root /var/www/flask;
server_name <fqdn>;
location / {
include uwsgi_params;
uwsgi_pass unix:/var/local/uwsgi/welcome.sock;
}
}
Generate web pages
https://flask.palletsprojects.com/en/1.1.x/patterns/templateinheritance/#template-inheritance
Put a index.html in <app-base>/templates/
#!/usr/bin/env python3
from flask import Flask, render_template
print(__name__)
app = Flask(__name__)
@app.route('/html')
def static_page():
return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000,debug=True)
Microservices and/or more applications on 1 web-server
Still al lot ToDo
This works, having 1 uwsgi app with 3 different routes:
#!/usr/bin/env python3
from flask import Flask
print(__name__)
app = Flask(__name__)
@app.route('/')
def root():
return 'The root of it'
@app.route('/hello')
def hello():
return 'Hello world'
@app.route('/welcome')
def welcome():
return 'Welcome'
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000,debug=True)
NGinx config; A simpler configuration with no upstream will probably work too.
upstream app {
server unix:///var/local/uwsgi/app.sock;
}
server {
listen 80 ;
listen [::]:80 ;
root /var/www/flask;
server_name <fqdn>;
location ~ /.* {
include uwsgi_params;
uwsgi_pass app;
}
}