Python Flask Basics Welcome to Flask Introduction

Pepe Suarez
Pepe Suarez
18,267 Points

How to run flask locally??

hey guys!! I know its a dumb question but I got used to do flask only in workspaces and now I can't figure out how to run it properly locally. I already have my virtual enviroment but how can I use it and run it in my computer? Is there any special text editor that can get the job done? Or does anyone knows a good tutorial to do this? Thanks for the help!!

4 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 58,946 Points

Hi Pepe, I am current working on Flask in my home environment, too (Ubuntu 14.04). I am using virtualenvwrapper which adds some convenient organization to virtualenv

# Setting up my virtual environment for Python 3 called "flasksocial"
$ mkvirtualenv --python=/usr/bin/python3 flasksocial

# install flask related modules (note I'm on the social network course. you might not need bcrypt, yet)
(flasksocial)$ pip install flask
(flasksocial)$ pip install peewee
(flasksocial)$ pip install flask-login
(flasksocial)$ pip install py-bcrypt
(flasksocial)$ pip install flask-bcrypt
(flasksocial)$ pip install flask-wtf

# app.py file will have HOST and PORT settings
(flasksocial)$ python app.py

After app.py is running, I can see it in my browser at http://0.0.0.0:8080.

Update: if you are only using virtualenv then the process would be:

# Set up virtual environment (set Python 3 as default)
$ virtualenv -p /usr/bin/python3 flasksocial
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in flasksocial/bin/python3
Also creating executable in flasksocial/bin/python
Installing setuptools, pip...done

# Activate virtual environment
$ . flasksocial/bin/activate

# Updated prompt shows env name
(flasksocial)$ 

# Install flask related modules
(flasksocial)$ pip install flask
(flasksocial)$ pip install peewee
(flasksocial)$ pip install flask-login
(flasksocial)$ pip install py-bcrypt
(flasksocial)$ pip install flask-bcrypt
(flasksocial)$ pip install flask-wtf
Andreas cormack
Andreas cormack
Python Web Development Techdegree Graduate 31,142 Points

great answer by Chris Freeman. Dont forget to activate your virtualenv once created.

#on linux
source bin/activate

# windows
Scripts/activate
Chris Freeman
Chris Freeman
Treehouse Moderator 58,946 Points

The best part of using the virtualenvwrapper is that it automatically invokes the env upon creation.

After that it's back to the workon, deactivate, etc. cycle.

For standard virtualenv, you are right, I would have to activate it.

Chris Freeman
Chris Freeman
Treehouse Moderator 58,946 Points

I updated my answer to include a regular virtualenv setup.

Pepe Suarez
Pepe Suarez
18,267 Points

Awesome Guys! Thanks!! I am getting there!! Just trying to understand better virtualenvwrapper lol!

Pepe Suarez
Pepe Suarez
18,267 Points

Hey guys I got everything working but I don't know why I can't see the app on the browser. The console is showing that the app is running in http://0.0.0.0:8000/ but when i go to this address nothing shows up! What am I doing wrong?? Thanks for the help!

Chris Freeman
Chris Freeman
Treehouse Moderator 58,946 Points

Can post what the console displays? Also post the line in app.py that launches the flask server, and the values of HOST, PORT, and DEBUG.