Python Flask Interview Questions and Answers

by Bharathkumar, on Sep 11, 2022 4:40:46 PM

Interview Questions (58)Q1). What is Flask?

Ans1: Flask is a web micro framework for Python based on “Werkzeug, Jinja 2 and good intentions” BSD licensed. Werkzeug and jingja are two of its dependencies.

Q2). What is the benefit of using Flask?

Ans2: Flask is part of the micro-framework. Which means it will have little to no dependencies on external libraries. It makes the framework light while there is little dependency to update and less security bugs.

Q3). What is the difference between Django, Pyramid, and Flask?

Ans3: Flask is a “microframework” primarily build for a small application with simpler requirements. In flask, you have to use external libraries. Flask is ready to use.Pyramid are build for larger applications. It provides flexibility and lets the developer use the right tools for their project. The developer can choose the database, URL structure, templating style and more. Pyramid is heavy configurable.

Q4). What is Flask-WTF and what are their features?

Ans4: Flask-WTF offers simple integration with WTForms. Features include for Flask WTF are

  • Integration with wtforms
  • Secure form with csrf token
  • Global csrf protection
  • Internationalization integration
  • Recaptcha supporting
  • File upload that works with Flask Uploads

Q5). What is the common way for the Flask script to work?

Ans5: The common way for the flask script to work is

  • Either it should be the import path for your application
  • Or the path to a Python file

Q6). Explain how you can access sessions in Flask?

Ans6: A session basically allows you to remember information from one request to another. In a flask, it uses a signed cookie so the user can look at the session contents and modify. The user can modify the session if only it has the secret key Flask.secret_key.

Q7). Is Flask an MVC model and if yes give an example showing MVC pattern for your application?

Ans7: Basically, Flask is a minimalistic framework which behaves same as MVC framework. So MVC is a perfect fit for Flask, and the pattern for MVC we will consider for the following example

from flask import Flask
app = Flask(_name_)

@app.route(“/”)

Def hello():

return “Hello World”

app.run(debug = True)

In this code your,

  • Configuration part will be

from flask import Flask

app = Flask(_name_)

  • View part will be

@app.route(“/”)

Def hello():

return “Hello World”

  • While you model or main part will be

app.run(debug = True)

Q8). What is a Thread-Local object in Flask?

Ans8: Flask uses thread local objects internally so that user don’t have to pass objects around from function to function within a request in order to stay threadsafe. This approach is useful, but it requires a valid request context for dependency injection or when attempting to reuse code which uses a value pegged to the request.

Q9). What is the requirement to create the database in Flask?

Ans9: Flask supports database powered application (RDBS). Such system requires creating a schema, which requires piping the shema.sql file into a sqlite3 command. So you need to install sqlite3 command in order to create or initiate the database in Flask.

Q10). Explain how you request database connections in Flask?

Ans10: Flask allows to request database in three ways

before_request() : They are called before a request and pass no arguments
after_request() : They are called after a request and pass the response that will be sent to the client
teardown_request(): They are called in situation when exception is raised, and response are not guaranteed. They are called after the response been constructed. They are not allowed to modify the request, and their values are ignored.

Q11). Mention how you can enable debugging in Flask? When enabling debugging in Flask can be useful?

Ans11: There are two ways you can enable debugging in Flask.

  • Either set the flag on the application object
  • OR pass it as a parameter to run If you enable debug support the server will reload itself when code changes and you don’t have to manually restart after each change to the code.

Q12). How can you secure your SQL statement in Flask from SQL injection?

Ans12: You can use questions marks while building SQL statements, it will protect SQL statements from the vulnerability of SQL injections.

Q13). In Flask, explain how does the view function will pass the entries?

Ans13: In Flask, the view function will pass the entries as dicts to the show_entries.html template and return the rendered one.

Q14). Explain how do you get a query string from the flask?

Ans14: To get query string from flask, here we want to get the value of user so we will follow the steps as below

from flask import request

@app.route(‘/data’)

def data ( ) :

user = request.arg.get (‘user’)

Q15). What is the syntax of a decorator used in Flask to match URLs to view functions in Flask apps?

Ans15: “@app.route” , the syntax is used in Flask to match URLs to view functions in Flask apps.

Q16). What is Flask Sijax?

Ans16: Sijax is nothing but a Python/jQuery library to make Ajax easy to use in web applications. Sijax uses JSON to pass data between the server and the browser.

Q17). What is the difference between “g” variable and “session” in the flask?

Ans17: “g” is data shared between different parts of the code base within one request cycle. For example, a database connection or the user that is currently logged in. While session provides you a storage place to store data for a specific browser. Which means using a specific browser, returns for more request.

Q18). Explain how does a request context can be created in Flask?

Ans18: A request context can be created by either

  • Automatically when the application receives a request
  • OR manually, by calling app.test_request_context (‘/route?param=value)

Q19). Explain how you can show all errors in the browser for the Flask?

Ans19: To show all errors in the browser for the Flask, you need to run the Python file on the shell. The command used to see errors in detail is “app.debug = True”

Q20). Explain how can you structure a large Flask application?

Ans20: To structure a large flask application, here are the following steps

  • Stick to the functions and move them to different files, as long as you are assured that it will get imported when the applications starts
  • Use blueprints to assign the views to “categories”. For instance auth, profile, backend, etc.
  • Use the underlying Werkzeug URL map and register functions on there on a central URL map
Topics:Interview Questions with Answers

Comments

Subscribe