Flask is a lightweight web application framework written in Python and based on the Werkzeug WSGI toolkit and Jinja2 template engine. It is BSD licensed.
Flask is called a microframework because it keeps the core simple but extensible. There is no database abstraction layer, form validation, or any other components where third-party libraries already exist to provide common functionality. However, Flask supports extensions, which can add such functionality into an application as if it was implemented in Flask itself. There are extensions for object-relational mappers, form validation, upload handling, various open authentication technologies, and more.
This tutorial will show you how to run Flask in CSE's server environment.
- Login to the student web development server, cheshire. Only cheshire is configured to run Flask.
% ssh cheshire
- If you haven't already created your personal CSE web environment yet, follow this tutorial to set it up:
https://wiki.cse.buffalo.edu/services/content/how-create-your-cse-home-page
- Following the install-flask routine, install Flask. Within your public_html directory, create a directory called myproject. Change directory to myproject. Install the Flask virtual environment (venv).
[cheshire] ~/public_html% mkdir myproject
[cheshire] ~/public_html% cd myproject
[cheshire] myproject% python3 -m venv venv
- To activate your virtual environment, you'll need to run the bash shell. Change your current shell to bash. CSE sets your default shell to tcsh. But you'll need to use bash for this job.
[cheshire] myproject% bash
- Activate your Flask virtual environment (venv):
bash-4.2$ . venv/bin/activate
- Within the activated environment, use the following command to install Flask:
(venv) bash-4.2$ pip install Flask
- Following the a-minimal-application routine, create a file called hello.py in your myproject directory:
(venv) bash-4.2$ vi hello.py
- Now run the application in development mode (when running flask run, optionally set the specific runtime port with the -p flag):
$ export FLASK_APP=hello
$ flask run --host=0.0.0.0 -p 5001
* Serving Flask app 'hello' (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 all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://128.205.32.39:5001/ (Press CTRL+C to quit)
- Access your app from the web:
http://www-student.cse.buffalo.edu:5001/
Consult cheshire's httpd error log:
[cheshire] ~% tail -f /var/log/httpd/httpd-error_log
- https://en.wikipedia.org/wiki/Flask_%28programming%29
- https://flask.pocoo.org/
- https://flask.palletsprojects.com/en/2.0.x/installation/#install-flask
- https://flask.palletsprojects.com/en/2.0.x/quickstart/#a-minimal-applica...