0% found this document useful (0 votes)
96 views

flask notes

Flask is a Python web application framework developed by Armin Ronacher, designed to simplify web development by allowing developers to focus on higher-level functionalities without dealing with low-level details. It is built on the Werkzeug WSGI toolkit and Jinja2 templating engine, and is often referred to as a micro framework due to its simplicity and extensibility. The document provides a step-by-step guide on installing Flask, creating a minimal application, setting up a virtual environment, and running the Flask app in a development environment.

Uploaded by

mriga jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

flask notes

Flask is a Python web application framework developed by Armin Ronacher, designed to simplify web development by allowing developers to focus on higher-level functionalities without dealing with low-level details. It is built on the Werkzeug WSGI toolkit and Jinja2 templating engine, and is often referred to as a micro framework due to its simplicity and extensibility. The document provides a step-by-step guide on installing Flask, creating a minimal application, setting up a virtual environment, and running the Flask app in a development environment.

Uploaded by

mriga jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

FLASK

What is Web Framework?


Web Application Framework or simply Web Framework represents a collection of libraries
and modules that enables a web application developer to write applications without having to
bother about low-level details such as protocols, thread management etc.
What is Flask?
Flask is a web application framework written in Python. It is developed by Armin Ronacher,
who leads an international group of Python enthusiasts named Pocco. Flask is based on the
Werkzeug WSGI toolkit and Jinja2 template engine. Both are Pocco projects.
 WSGI - Web Server Gateway Interface (WSGI) has been adopted as a standard for
Python web application development. WSGI is a specification for a universal
interface between the web server and the web applications.
 Werkzeug- It is a WSGI toolkit, which implements requests, response objects, and
other utility functions. This enables building a web framework on top of it. The Flask
framework uses Werkzeug as one of its bases.
Jinja2
Jinja2 is a popular templating engine for Python. A web templating system combines a
template with a certain data source to render dynamic web pages.
 In the context of software development, a template is a predefined format or pattern
used as a starting point for creating documents, code, or other structures. Templates
provide a way to separate the structure and presentation of content from the actual
data, allowing for easy reuse and consistency.
Flask is often referred to as a micro framework. It aims to keep the core of an application
simple yet extensible. Flask does not have built-in abstraction layer for database handling,
nor does it have form a validation support. Instead, Flask supports the extensions to add such
functionality to the application.
Step 1: Install Flask
pip install Flask
Step 2. Creating minimal flask application

from flask import Flask


app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello World’

if __name__ == '__main__':
app.run(debug=True)

Importing flask module in the project is mandatory. An object of Flask class is our WSGI
application.
Flask constructor takes the name of current module (__name__) as argument.
The route() function of the Flask class is a decorator, which tells the application which URL
should call the associated function.

Step 3. Create and activate new virtual environment (Creating Virtual Environment)

You can use virtualenv or conda to do same.


1.Go to present project folder
2.conda create -- name environment_name (e.g. conda create project1env)
3.conda activate environment_name (e.g. conda activate project1env)
 To deactivate virtual environment
e.g. conda deactivate project1env

Step 4: Install the Python Extension


Make sure you have the Python extension installed in VSCode. You can install it from the
Extensions view (Ctrl+Shift+X).
Step 5: Run the Flask App
In VSCode, open the app.py file and right-click in the editor. Choose "Run Python File in
Terminal" from the context menu.
Alternatively, open the integrated terminal in VSCode and run:

python app.py
Your Flask app should now be running, and you can access it in your web browser at
http://127.0.0.1:5000/ or http://localhost:5000/.
Remember to stop the Flask app by pressing Ctrl+C in the terminal when you're done testing
or developing.
 FLASK_APP=app.py
 flask run
The commands set FLASK_APP=app.py and flask run are used to set the Flask application
entry point and run the development server, respectively. Here's what each command does:
1. Set FLASK_APP:
 set FLASK_APP=app.py is a command used in the Windows command
prompt to set the FLASK_APP environment variable to the value app.py.
This environment variable tells Flask which Python file contains the Flask
application. In this case, it indicates that the Flask application is in the file
named app.py.
 On Unix-based systems (Linux, macOS), the equivalent command is export
FLASK_APP=app.py.
2. Run Flask Application:
 After setting the FLASK_APP environment variable, you use the command
flask run to start the development server and run your Flask application.
 The flask run command looks for the FLASK_APP environment variable to
determine which Python file contains the Flask application. It then runs the
Flask development server, allowing you to access your application locally.
 By default, the development server will be accessible at http://127.0.0.1:5000/
or http://localhost:5000/.
 Press Ctrl+C in the terminal to stop the Flask development server when you're
done testing or developing.

You might also like