Python-Flask/C2/Request-Response-Cycle-in-a-Flask-Web-App/English
Title of script: The Request-Response Cycle in a Flask web app
Author: Siddhartha Sarkar
Keywords: Video tutorial, Python Flask, view functions, request object, HTTP response, debugging.
Visual Cue | Narration |
Slide 1: The Request-Response Cycle in a Flask web app | Welcome to the Spoken Tutorial on the request-response cycle in a Flask web app. |
Slide 2: Learning Objectives | In this tutorial, we will learn
|
Slides 3: System Requirements | To record this tutorial, I’m using
You can use any text editor and web browser of your choice. |
Slide 5:
Pre-requisites
|
To follow this tutorial, you should have working knowledge of* Linux commands
If not, then please go through the corresponding tutorials on this website. http://spoken-tutorial.org |
Slide 6: The request object | We already said that the Flask app instance has to process the received request.
|
Slide 6: The request object | A few objects are provided to the view functions, where the processing takes place.
|
Slide 7: The request object | The incoming request data can be of several forms. For example
|
Open a Terminal window
|
Let us open the Terminal by pressing Ctrl, Alt and T keys simultaneously on the keyboard. |
[Terminal]
Type cd project_flask & Press Enter |
Now go to the folder project underscore flask which we created earlier using cd command. |
[Terminal]
|
Let us activate our virtualenv.
And press Enter. |
[Terminal]
Highlight (flask_venv) |
Notice that we are now inside the virtual environment flask_venv. |
[Terminal]
Type $ atom hello_flask.py |
Let’s open the hello_flask.py file in a text editor, which we created earlier in this series.. |
[Text Editor]
|
Let’s look at how the request object is made available in a view function.
|
[Text editor]
Type in hello_world(): (As the first line)
|
The request object has many attributes.
|
Highlight
|
This will print the method attribute of the request object.
|
Press Ctrl+S | Save the file. |
[Terminal]
Type
|
Let us start the server to see the changes.
|
[Terminal]
Type
[Enter] |
Now type
python3 <space> hyphen m <space> flask <space> run
|
[Firefox]
Type And press Enter. |
Now, open a web browser.
|
Highlight the output | We got the output as Hello World. |
[Terminal] | Switch to the terminal once again. |
Highlight “Request method is Get” | Observe that the current request method is “GET”.
|
Only narration. | Now let us see how one can send data to the server using the request object.
|
[Text Editor]
Type
def query_args(): (Tab)framework = request.args['framework'] (Tab)return '''<h1>The framework value is: {}</h1>'''.format(framework) |
Let us define a new route to explain this.
|
Press Ctrl+S | Save the file. |
Highlight
|
Here we have written the statement inside the HTML’s h1 heading tag. |
Highlight args in
framework = request.args['framework']
|
Here we have an attribute called args in the request object.
|
[Terminal] | Switch to the terminal. |
Press Ctrl+C | Press Ctrl and C keys to stop the server. |
Type
|
Press the up arrow key to get the command to run the flask application.
|
[Firefox] | Switch to the web browser. |
Type
And press Enter. |
In the address bar, next to 5000 type slash query
and press Enter. |
Highlight the error | We get a “Bad request” error. |
[Terminal] | Switch back to the terminal. |
Highlight
|
Notice that this response has HTTP code 400.
|
Only narration. | So now we have to find out where this syntax issue has occurred.
|
[Text Editor]
|
For this, we have to start the server in the debug mode.
|
[Terminal]
Type $ export FLASK_ENV=development |
Then type export <space>FLASK <underscore> ENV <equal to> development
|
[Terminal]
Type
|
Now type
python3<space>(hyphen)m<space>flask<space>run
|
[Terminal]
|
Observe that now it says debug mode is on.
|
[Firefox]
|
Go back to the browser and refresh the page.
This means that we get the line numbers where the errors have occurred in different files. |
Highlight the different error lines in the traceback of errors.
|
The traceback includes the errors from the corresponding flask files as well.
This is because an error in one file causes errors in other files as well. This is termed as the cascading effect. We have to look for the error in a file that we have written. |
Highlight
|
Scroll down and search for the error in the file hello_flask.py |
Highlight
BadRequestKeyError
|
We have a BadRequestKeyError.
That means that the key “framework” for the dictionary request.args does not exist. To rectify this, we have to modify that line. |
[Text editor]
Type framework = request.args.get('framework') |
Switch to the editor.
Now change the line as shown.
|
Press Ctrl+S | Save the file. |
[Text Editor]
Highlight framework = request.args.get('framework') |
Now even if the key is not found, python will handle this properly.
|
[Terminal] | Switch to the terminal. |
Press Ctrl+C | Once again stop the server by pressing Ctrl and C keys together. |
Type python3 -m flask run | Then run the server again. |
[Firefox] | Go back to the browser and refresh the page. |
Click on the refresh button. | Now we have the output as expected.
|
Next, we will send some value for the framework via the URL. | |
[Firefox]
|
args gets the part of the URL after the question mark in the form of a python dictionary. |
[Firefox] | In the address bar, next to the word query type:
And press Enter. |
Highlight:
|
So now we have the framework attribute of the dictionary set to my_flask.
|
Slide 8: form attribute | The request object has one more important attribute called ‘form’.
|
[Text Editor]
|
We will see how form data can be sent and received using the request object.
|
[Text Editor]
if request.method == 'POST': firstname = request.form.get('firstname') lastname = request.form.get('lastname') |
Inside the form route, define a view function my_form()
|
Highlight:
firstname = request.form.get('firstname') lastname = request.form.get('lastname') |
This block will get executed, if the request is via POST method. |
Slide 9: POST method | In POST request method
|
Slide 9: POST method |
|
Only narration
|
Now let us store the form values into some variables.
|
Type
<h1>The lastname value is: {}</h1>'''.format(firstname, lastname)
|
Inside the if condition which checks the post method, type this code.
|
[Text Editor]
Firstname: <input type="text" name="firstname"> Lastname: <input type="text" name="lastname"> <input type="submit" value="Submit"> </form>
|
Now outside this if condition, we type this code.
This will simply display a web form in the output, if the request method is GET. |
Press Ctrl + S | Save the file. |
[Terminal] | Switch back to the terminal. |
Press Ctrl+C | Stop the server by pressing Ctrl and C keys together. |
Type python3 -m flask run | Then run the server again. |
[Firefox]
|
Switch to the browser.
|
The page is displayed with a form. | |
Let us enter some values in the form. | |
Type First name as Spoken.
|
I will type the First name as Spoken.
|
[Firefox]
The text in the browser |
Here we have received the firstname and lastname from the request object.
|
[Text Editor] | The last thing I want to talk in this section is about “500: Internal Server Error”.
|
Let us write a code snippet to demonstrate this. | |
[Text Editor] | Switch back to the editor. |
Type inside user(name):
|
In the user view function, before the return statement type
a <equal to> 10 slash zero
|
[Terminal] | Switch to the terminal. |
Press Ctrl+C | Stop the server by pressing Ctrl and C keys together. |
Type python3 -m flask run | Now run the server again. |
[Firefox]
|
Switch to the browser.
|
[Firefox] | Observe that we have a ZeroDivisionError. |
Highlight star | Let’s switch to the terminal to see the HTTP status code. |
[Terminal]
|
Here we can see HTTP response code as 500.
|
Since we have the debug mode on, we know the specific reason for the error. | |
With this we come to the end of this tutorial.
Let us summarise. | |
Slide 10: Summary | In this tutorial, we learnt about,
|
Slide: About Spoken Tutorial project | The video at the following link summarises the Spoken Tutorial project.
|
Slide:
Spoken Tutorial workshops |
The Spoken Tutorial Project team conducts workshops and gives certificates.
|
Slide:
Forum for specific questions: |
Please post your timed queries in this forum. |
Slide: Acknowledgement | Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
|
This is Siddhartha Sarkar signing off.
Thanks for watching. |