/01_Flask_Tutorial

Flask is a lightweight and flexible web framework for Python. It's designed to make getting started with web development in Python quick and easy, with the ability to scale up to complex applications. Flask was created by Armin Ronacher and first released in 2010.

Primary LanguagePythonMIT LicenseMIT

Topics


What is Flask

Flask is a lightweight web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. Compared to Django, Flask provides a lightweight codebase and more freedom to the developer.

Virtual Environment

-We use a module named virtualenv which is a tool to create isolated Python environments. virtualenv creates a folder that contains all the necessary executables to use the packages that a Python project would need.

-A Python Virtual Environment is an isolated space where you can work on your Python projects, separately from your system-installed Python.

-You can set up your own libraries and dependencies without affecting the system Python.

-We will use virtualenv to create a virtual environment in Python.

Why do we need a virtual environment?

-Imagine a scenario where you are working on two web-based Python projects one of them uses Django 4.0 and the other uses Django 4.1 (check for the latest Django versions and so on). In such situations, we need to create a virtual environment in Python that can be really useful to maintain the dependencies of both projects.

Installing Virtula environment

#Installing Virtula environment
pip install virtualenv

Call Virutal Environment

#Call Virutal Environment and it's  local directory Name  Like--- Venv or Guru or Data etc....
virtualenv venv

Activate a virtual environment

#Activate a virtual environment based on your OS
For windows > venv\Scripts\activate
For linux > source ./venv/bin/activate

#If your Facing Any error Then Try this

Set-ExecutionPolicy RemoteSigned -Scope Process

Install Flask

The easiest way to install Flask is to use PIP the official package-management tool.

Install Flask Command

pip install Flask

How to check Flask version

Open a Python console or platoform (type python in terminal) and check the installed version as below:

import flask
flask_version = flask.__version__
print(f"Installed Flask version: {flask_version}")

       Or
flask --version

Mini App

Code Here ⚙️

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

if __name__=="__main__":
    app.run()

So what did that code do?

1. First we imported the Flask class. An instance of this class will be our WSGI application.

2. Next we create an instance of this class. The first argument is the name of the application’s module or package. name is a convenient shortcut for this that is appropriate for most cases. This is needed so that Flask knows where to look for resources such as templates and static files.

3. We then use the route() decorator to tell Flask what URL should trigger our function.

4. The function returns the message we want to display in the user’s browser. The default content type is HTML, so HTML in the string will be rendered by the browser.


Debug Mode

Code Here ⚙️

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

# debug mode running on 8000 port
if __name__=="__main__":
    app.run(debug=True, port=8000)

> The flask run command can do more than just start the development server. By enabling debug mode, the server will automatically reload if code changes, and will show an interactive debugger in the browser if an error occurs during a request.

> Warning ⚠️ > The debugger allows executing arbitrary Python code from the browser. It is protected by a pin, but still represents a major security risk. Do not run the development server or debugger in a production environment.


# Creation Routing App

Code Here ⚙️

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'This is Index Page'

@app.route('/login')
def login():
    return 'This is Login Page'

@app.route('/hello')
def hello():
    return 'Hello, World'

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

Modern web applications use meaningful URLs to help users. Users are more likely to like a page and come back if the page uses a meaningful URL they can remember and use to directly visit a page.

Use the route() decorator to bind a function to a URL.

Rendering Templates

Code Here ⚙️

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def index():
    return render_template('index.html')

@app.route("/")
def about():
    return render_template('about.html')

if __name__=="__main__":
    app.run()
    
python filename.py
      or
start flask
 

In flask, html file are served from the 'templates' folder by default and all the static file; images, css, js, etc are served from the 'static' folder.

These folders should be present in the root directly of your python application

structure


Crl Rule and Url Creation

Code Here ⚙️

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

# string
@app.route('/string/<string:value>')
def string(value):
    return f"<p>Hi this is a string value {value}</p>"

# int
@app.route('/int/<int:value>')
def int(value):
    return f"<p>Hi this is a int value {value}</p>"

# float
@app.route('/float/<float:value>')
def float(value):
    return f"<p>Hi this is a float value {value}</p>"

# path
@app.route('/path/<path:value>')
def path(value):
    return f"<p>Hi this is a path value {value}</p>"

# uuid
@app.route('/uuid/<uuid:value>')
def uuid(value):
    return f"<p>Hi this is a uuid value {value}</p>"



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