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