Skip to content

Latest commit

 

History

History
76 lines (53 loc) · 4.03 KB

Trivedh_Flask.md

File metadata and controls

76 lines (53 loc) · 4.03 KB

Flask

Flask is a web application framework written in Python. A web framework is a collection of modules and packeges which facilitates or supports the user to create dynamic web applications. It facilitates creation,development and publishing of web applications. Flask is based on the Werkzeug WSGI toolkit and Jinja2 template engine.It includes a built-in development server, unit tesing support, and is fully Unicode-enabled with RESTful request dispatching and WSGI compliance.

WSGI(Web Server Gateway Interface)

Web Server Gateway Interface is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python programming language. It acts as an interface between web servers and web applications.

Jinja2

Jinja2 is a template engine written in pure Python. It combines a template with a certain data source to render dynamic web pages.

Prerequisites for the better understanding of Flask

  • HTML
  • Python

Installation of Flask

Now lets look at the installation of flask and execution of a basic Application "Hello world"


  • Step 1:

    Installation of latest version of Python in the local computer


    Make sure to install the latest version of python in the system using the following link: Python download

  • Step 2:

    Creation of a directory and virtual environment

    Python Virtual environment is used to create an isolated environment for Python projects. A project can have its own dependencies, regardless of what dependencies every other project has. Type the following command in the windows command prompt to create a new directory and virtual environment.




    The above command "py -3 -m venv venv " is for virtual environment creation
  • Step 3:

    Activation

    Now type the following command in the command prompt to activate the virtual environment.



  • Step 4:

    Install flask

    After activation you could notice "(venv)" on the left side implying the creation of environment and now type the following command to install flask"


    "pip install flask"

  • Step 5:

    Creation of the application using a text editor

    Now use a text editor and create a ".py" file and save it in the same directory created previously i.e "app3". The code to execute "Hello world" in flask:


    from flask import Flask

    app = Flask(name)

    @app.route('/')

    def index():
      return '<h1>Hello World!</h1>'



    Explanation of the above code:


    • from flask import Flask:


      Imports the Flask module and all the related libraries

    • app = Flask(__name__)


      Creates the App object.


    • @app.route('/')


      Flask uses route() to say that if the browser requests the address /, then the app should route that request to this index() function.


    • def index():


        return '<h1>Hello World!</h1>'

      Defines a function to return "Hello world!" when the app executes

  • Step 6:

    Set FLASK_APP and run the flask

    To get the output type the following commands



  • Step 7:

    Output

    After running the above command copy the address provided by the prompt after successful execution of the application and paste it in the browser, click enter to see the output.




    The following is the link to the above python file:app2.py