Getting Started with Flask

We’re doing a FREE ONLINE class next Tuesday 😱. If you want to learn all this explained by a real person 👩‍🏫👨‍💻, sign up now! (*limited spots available*): https://blog.rmotr.com/free-online-class-getting-started-with-flask-e529cf29c057

A few weeks ago, we released a free Flask Tutorial. We then learned from our students that they were having issues with the first steps of the process. We then decided to write this complementary post to help you get started with Flask development. If you want to learn the internals of Flask, you can just go ahead and check our FREE tutorial (also available in Udemy).

To make this process even easier, we’ve created a sample repo on Github that has the minimum required to have a Flask application running: https://github.com/rmotr-curriculum/getting-started-with-flask

Step 1 — Platform setup and installing Flask

For this guide we’ll be working with Cloud9, an Online IDE. We use C9 for our classes and it’s a great tool for people starting with programming. C9 let us focus on the important details of programming and writing apps, and we can forget about environment setups. After you have everything running on C9, we encourage you to try setting up your own environment.

The first thing we’re going to do is create a new workspace, from your C9 dashboard:

You’ll be dropped in a view like the one in the image below, where you’ll have to provide some important information:

Workspace name : Give a meaningful name to your workspace, we’ll use it later to browse our webapp.

: Give a meaningful name to your workspace, we’ll use it later to browse our webapp. Clone from Git… : *IMPORTANT*, use the clone URL from our Github repo: https://github.com/rmotr-curriculum/getting-started-with-flask.git

: *IMPORTANT*, use the clone URL from our Github repo: https://github.com/rmotr-curriculum/getting-started-with-flask.git Choose a template: *SUPER IMPORTANT* Choose the Python Template, as shown on the image, so we have a few Python goodies installed by default.

Finally, just click on the Create Workspace button to have your workspace created.

Step 2 — Familiarize yourself with the Environment

Once you create your workspace, you’ll be dropped in a view like this one:

It’s like a traditional IDE where you can browse files and edit them right in pace. Try opening app.py or run_app.py and browsing their content:

In the section below the code, you can see a command line interface. It’s a terminal that C9 makes accessible to run operating system commands. Go ahead and try a few commands like ls or pwd .

Step 3 — Installing Flask

To run our webapp, we have to first install Flask. And to do that, we’re going to create a virtualenv. Virtualenvs are a way for us to install Python libraries in an isolated and secure environment. To create the virtualenv, execute the following command in the command line:

$ mkvirtualenv flask-tutorial

The mkvirtualenv will create a virtual environment with the name flask-tutorial . You can choose whatever name you prefer. After you run the command, your terminal will be updated and it’ll look something like:

Now, before your prompt (the text before the dollar sign displaying my username and location), you’ll see the name of the virtualenv that we had chosen flask-tutorial . That means that the virtualenv is currently active, it’s REALLY important for the rest of this guide to make sure your virtualenv is ALWAYS active (that you can see the name in your terminal as shown in the image above).

Now we’re can install Flask. We’ll execute the following command:

$ pip install Flask

Your console will display some information messages and Flask should be correctly installed:

To verify that you’ve installed it correctly, you can open the Python shell and type import flask . If you don’t get an error, that means you installed it correctly:

Step 4 — Running the Flask app

Now that we have our virtualenv and Flask is installed, we can run the app. To do so (and making sure that your virtualenv is active), run the following command:

$ python run_app.py

In this case, we’re running the run_app.py script (that you can see in the repo):

This script is starting a simple webserver that will process all our incoming requests with our Flask webapp.

Now point your browser to your C9 public URL. To construct your public C9 URL you’ll need your workspace name and your C9 username:

https://[WORKSPACE-NAME]-[USERNAME].c9users.io

For example, mine is: https://getting-started-with-flask-santiagobasulto-1.c9users.io/

You should see the following in your browser, which means everything is working 👏:

Step 5 — Making changes to your app

You can go to app.py and make changes to your app. The flask webserver will reload automatically whenever it detects a change.

For example, I’ll make a change in the home_view function (return a different text) and save the file and you’ll see Flask automatically detecting the change and reloading the app:

If I refresh my browser I’ll see the new text being returned:

At this point you’ve successfully ran your app 💯, and you can start making your own changes. To learn more about flask, check out our Free Flask Tutorial or our online 👉Advanced Python Programming course 👈.