Instructor: 00:00 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.
00:14 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.
00:26 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.
00:41 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.
00:49 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.
00:58 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."
01:14 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.
01:26 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.
01:36 After started, it says that our server is running on local host on port 5000. Let's check that out and see.
01:45 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.
02:00 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.
02:18 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.
02:33 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.
02:49 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.
03:01 Returning to the browser, I can reload the page, and everything works like it was before.
03:07 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.
03:26 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.
03:42 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.
03:54 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.
04:06 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.
04:20 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.
04:37 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.
05:00 When I browse to that on the website, it returns it as the server response.