Build A Live Web Application In Minutes — Using Python, Flask, and Azure Web Apps (Part 3)
Local Development
Stop! Did you complete the steps of Part 2? That’s really really important so if you haven’t gone through that article yet, please don’t continue until you have finished it.
Alright, well it would be pretty weird if the only way to test your application changes was by literally making it live. That doesn’t sound like much of a test.
Sure, you can set up different deployment slots… blah blah blah. My point is that you should be able to develop and test your application on your machine (locally) and then push your changes when you’re ready to risk the rest of the world seeing it.
Part 3, this, is all about how to get that done!
Install Python
If you don’t have Python installed yet, don’t worry, I won’t tell anyone. It’ll be our secret. Just grab it from their page real quick and come back. Alternatively, if you installed Miniconda (which I highly recommend for future development), you’ve already got what you need.
Check Out The Requirements
Open your Azure Web App repository (the folder that houses the code that you pushed in Part 2). You’ll notice that there’s a file in here called requirements.txt.
This file is really important, so let’s open it up and take a look at it.
Each line of this file contains a Python dependency, and it’s version, that is required for the project. For example, notice that we have Flask listed. We’re deploying a Flask application — so that makes sense.
Over the course of building out your app, you’ll probably add more and more dependencies. It’s important that you keep this file updated when you do so because when you deploy your code, this file is how the build server will know which dependencies to install as it builds your web application.
One way to update your requirements.txt file is to run this command after you install a package:
pip freeze > requirements.txt
Keep in mind that if you’re not using conda or virtualenv (you really should be using at least one of these — I talk about getting conda in Part 1), this command will dump all the packages that have been installed in your current environment into this requirements.txt file. That means that it’ll list the packages that you aren’t necessarily using for this project. Using conda/virtualenv to create isolated environments makes this a non-issue.
Install Requirements
We want to run our project locally, so we need to install the packages from the requirements.txt file.
Open a shell and navigate to your project’s repository.
If you have conda installed through Miniconda or Anaconda, you’re going to want to create a new environment for this project. Use this command (at the time of writing, this isn’t well supported in Powershell so use the Anaconda prompt or Git Bash):
conda create --name NAME_OF_ENVIRONMENT python
This creates the new environment and immediately begins an installation of the latest Python. Once you’ve finished that up, we need to make that new environment active via:
conda activate NAME_OF_ENVIRONMENT
Now, everyone (conda or no conda), let’s install our requirements. Run:
pip install -r requirements.txt
Run Your App
Awesome. When that’s done, let’s actually test out our Flask app! You should still be in the project’s repository folder. In order to run your Flask app, you need to set an environment variable that tells Flask where your application is defined. Then we should let Flask know that we’d like to run this in development mode.
In Powershell, you can use this command (I think, I haven’t tested this — I just used Git Bash :)). As a general note, if the commands below are giving you issues, just switch to another shell like Git Bash:
$env:FLASK_APP="application.py"
$env:FLASK_ENV="development"
In other terminals:
set FLASK_APP=application.py
set FLASK_ENV=development
Now, go ahead and run:
flask run
You should see something like:
It’s telling us that our code is running on port 5000 of our machine. Let’s head over to our browser and see what’s going on. Type the URL you see in the terminal, http://127.0.0.1:5000/ or http://localhost:5000.
Get the wine ready.
Making Changes
Keep that terminal open. Let’s open up our application.py and change the contents to this:
from flask import Flaskapp = Flask(__name__)@app.route("/")
def hello():return "Damiene was here."
Now, go back to your browser and refresh the page. Ta-Da!
The changes you make to your project are reflected without you needing to restart. This happens because we set the FLASK_ENV variable to “development”.
Deploy Your Changes
Now, review the the changes you just made. Once you’re satisfied, just like we did in Part 2, open a terminal and navigate to your repository. Finally, commit your changes and then push them.
Once the process is complete, go to your Azure Web App’s URL and view your handiwork!
Wrapping Up
Welp. You did it. I’m seriously impressed. This wasn’t easy and you stuck with it. You deserve that wine. But just a little — there’s a Web App that needs to get made.
I really hope this series was helpful! If you’re having any issues at all — drop a note in the comment and I’ll try to help out.
Happy devving!
Thanks for reading!
Consider hitting that follow button. Also, follow me on Twitter @damienestewart. I can’t promise I’ll say anything interesting, but it’ll give me that good “new follower” feeling :).