Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Create a minimal website in Python using the Flask Microframework

    Will ButtonWill Button

    In this lesson, you will learn:

    How to install Flask Use Flask to create a minimal website Build routes in Flask to respond to website endpoints Use Variable Rules to pass parts of the URL to your functions as keyword parameters



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: The first step in building a basic Flask application is to install Flask using pip install Flask. Now with Flask installed, we can go to our code editor and start writing our code.

    I'll create a new Python file inside of my folder, and I am going to call it app.py. You can use any name except flask.py because that would conflict with the Flask module itself.

    From the Flask library, we are going to import Flask, then we'll create a variable called App that will create an instance of the Flask class. The argument we provide under name is the name of our application's modular package.

    Since we only have a single module in this example, we'll use under name, and this tells Flask where to find static templates and code.

    Next, we'll create a decorator for our route and define the route. The route we define is going to be a / to act as the root of our website.

    We'll create a Python function that's executed when this route is accessed. This helloWorld function will just return the string, "Hello World." That's all we need to start our Flask app and have it return "Hello World."

    Switching over to the terminal, we need to export an environment variable name FLASK_APP and set it to the name of the Python file that Flask is going to look to, to execute its code.

    We'll then start the development server with flask run, and it's going to find the value of that FLASK_APP under environment variable and use that to identify the code that it should run.

    After started, it says that our server is running on local host on port 5000. Let's check that out and see.

    Sure enough, when we navigate to the site, it returns the string, "Hello World." I want to show you a few more of the basics features of Flask, but every time I make a change to the code, I have to restart the development server so that we can see those changes.

    Rather than do that, I am going to create a new file and I am going to call it run-local.sh. In that file, I'll set my interpreter as bash. I am going to export the FLASK_APP environment variable just like we had to do in the shell.

    I am going to export another environment variable called FLASK_DEBUG with a value of one. That's going to start my development server in debug mode so that every time it sees the file or the application code change, it restarts the development server for me.

    Finally, we'll do the flask run command to start the server. If I check my current environment variables, you can see the FLASK_APP environment variable we set earlier. I am going to unset that just to prove to you that the startup script will work.

    If I check again, environment variable is gone. I am just going to add the execute boot to our startup script. Now, we can start the development server by executing the script.

    Returning to the browser, I can reload the page, and everything works like it was before.

    Let's add a new route. We'll call this one foo and note that I put a trailing slash on the end of this. We'll give it a function that just returns a string that says, "The Foo Page." I'll create another route call /bar, and I am not going to put a trailing slash on this one.

    We'll give it a function called bar. This is going to return the string, "The Bar Page." I'll save that. The development server should restart on its own, and we can now browse to /foo, and it turns The Foo Page. Take a look at what it did there.

    I am going to remove the trailing slash from foo and hit enter. The trailing slash gets automatically added onto it and The Foo Page is returned to the browser.

    Now, if I go to /bar, it loads up the string, "The Bar Page." If I try bar with a trailing slash on the end, we get a 404 error -- page not found.

    This functionality is built into Flask specifically so that its response mimics the same responses and same behaviors as Apache and some of the other common web servers available today.

    Flask routes can also have variables in them as well. I am going to create a new route called /hello/ and then a variable name called user. We can identify this as a variable by the < > on each side of it.

    What that allows us to do is in our function, we can pass in user as a variable to that, or as a parameter to that function. Now that variable is available inside of our function, just like any other Python variable would be, we can return the string and insert in that the user parameter.

    When I browse to that on the website, it returns it as the server response.