ReactorLabs

JavaScript And Ptyhon How To’s With The Occassional Tip And Tricks

Quickly Get Started With Flask

This is a quick introduction to Flask to get you started.

jQN
ReactorLabs
Published in
3 min readJan 25, 2021

--

Minimal Requirements

- Mac OS
- Python 3
- Comfortable with the Terminal
- Code Editor — VS Code, Atom, etc

Python Version

Please use Python 3, as of this article Python 2.7 is officially deprecated.

Virtual Environments

Python 3 comes bundled with the venv module to create virtual environments. Virtual environments isolate your project’s dependencies and prevent version and library conflicts.

Create a project folder and a venv folder within in your machine’s user root directory. Launch the terminal and run the following commands.

$ cd
$ mkdir www
$ cd www
$ mkdir quickstart
$ cd quickstart
$ python3 -m venv quickstartvenv

Note: keeping your projects in a single directory in your machine (in this case www) will help you stay more organized and quickly find them.

Activate The Environment

Before working on a project make sure to activate the corresponding environment. If activated the terminal will show
the name of the activated environment

$ . quickstartvenv/bin/activate
(quickstartvenv) ➜ quickstart

Install Flask

Within the activated environment, use the following command to install Flask

$ pip install Flask

A Minimal Application

Once you have Flask installed create a minimal hello world application.

$ touch run.py

Your minimal app should look like this.


from flask import Flask
app = Flask(__name__)
@app.route(‘/’)
def hello_world():
return ‘Hello, World!’

Run The App

Before running the application you need to tell your terminal the application to work with. Export the FLASK_APP environment variable.

$ export FLASK_APP=run.py
$ flask run
* Running on http://127.0.0.1:5000/

If you forget to export the variable you will see the following error.


Error: Could not locate a Flask application. You did not provide the “FLASK_APP” environment variable, and a “wsgi.py” or “app.py” module was not found in the current directory.

You will need to do this every time unless you save it in the virtual environment. To do that edit the activate file in your virtual environment. Open the following file with your editor.

quickstartvenv/bin/activate

Add the following to activate. FLASK_APP tells the terminal which application to work with, FLASK_ENV enables debug mode and all development features. Development mode reloads the server after code changes and will provide the debugger if things go wrong.

export FLASK_APP=run.py
export FLASK_ENV=development

Deactivate and activate your virtual environment to load the changes and run the app.

$ deactivate
$ . quickstartvenv/bin/activate
$ flask run

If your app is running on development mode you should see the following.

(quickstartvenv) ➜ quickstart flask run
* Serving Flask app “run.py” (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 173–338–857

Launch your browser and navigate to http://127.0.0.1:5000/ where you will see your app running.

Thank you for reading, let’s connect!

Find me on Twitter and say hi. Best.

--

--

ReactorLabs
ReactorLabs

Published in ReactorLabs

JavaScript And Ptyhon How To’s With The Occassional Tip And Tricks

jQN
jQN

Written by jQN

I'm a Full-Stack Software Engineer who believes in the power of technology to change and improve lives. Connect with me on https://twitter.com/FullStackJQN

No responses yet