An introduction to the Flask Python web app framework

bhavya_agrawal

Bhavya Agrawal

Posted on May 20, 2022

An introduction to the Flask Python web app framework

In computer programming, a software framework is an abstraction in which software, providing generic functionality, can be selectively changed by additional user-written code, thus providing application-specific software.
"-Source Wikipedia"

Flask is a web framework. This means flask provides you with tools, libraries and technologies that allow you to build a web application. This web application can be some web pages, a blog, a wiki or go as big as a web-based calendar application or a commercial website. Flask is based on WSGI(Web Server Gateway Interface) toolkit and Jinja2 template engine.

Flask makes the web development process rapid & easy with few lines of code.

Create an Simple web Application with Flask -

  1. Open a terminal and install Flask using PIP:

    $ pip install Flask
    
  2. Create a file with .py extension and paste below code-

    from flask import Flask    
    app = Flask(__name__)   # Flask constructor
    
    # A decorator used to tell the application
    # which URL is associated function
    @app.route('/')      
    def hello():
        return 'HELLO WORLD'
    
    if __name__=='__main__':
       app.run()
    

    The variable name is passed as first argument when
    creating an instance of the Flask object (a Python Flask
    application). In this case name represents the name of the
    application package and it’s used by Flask to identify
    resources like templates, static assets and the instance
    folder.

    route() decorator, to bind the URL to a function. It also
    contains method keyword use to bind request method along
    with url.
    If the server get the request on the binding URL then it calls

    the function bind to that URL.

  3. Now run the file, you will get the localhost URL and paste it in your browser.

Image description

Project Structure in Flask

< PROJECT ROOT >
   |
   |-- app/__init__.py
   |-- app/
   |    |-- static/
   |    |    |-- <css, JS, images>         # CSS files, Javascripts files
   |    |
   |    |-- templates/
   |    |    |
   |    |    |-- includes/                 # Page chunks, components
   |    |    |    |
   |    |    |    |-- navigation.html      # Top bar
   |    |    |    |-- sidebar.html         # Left sidebar
   |    |    |    |-- scripts.html         # JS scripts common to all pages
   |    |    |    |-- footer.html          # The common footer
   |    |    |
   |    |    |-- layouts/                  # App Layouts (the master pages)
   |    |    |    |
   |    |    |    |-- base.html            # Used by common pages like index, UI
   |    |    |    |-- base-fullscreen.html # Used by auth pages (login, register)
   |    |    |
   |    |    |-- accounts/                 # Auth Pages (login, register)
   |    |    |    |
   |    |    |    |-- login.html           # Use layout `base-fullscreen.html`
   |    |    |    |-- register.html        # Use layout `base-fullscreen.html`  
   |    |    |
   |    |  index.html                      # The default page
   |    |  page-404.html                   # Error 404 page (page not found)
   |    |  page-500.html                   # Error 500 page (server error)
   |    |    *.html                        # All other pages provided by the UI Kit
   |
   |-- requirements.txt
   |
   |-- run.py
   |
   |-- ************************************************************************
Enter fullscreen mode Exit fullscreen mode

The relevant files:

  • run.py - the entry point used to start the application
  • requirements.txt - a file that specifies all dependencies (for now is just Flask)
  • app - the application folder where we will add our code
  • app/_init_.py - This file is required to let us use the app as a Python Package
  • app/static - this folder will contain design assets: JS, CSS, and images.
  • templates - the directory with pages, layouts, and components used by Flask to generate some nice pages for us
💖 💪 🙅 🚩
bhavya_agrawal
Bhavya Agrawal

Posted on May 20, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related