Building your first Flask Application

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

Building your first Flask Application

One of the reasons many Python developers love using Flask is that the framework makes it easy to get started. It only takes about five lines of code to build a functional web application. In this chapter we're going to start from scratch and build out a Flask application and deploy it to the internet.

Here's what our application is going to look like when we're finished

By the end of these two chapters, you'll have built this Flask application
By the end of these two chapters, you'll have built this Flask application

In the next section, we're going to get started.

  • |

Lesson Transcript

  • [00:00 - 00:11] In today's video, we're going to be going over how to build our very first Flask application. Now before we get started, there are a few things we'll need to set up first.

  • [00:12 - 00:21] The most important one is installing Python 3. If you're on a Mac running OS X or are running Ubuntu, chances are this is probably already pre-installed.

  • [00:22 - 00:35] If you're using Windows or Python 3 is not already installed on your system, there are detailed installation instructions located in the manuscript as well as in the appendix. You also need a text editor.

  • [00:36 - 00:44] The one we use is called Visual Studio Code, but you can use whatever text editor you're comfortable with. And then the next one is a terminal.

  • [00:45 - 00:57] You'll need to be able to open a terminal to run commands to start Flask applications. Once you have these installed, we can get started by building our applications.

  • [00:58 - 01:13] In this course, we're going to go over building multiple applications. One thing that you'll discover is that it's possible for these different projects to have dependencies on different versions of libraries or even different versions of Python's.

  • [01:14 - 01:26] So as a result, we can run into some conflicts naturally if there is a discrepancy between our projects. The solution to this discrepancy is to create virtual environments.

  • [01:27 - 01:43] Virtual environments allow us to isolate the version of libraries that we're using as well as which Python version we're referring to specifically for each project. This helps ensure consistency both between projects but also across developers.

  • [01:44 - 02:08] And so you can ensure that everyone's working on the same Python version as well as the version of libraries that you use for your project. You can visualize virtual environments somewhat like this where each application folder has its own virtual environment that points to specific version of Python that it needs as well as a specific version of libraries that it needs.

  • [02:09 - 02:24] We'll be going into more detail later, but this is just some context before you start creating virtual environments. In this project, we're going to be building something to simulate dice rolling.

  • [02:25 - 02:36] So I noticed that I don't actually have any dice in my home. So I wanted to write a quick Python script to turn and tell me random numbers.

  • [02:37 - 02:44] Now our final application is going to look something like this. It's not very fancy, but it does the job.

  • [02:45 - 02:56] And it's doing the job based on a simple Python script. This is one thing that's neat about Python is we're able to write this logic in essentially four lines of code.

  • [02:57 - 03:08] And now to turn it into a web application, thanks to Flask, we can do that in only an additional three lines of code. So that's what we're going to do in the next video.

  • [03:09 - 03:43] And then we'll go over a larger Flask application that we're going to build using what we learn in the next video. [ Silence ]