Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Introducing Flask
Lecture: Hello world, Flask-style
0:00 Let's build our first Flask application. First of all, we want to activate our virtual environment, so let's just source our activation script
0:15 and here we go, as you can see the virtual environment is now active and we are sitting in our working folder. We can now launch our text editor
0:28 we already have a hello.py script in here. The first thing I want you to note is that we are dealing with just 6 lines of code,
0:37 actually 4, if we don't factor in the import line here and the blank line, so we are speaking about 4 lines of code for a fully functional Flask app.
0:47 I guess this is where the famous, simple and elegant Flask definition comes from. Let's review our code line by line.
0:57 On the first line, we are importing a Flask class from the Flask package. This class will be our wsgi application.
1:04 On the second line, we are creating an instance of our class and as you can see, we are passing an argument.
1:10 This is the name of the application module or package and it is needed so Flask knows where to look for templates, static files and so on.
1:19 On line 4, we are using the route decorator to tell Flask what URL should trigger our function. So any time the home page is hit by the browser
1:32 the hello function will be triggered and the function itself is super simple, it is simply returning a hello world message.
1:42 So now our script is ready and we want to see it running, right? So how do we do that? We could go back to our terminal window
1:50 and launch the built in Flask server, but we can do better than that because within code we have an integrated terminal window and we can use it
1:58 let's just go to the view menu and click on integrated terminal, and here we go, we have a new window with the terminal.
2:06 And as you can see, we are already in our working folder and in fact, if we check the contents, we see our hello.py script right there.
2:16 Now, before we can continue, we need to activate the virtual environment, so let's do that. Now we're ready, and by the way,
2:30 by the the time you see this screencast, you probably want to activate the virtual environment yet again because as you might remember,
2:37 we already activated it before launching the editor. There is a ticket open on the github repository for Visual Studio Code
2:44 and a fix to go online with the next update in February 2018. So now that the virtual environment is ready we can try and launch our script.
2:56 To launch the built in server, all we need to do is issue this command Flask run, and if we do that, it will fail,
3:06 and the reason is that we didn't tell Flask which is the launch script, we can do that by simply exporting the Flask app variable
3:17 and we set it to our scrap Okay, let's try again, Flask run, there we go, as you can see, now Flask is serving the app hello on local host port 5000.
3:34 Let's go and try it out. Hello world, this is nice, we have a working website up and running with just six lines of Python code
3:51 and the built in Flask server is serving it to us. How can we improve it? Well, first we might want Json as a response
3:59 since we are going to build RESTful APIs anyway, so how do we do that? Let's go back to our app, it turns out
4:08 that Flask comes with a very powerful jsonify function so we can leverage it, and in our function
4:19 we simply go and call jsonify and then we need to pass a Python dictionary, so maybe something like this, it should be good enough, let's save,
4:38 go back to our browser and refresh. And nothing happens. Why is that? Well, the reason is that we didn't relaunch our server
4:46 so let's stop the server and launch it again, so it can pick the new script, and here we go, as you can see,
4:58 now we're getting Json back as a response to our request. Now, powerful, but it is quite annoying that we have to stop the server and relaunch
5:06 every single time we make a change in our scripts. Well, luckily for us, Flask comes with a built in debug mode, and we can activate it
5:15 by simply setting an environment variable, so let's do that. Again, let's stop the server and export Flask debug let's switch this variable on.
5:32 And now, let's run the server again. As you can see, we get a slightly different message now the Flask app is running but it is forced to debug mode,
5:46 so now, any change we do here, it will be picked up by the server. Let's try for example and add a new route— how about we make a log in page,
6:14 okay, let's save, and we can see that the server here has detected a change in hello.py and it is reloading, in fact, the server restarted
6:26 and let's see if we go back and just go to the login page there it is, we didn't need to stop the server,
6:37 so for example, let's say that we change the contents of the string in welcome, you are logged in,
6:52 save, again, the server restarts, we go back, refresh, here we go, back to our script, we only scratched the Flask surface here,
7:01 of course, there is a lot more to Flask to be seen but I believe in a few minutes, we achieved quite a lot we now have our Flask site up and running,
7:11 we know how to handle growth with Flask and we also learned how to import some very nice functionality from Flask.
7:20 Of course, we learned about the built in server and the debug mode. All these features will come in handy in the next segments, when we move on to Eve.