Skip to content

Project 1 - Flask Setup

Objectives

  • Install PyCharm
  • Create a simple Flask site

 

For this assignment, you will be familiarizing yourself with a new work environment. You need to complete the steps in order.

Step 1. Install Python

Go to python.org and get the latest version of Python and install it on your computer. The version I am using is 3.11.1 on my Mac. When you are finished installing you can move the Python installer to the trash.


Step 2. Install PyCharm Community software.

Go to jetbrains.com/pycharm/download and select the Community edition for you Mac. This is a pretty big file. It will ask you to copy the file to your application folder. This is acceptable. Then browse to your application folder and open the PyCharm CE application. It will ask permission to open, since it was downloaded off the internet. This is also acceptable. Once it is open, I would add it to your dock. You will be using this software a lot this semester.


Step 3. Create a new project in PyCharm CE.

You should get a “Welcome” screen when you open PyCharm CE. Click the “New Project” button.

Location: Select a location where all your project files will live. I like putting these on my Desktop on my Mac.
/Users/bryanmarshall/Desktop/4214/2023_Spring/Inclass_Project

Make sure you select “New environment using Virutalenv” bullet. The environment should go into the Inclass_Project folder and be called venv.

The Base Interpreter should be “python3.11”.

Make sure you do not “create a main.py”. If you do like I did below, you will just need to delete the main.py file.


Step 4. Update and install Flask using Terminal in PyCharm

Go to PyCharm and Settings in the Top Menu bar. On the left browse to Project: Inclass_Project and find the Python Interpreter. This is where you will install most packages. A package is basically a library that will extend your Python abilities.

Click the plus sign and search for Flask. You should find version 2.2.2 by Armin Ronacher. Select it and then select Install Package.

You may get a message about installing a command line tool, this is okay.

Next, let’s install a different way. This time open the terminal tab at the bottom of PyCharm. We want to install python-dotenv package. This will allow us to store two (2) environmental variables that we will need when running flask.

Type in the following code:

pip install python-dotenv

When this is finished you need to create a new file called .flaskenv in your project folder. Be careful NOT to create the file inside the venv folder. Add the following lines to that the file. Be sure of a couple things. Make sure you keep the . in front of the flaskenv folder. And second, whatever you name your application filename, is what should be where the hello.py is.

FLASK_APP=hello.py
FLASK_DEBUG=True

Step 5. Create a “requirements” list.

Let’s create a requirements list. This list is used to tell other applications what libraries are needed to run this specific application and what version you need. The command is found below. This will automatically create a requirements.txt file in the right location. Every time you add new libraries make sure you run the command in the terminal.

pip freeze > requirements.txt

Step 6. Create a Hello World App

Create a new file named ‘hello.py’ in your project folder; NOT in the venv folder. There are two functions we are building with this application. These functions are routes in flask. The first is what is going to happen when you go to just the IP address. The second is what happens if you add a name to the end of the IP address.

Add the following code to the file:

from flask import Flask

app = Flask(__name__)


@app.route('/')
def index():
    return '<h1>Hello World!</h1>'


@app.route('/<name>')
def user(name):
    return f'<h1>Hello, {name.title()}! </h1>'


if __name__ == '__main__':
    app.run(debug=True)

Step 7. Testing your Application

Go down to the terminal again and type:

flask run

Note: to get out of the running web server just hit command c or control c, depending on your system. You should get a small warning but you should get an IP address where the flask web server is running on your local machine.

If you go to the link this is what should be showing.

If you add your name to the end of the IP you should get a different result.


Step 8. To turn in, take a screenshot of your project with your name in the URL.