Flask WSGI
Based on the following Youtube Tutorial.
Other useful links and tutorials can be found here:
How to Install and Use Flask on Windows for Beginners (2019)
- Make sure have installed Python (currently 3.7.3)
- Create a folder where your App will be stored, eg. Q:\flashDemo
- Create a Virtual Environment - so that configuration only applies to the virtual environment only
- Open CMD
- Nav to Q:\flashDemo folder
- Q:\flaskDemo>py -m venv env
- Slight delay while it installs
- Creates a new folder called 'env'
- Activate the env by:
- env\Scripts\activate
- Next prompt will be:
- (env) Q:\flaskDemo>
- Now install Flask: pip install flask
- Now cd to env folder
- Create in text editor (e.g. Notepad++) a new file called: app.py
- Back to CMD prompt
- set an environment variable to tell what file to run when flask runs:
- set FLASK_APP=app.py
- In text editor, now work on app.py
- from flask import Flask // get Flask class from package
- Setup a route:
- @app.route('/') // route to a URL endpoint in my app
- def index(): //method to run at the route/endpoint
- return '<h1>Hello!</h1>' //returns directly to the browser
- CMD prompt:
- Run the flask app setup earlier
- flask run
(env) Q:\flaskDemo>flask run
* Serving Flask app "app.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)
- Copy the URL: http://127.0.0.1:5000/
- paste it into browser
- Now to pass parameter modify to include a name parameter in app.py
@app.route('/<name>')
def index(name):
return f'<h1>Hello {name}!</h1>'
- Now ctrl-c to kill the app and re-run flash run again
- Type in browser:
- http://127.0.0.1:5000/Joe
- Hello Joe! appears in browser
- When finished
- Ctrl-C to stop app
- deactivate
|