Flask: Difference between revisions

From wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Installation=
Check this[https://flask.palletsprojects.com/en/1.1.x/quickstart/]
In the directory where you want to create your flask application:
<syntaxhighlight lang=bash>
apt install python3.11-venv  # make sure the venv module is there
python -m venv .venv  # Create a virtual environment
. .venv/bin/activate  # Activate the virtual environment
pip install Flask      # Install flask in the virtual environment
</syntaxhighlight>
=Basics=
=Basics=
Check this[https://flask.palletsprojects.com/en/1.1.x/quickstart/]


Hello world app:
Hello world app:
Line 16: Line 27:


if __name__ == '__main__':        # When executed on the commandline (build in webserver)
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=5000) # Listen to all on 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
     #app.run(host='0.0.0.0',port=8080,Debug=True)  # Run in debug mode, listen to all hosts on port 8080


</syntaxhighlight>
</syntaxhighlight>
Run the build-in server listening to all
Or run from the commandline so you can pass options to flask
<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
export FLASK_APP=welcome_app.py  
export FLASK_APP=welcome_app.py  
flask run --host=0.0.0.0
flask run --host=0.0.0.0
</syntaxhighlight>
</syntaxhighlight>
Output:
  welcome_app
  welcome_app
   * Serving Flask app "welcome_app"
   * Serving Flask app "welcome_app"
Line 37: Line 49:
Mayor part is from this site [https://vladikk.com/2013/09/12/serving-flask-with-nginx-on-ubuntu/].
Mayor part is from this site [https://vladikk.com/2013/09/12/serving-flask-with-nginx-on-ubuntu/].


* Install uwgi-emperor and it's python plugin
* Install [[uwsgi]] and uwsgi-plugin-python3.
Configuration files I used.
 
The inifile in
<syntaxhighlight lang="text">
[uwsgi]
base = /var/www/flask/demoapp
plugins = python3
 
# Script to run without .py
app = hello
# It's unclear if the script to run is defined by module or callable
module = %(app)
callable = app


:* Generic configuration file (/etc/uwsgi-emperor/emperor.ini):
home = %(base)/.venv
[uwsgi]
pythonpath = %(base)
# 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)
#socket file's location and permission
socket = /tmp/%n.sock      # %n is the name of the ini-file without .ini
chmod-socket    = 666


[uwsgi]
#location of log files
#application's base folder
logto = /var/log/uwsgi/%n.log  # Funny, logging is done to /var/log/uwsgi/app/%n.log
base = /var/www/flask
</syntaxhighlight>
#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)
* Create a virtual webserver (port 80 is save enough if you have a secure front-end server)
Line 83: Line 78:
<syntaxhighlight lang="nginx">
<syntaxhighlight lang="nginx">
server {
server {
listen 80 ;
    listen 80 ;
listen [::]:80 ;
    listen [::]:80;


root /var/www/flask;
    root /var/www/flask;
    server_name <fqdn>;


server_name <fqdn>;
    location @demoapp {
 
        include uwsgi_params;
location / {
        uwsgi_pass unix://tmp/demoapp_uwsgi.sock;
include uwsgi_params;
    }
        uwsgi_pass unix:/var/local/uwsgi/welcome.sock;
}
}
}
</syntaxhighlight>
</syntaxhighlight>


=Generate web pages=
That's it. Point your webbrowser to http://<your webserver fdqn></code> and it will show you what the app returns
https://flask.palletsprojects.com/en/1.1.x/patterns/templateinheritance/#template-inheritance


 
=Running applications=
[https://www.idkrtm.com/creating-a-webpage-using-python-and-flask/ Source for this basic app]
You can make any number of applications by adding more apps to [[uwsgi]] and a location in [[nginx]]. You can also run any number of programs by adding routes to an app or with just a few routes like this:
 
Put a index.html in <app-base>/templates/


<syntaxhighlight lang=python>
<syntaxhighlight lang=python>
Line 109: Line 100:


from flask import Flask, render_template
from flask import Flask, render_template
import os
import subprocess


print(__name__)
### Application whitelist
app = Flask(__name__)
whitelist = {
    'script as in URL' :'<scriptlocation>',
    'script as in URL' :'<another scriptlocation>',
    }
   
application = Flask(__name__)


@app.route('/html')
@application.route('/app1/<script>/<path:arguments>')
def static_page():
def func1(script,arguments):
  return render_template('index.html')
    if script in whitelist:
        arglist = arguments.split('/')
        result = subprocess.run([whitelist[script]]+arglist, stdout=subprocess.PIPE)
        output = result.stdout
    else:
        output = '{} does not exist in whitelist'.format(script)
    return output
   
@application.route('/app1/<script>')
def func2(script):
    if script in whitelist:
        result = subprocess.run([whitelist[script]], stdout=subprocess.PIPE)
        output = result.stdout
    else:
        output = '{} does not exist in whitelist'.format(script)
    return output


if __name__ == '__main__':
if __name__ == '__main__':
     app.run(host='0.0.0.0',port=5000,debug=True)
     application.run(host='0.0.0.0', port=8080,debug=True)
</syntaxhighlight>
 
The route is the request URI as passed by the webserver. In this case 'app1' is the location. The things in <> are converted to python variables. <path:arguments> will put everything, including the '/'-es into 1 variable (arguments). So you can have a variable number of arguments that all will be passed to the pythonscript called by subprocess.
 
The whitelist is there for security to avoid one can run any script is knows the location for.
 
=Generate web pages=


</syntaxhighlight>
Flask is by default using the [https://jinja.palletsprojects.com jinja] template engine. They have a nice tutorial for this too[https://flask.palletsprojects.com/en/2.0.x/tutorial/].


=Microservices and/or more applications on 1 web-server=
Very simple example is to put a index.html in <code><app-base>/templates/</code>
Still al lot ToDo


This works, having 1 uwsgi app with 3 different routes:
<syntaxhighlight lang=python>
<syntaxhighlight lang=python>
#!/usr/bin/env python3
#!/usr/bin/env python3


from flask import Flask
from flask import Flask, render_template
 
print(__name__)
print(__name__)
app = Flask(__name__)
app = Flask(__name__)


@app.route('/')
@app.route('/app1/html')
def root():
def static_page():
    return 'The root of it'
  return render_template('index.html')
 
@app.route('/hello')
def hello():
    return 'Hello world'
 
@app.route('/welcome')
def welcome():
    return 'Welcome'


if __name__ == '__main__':
if __name__ == '__main__':
     app.run(host='0.0.0.0',port=5000,debug=True)
     app.run(host='0.0.0.0',port=5000,debug=True)


</syntaxhighlight>
NGinx config; A simpler configuration with no upstream will probably work too.
<syntaxhighlight lang="nginx">
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;
}
}
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 20:10, 10 June 2025

Installation

Check this[1]

In the directory where you want to create your flask application:

apt install python3.11-venv  # make sure the venv module is there
python -m venv .venv   # Create a virtual environment
. .venv/bin/activate   # Activate the virtual environment
pip install Flask      # Install flask in the virtual environment

Basics

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 all on 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

Or run from the commandline so you can pass options to flask

export FLASK_APP=welcome_app.py 
flask run --host=0.0.0.0

Output:

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

https://code.tutsplus.com/tutorials/creating-a-web-app-from-scratch-using-python-flask-and-mysql--cms-22972

Integrate with nginx

Mayor part is from this site [2].

  • Install uwsgi and uwsgi-plugin-python3.

The inifile in

[uwsgi]
base = /var/www/flask/demoapp
plugins = python3

# Script to run without .py
app = hello
# It's unclear if the script to run is defined by module or callable
module = %(app)
callable = app

home = %(base)/.venv
pythonpath = %(base)

#socket file's location and permission
socket = /tmp/%n.sock      # %n is the name of the ini-file without .ini
chmod-socket    = 666

#location of log files
logto = /var/log/uwsgi/%n.log   # Funny, logging is done to /var/log/uwsgi/app/%n.log
  • 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 @demoapp {
        include uwsgi_params;
        uwsgi_pass unix://tmp/demoapp_uwsgi.sock;
    }
}

That's it. Point your webbrowser to http://<your webserver fdqn> and it will show you what the app returns

Running applications

You can make any number of applications by adding more apps to uwsgi and a location in nginx. You can also run any number of programs by adding routes to an app or with just a few routes like this:

#!/usr/bin/env python3

from flask import Flask, render_template
import os
import subprocess 

### Application whitelist
whitelist = {
    'script as in URL' :'<scriptlocation>',
    'script as in URL' :'<another scriptlocation>',
    }
    
application = Flask(__name__)

@application.route('/app1/<script>/<path:arguments>')
def func1(script,arguments):
    if script in whitelist:
        arglist = arguments.split('/')
        result = subprocess.run([whitelist[script]]+arglist, stdout=subprocess.PIPE)
        output = result.stdout
    else:
        output = '{} does not exist in whitelist'.format(script)
    return output
    
@application.route('/app1/<script>')
def func2(script):
    if script in whitelist:
        result = subprocess.run([whitelist[script]], stdout=subprocess.PIPE)
        output = result.stdout
    else:
        output = '{} does not exist in whitelist'.format(script)
    return output

if __name__ == '__main__':
    application.run(host='0.0.0.0', port=8080,debug=True)

The route is the request URI as passed by the webserver. In this case 'app1' is the location. The things in <> are converted to python variables. <path:arguments> will put everything, including the '/'-es into 1 variable (arguments). So you can have a variable number of arguments that all will be passed to the pythonscript called by subprocess.

The whitelist is there for security to avoid one can run any script is knows the location for.

Generate web pages

Flask is by default using the jinja template engine. They have a nice tutorial for this too[3].

Very simple example is to 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('/app1/html')
def static_page():
  return render_template('index.html')

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=5000,debug=True)