- What is Flask
- Install Virtual Environment
- How to Install Flask
- How check Flask Version
- A mini_Flask application
- Flask Project Structure - a few options
- (WIP) Flask Bootstrap Sample - a simple project built with Bootstrap
- (WIP) Jinja Template - how to render HTML pages efficiently
- Url_Variable Rule and Creatinng_url_Examle-What is the Rule of Variable and creation of Url
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.
-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.
-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
pip install virtualenv
#Call Virutal Environment and it's local directory Name Like--- Venv or Guru or Data etc....
virtualenv venv
#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
The easiest way to install Flask is to use PIP the official package-management tool.
pip install Flask
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
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
if __name__=="__main__":
app.run()
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.
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
# Creation Routing App
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.
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
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)