Flask Interview Questions and Answers
Ques 21. How to create the RESTFul application in the Flask framework?
There are so many extensions that we can use to create the RESTFul application in Flask. We need to choose them depending upon the requirements.
A few of them are as follows:
- Flask-RESTFul.
- Flask-API.
- Flask-RESTX.
- Connexion.
Ques 22. How can we get a user agent in Flask?
We can use the request object, see the given below code:
From flask import Flask
From flask import request
app=Flask(_name_)
@app.route(“/”)
Def index():
val=request.args.get(“var”)
user_agent=request.headers.get(‘User-Agent’)
response
=
"""
<p>
Hello, World! {}
<br/>
You are accessing this app with {}
</p>
"""
.
format
(val, user_agent)
return
response
Hello, World!{}
you are accessing this app with {}
if_name_==”_main_”;
app.run(host=”0.0.0.0”,port=8080)
Ques 23. How to use URLs in Flask?
We need to call the view function with parameters and give them values to generate URLs. Mainly Flask url_for the function we use here. It can also be used in Flask templates.
Ques 24. How to create an admin interface in Flask?
Here we also need to use one of the Flask extensions named Flask-Admin. It helps to group all individual views together in classes. Another extension named Flask-Appbuilder can be used. It has a built-in admin interface.
Ques 25. Explain the process of using the session in Flask?
The session is mainly used to store data in requests. We can store and get data from the session object in Flask. As shown below code we can try to do this:
From flask import Flask
sessionapp=Flask(_name_)
@app.route(‘/use_session’)
Def use_session():
If ’song’ not in session;
session[;songs’]={‘title’;’Tapestry’,’Bruno Major’}
return session.get(‘songs’)
@app.route(‘/delete_session’)
def delete_session():
session.pop(‘song’,None)
return “removed song from session”
Most helpful rated by users: