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
|
Slide 5:
Pre-requisites
|
To follow this tutorial, you should have working knowledge of
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]
Type “, request” |
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. |
Next, 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 @app.route('/query') 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
<h1>The framework value is: {}</h1> |
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]
Press Ctrl+C |
For this, we have to start the server in the debug mode.
Before that, we need to stop the current server by pressing Ctrl and C keys together. |
[Terminal]
Type $ export FLASK_ENV=development |
Then type export <space>FLASK <underscore> ENV <equal to> development
and press Enter. |
[Terminal]
Type $ python3 -m flask run |
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.
|
Highlight the different error lines in the traceback of errors.
|
The traceback includes the errors from the corresponding flask files as well.
|
Highlight
|
Scroll down and search for the error in the file hello_flask.py |
Highlight
|
We have a BadRequestKeyError here.
|
[Text editor]
|
Switch to the editor.
|
Press Ctrl+S | Save the file. |
[Text Editor]
|
Now even if the key is not found, python will handle this properly.
That means, instead of causing a 400 error, it will return None. |
[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 that we 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]
@app.route('/form', methods=['GET', 'POST']) |
We will see how form data can be sent and received using the request object.
Let us define a new route called form. We allow both GET and POST as possible request methods. Before the if statement, type the code as shown here. |
[Text Editor]
def my_form(): if request.method == 'POST': firstname = request.form.get('firstname') lastname = request.form.get('lastname') |
Now inside this form route, define a view function my_form()
|
Highlight:
firstname = request.form.get('firstname') lastname = request.form.get('lastname') |
|
Slide 9: POST method | In POST request method
|
Slide 9: POST method | * For example - you enter your username and password while logging in to a website.
|
Only narration
|
Now let us store the form values into some variables.
|
[Text Editor] | Switch back to the editor. |
Type
<h1>The lastname value is: {}</h1>'''.format(firstname, lastname)
|
Inside the if condition which checks the post method, type the following piece of code.
|
[Text Editor]
Firstname: <input type="text" name="firstname"> Lastname: <input type="text" name="lastname"> <input type="submit" value="Submit"> </form>
|
Now outside the if condition, type the following code.
|
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 retrieved the firstname and lastname from the request object.
|
So this shows how the form attribute of the request object is used. | |
[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 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 received 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. |