Python is very well documented, but there are still some tricky parts when it comes to setting up a web app. This simple step-by-step guide might help you.
Step 0: Make a Project Folder
This one should be obvious.
Step 1: Create a Virtual Environment
Virtual environments allow you to install things for one project only, so you can easily keep track of all requirements for your app. Additionally, you avoid installing stuff globally.
You don’t have to use a virtual environment, but it is strongly recommended. It’s also very easy to set up:
- If you haven’t done so before, install virtualenv with the command
pip install virtualenv
- Create a virtual environment with the command
There are two different recommendations where to create a virtual environment and how to call it:
- call it venv and put it in your project folder
- call it the same as your project and put it in a hidden .virtualenv folder in your home directory
Just choose the method you like best.
- Activate your virtual environment in a terminal. To do so, you need to call the script
activateinside your virtual environment folder.
Under Windows, it should be located in venv_folder/Scripts/.
For all other operating systems, you should find it in venv_folder/bin/.If the activation was successful, your terminal should now print
(virtual_environment_name)before your name. The virtual environment is now activated in this terminal, and nowhere else! To deactivate it again, simply use the command
Step 2: Install Flask
Make sure your virtual environment is activated and run
pip install Flask
Step 3: Write Some Code
Obviously, this step depends on what you want to build. These are the basic rules:
- .html files need to be in a new templates subfolder.
- .css files should be placed in static/css
- images also need to be in static, preferably in a new folder static/img
- the python file that starts the app (let’s call it app for now) should be directly in your project folder.
You can define routes to describe how to navigate between pages. This is how your app.py could look:
from flask import Flask, render_template, request app = Flask(__name__) @app.route("/") def home(): return render_template("landing.html") if __name__ == "__main__": app.run()
Step 4: Set Up Heroku
- Register at heroku.com
- Install heroku CLI. This step depends on your operating system. Find out more here.
- Log in via heroku CLI:
- Create your app by calling
from your project directory.
Step 5: Deploy Your App
Heroku needs you to create a few files first:
requirements.txtcontains all requirements. If you are using a virtual environment, you can create this file with
pip freeze > requirements.txt
runtime.txtcontains your python version. You can add this file manually.
Procfile(there is no suffix!) tells heroku how to start your app. See this official article for more information.
Your project folder should now look similar to this:
: |_ (git related files)
: \_ css
: : |
: : |_ style.css
: \_ img
: : |
: : |_ logo.png
: |_ landing.html
It’s time to deploy your app! Use git:
git add . git commit git push heroku master
If something goes wrong, have a look at the logs with
That’s it! You have just build your first web app!