Understanding Flask
arpit-dwivedi opened this issue · 23 comments
Flask is a Python framework that is used to build basic webpages. To make a successful webpage one should have knowledge of basic HTML and CSS. So I tried to make a basic webpage that works on my localhost.
The installation of a Flask should be carried in a virtual environment.
- We first import Flask from flask.
- We created an app object.
- Create the main variable in which we would run our object and set debug to True, so if any error occurs we would be notified.
- This can be considered as the template for any of the flask application.
- Flask uses route() which routes the browser to that path if any request is made.
- This would enable the function defined under the route.
- We can create multiple routes to route towards multiple paths.
- Inside the index function under the first rout I have returned render templates which basically helps to render any HTML or CSS file.
- The HTML and CSS file is very basic which has one header and a body.
- So this is the link to the very basic webpage
Understanding_Flask
- Flask is a web application framework in python
- Flask depends on the Jinja template engine and the Werkzeug WSGI toolkit
I tried with a basic small web app using flask which prints hello world! in the browser and below is the explanation for the same:
- First I imported the Flask class. An instance of this class will be WSGI application
- Next I created the App object.
- And then used the route() decorator to tell Flask what URL should trigger the function.
- The function returns the message to display in the browser ( in this example"Hello , World!")
This is the link a detailed explantion for setting up environment and developing a flask web app.
And this the code , i used in the above example.
Just save it as hello.py or something similar. Make sure to not call your application flask.py because this would conflict with Flask itself.
I have made a simple app that shows a html page displaying my name
- First import Flask and create a simple app.
- Then used the app.route() decorater to render the html file using render_template.
- The html shows a background image and my name in the foreground.
- The output for same can be seen
- This is the link to see the code and other files in my repo.
Flask
What is Flask?
Flask is an API of Python that allows us to build up web-applications. It was developed by Armin Ronacher. Flask’s framework is more explicit than Django’s framework and is also easier to learn because it has less base code to implement a simple web-Application. A Web-Application Framework or Web Framework is the collection of modules and libraries that helps the developer to write applications without writing the low-level codes such as protocols, thread management, etc. Flask is based on WSGI(Web Server Gateway Interface) toolkit and Jinja2 template engine.
Today I learnt about a web framework called "Flask". It is a micro web framework which can add application features or develop and support web applications. It is based upon Pocco projects such as Werkzeug and Jinja2.
Werkzeug is a toolkit for WSGI applications. Werkzeug can realize software objects for request, response, and utility functions.
Jinja2 is a template engine which combines templates with a data model to produce result documents.
After referring to documentation of Flask and few examples, I was able to create a web application to display "Hello World!!".
-
Firstly, I had to create a new virtual environment to work on this small demonstration and work on other projects using Flask. Then I was able to install flask using the python package manager(pip). After activating the virtual environment, I used the command "
pip install flask
" to install the flask to my virtual environment. -
Then I opened the text editor and typed in the code to display "Hello World!!" to the web application or the web page. To execute the code I wrote the following commands:
- The url on the last line, lead me to the following output:
Flask is a web application framework written in Python. Armin Ronacher, who leads an international group of Python enthusiasts named Pocco, develops it. Flask is based on Werkzeug WSGI toolkit and Jinja template engine. Both are Pocco projects.
-
The Web Server Gateway(WSG) Interface is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python programming language
-
Jinja2 is a template engine written in pure Python. It combines a template with a certain data source to render dynamic web pages.
I tried making a basic web app using flask which prints hello world! in the browser and below are the steps(for Ubuntu users) I took:
- Created python virtual environment,(in the particular directory of your choice)
python3 -m venv venv (name of virt env)
- Installed flask in it.
pip install flask
- Create a python file in the same directory with this code
- Then finally run the following commands:
export FLASK_APP=<name of your python file>
flask run
- Now you'll see a link, open that in your browser.
Hello Sir, here's what I tried creating using Flask:
To Do List App
What is Flask?
Flask is an API of Python that allows us to build up web-applications.. Flask’s framework is more explicit than Django’s framework and is also easier to learn because it has less base code to implement a simple web-Application. A Web-Application Framework or Web Framework is the collection of modules and libraries that helps the developer to write applications without writing the low-level codes such as protocols, thread management, etc. Flask is based on WSGI(Web Server Gateway Interface) toolkit and Jinja2 template engine.
Flask is easy to learn and very easy to deploy ml apps with flask.
ML Model Deployment using Flask
- The basic thing is to create and train a machine learning model.
- The model building file should include all pre-processing, feature engineering, training and saving model.
- There should be HTML and CSS files that includes the frontend of the web app and this will be used to interact with the Flask app.
- The main task is to create Flask file app.py which consists of routes:
- one for displaying homepage.
- other for taking input as well as displaying output.
- Even we have to add the methods argument to accept the data entered by the user on the webpage.
- Later pass the inputs to the model which is load in the same file and return the results to html file through render_templates.
- In the html file there should be some placeholder to show output provided by flask python script.
- Now you are ready to deploy and enjoy your project.
But if you want to host like a website, you require some hosting, isn't it?
- There's a solution -> Heroku which is a PaaS or Platform as a Service to host your our model and make it public.
- The step are almost the same but you have to commit that model with all the required file over to GitHub.
- Then connect that repo on Heroku.
- All the other things will be done by the service itself.
- And also its free to use on individual basis.
What is Flask?
- Flask is a web framework that provides libraries to build lightweight web applications in python. It is developed by Armin Ronacher who leads an international group of python enthusiasts (POCCO).
- It is based on WSGI toolkit and jinja2 template engine.
- Flask is considered as a micro framework.
What is WSGI?
- It is an acronym for web server gateway interface which is a standard for python web application development.
- It is considered as the specification for the universal interface between the web server and web application.
What is Jinja2?
- Jinja2 is a web template engine which combines a template with a certain data source to render the dynamic web pages.
Note: To install flask on the system, we need to have python 2.7 or higher installed on our system.
Deploy ML model using Flask
- I have made a simple project to elaborate how to deploy a Machine Learning model using Flask API ,
- I have build a simple regression model using scikit-learn’s built-in LinearRegression() function. And I used a very simple data set with 4 attributes and 8 rows.
- This data used here is clean and is ready to be applied for a classifier.
- After our model is built, we will be saving our trained model using a library called pickle.
- Python pickle module is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled so that it can be saved on disk.
- What pickle does is that it “serializes” the object first before writing it to file.
- Here’s the code of building a simple regression model and then saving the model by serializing it using pickle.
- Now run this, your model is now trained and your model gets saved in the directory where your project files are stored in your local machine.
- Setting up flask-app:
- Make sure flask is installed.
- The flask code can be explained in three sections:
- Loading the saved model
a. We load the model.pkl file and initialize the flask app. - Redirecting the API to the home page index.html
- Redirecting the API to predict the result (salary)
- Starting the flask server
- Loading the saved model
- This project has four major parts:
1. model.py — This contains code for our Machine Learning model to predict employee salaries based on training data in
‘hiring.csv’ file.
2. app.py — This contains Flask APIs that receives employee details through GUI or API calls, computes the predicted value based on our model and returns it.
3. template — This folder contains the HTML template (index.html) to allow user to enter employee detail and displays the predicted employee salary.
4. static — This folder contains the css folder with style.css file which has the styling required for out index.html file. - Running the project: Create the machine learning model by running below command from command prompt -
python model.py
- Run app.py using below command to start Flask API
python app.py
- Navigate to URL http://127.0.0.1:5000/ (or) http://localhost:5000
This is the code and this is the output
To host the web app , Heroku is one of the free hosting platform , heroku is a container-based cloud Platform as a Service (PaaS).
Deploy Machine Learning Model Using Flask
- Deployment of machine learning models or putting models into production means making your models available to the end users or systems. However, there is complexity in the deployment of machine learning models.
- In the case of developing a ML model, this can be done by using just three python files:
- model.py = In this file the ML model should be implemented and trained. The trained model should then be saved
using the Pickle library in order to be ready to be used to give real-time predictions when used on the Web. - app.py = In this file Flask is used to handle POST requests we get from request.py and then return the results. To do so, the trained ML model is retrieved using the Pickle library.
- request.py = It is used to request the feature to the server and retrieve the results.
- model.py = In this file the ML model should be implemented and trained. The trained model should then be saved
- We can also add some HTML, CSS or any other type of content to improve the online graphics of the model.
- Using just Flask we can be able to run our model on our local machine using a local server. If we want instead to make our model available on the Web, we can deploy our Flask App to an online platform such as Heroku.
- Heroku is a cloud platform which can be used to deploy websites and other services. In order to use Heroku, it is necessary to have: a Heroku account, the Heroku CLI tool and git installed on your machine.
- This was a brief introduction on how ML models can be deployed using FLASK.
Understanding Flask and building a simple application.
What is Flask?
-
Flask is a web application framework written in Python.
-
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.
-
WSGI stands for "Web Server Gateway Interface".
-
It is used to forward requests from a web server (such as Apache or NGINX) to a backend Python web application or framework.
-
From there, responses are then passed back to the webserver to reply to the requestor.
-
Jinja is a modern and designer-friendly templating language for Python, modeled after Django’s templates.
-
It is fast, widely used, and secure with the optional sandboxed template execution environment.
A simple application
The output would be the following:
Link to the detailed explanation of flask, code, and application.
ML Model Deployment Using Flask
What is Model Deployment?
- The concept of deployment in data science refers to the application of a model for prediction using new data.
- Building a model is generally not the end of the project. Even if the purpose of the model is to increase knowledge of the data, the knowledge gained will need to be organized and presented in a way that the customer can use it.
- Depending on the requirements, the deployment phase can be as simple as generating a report or as complex as implementing a repeatable data science process.
- In many cases, it will be the customer, not the data analyst, who will carry out the deployment steps.
- For example, a credit card company may want to deploy a trained model or set of models (e.g., neural networks, meta-learner) to quickly identify transactions, which have a high probability of being fraudulent. However, even if the analyst will not carry out the deployment effort it is important for the customer to understand upfront what actions will need to be carried out in order to actually make use of the created models.
What do you need to deploy an application?
In order to deploy any trained model, you need the following:
- A trained model ready to deploy — save the model into a file to be further loaded and used by the web service.
- A web service — that gives a purpose for your model to be used in practice.
- A cloud service provider — you need special cloud servers to deploy the application and for simplicity, for example, Heroku.
Heroku
- Heroku is a container-based cloud Platform as a Service (PaaS).
- Developers use Heroku to deploy, manage, and scale modern apps.
what is flask?
Flask is a web framework that provides libraries to build lightweight web applications in python.
What is WSGI?
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.
What is jinja2?
Jinja2 is a template engine written in pure Python. It combines a template with a certain data source to render dynamic web pages.
Works to do?
- Install python latest version on your computer.
- Create a new folder on desktop and a new environment on that folder.
- After all these things install flask on the environment using code 'pip install flask'.
- Now open a text editor or python ide and create a file with .py extension.
- Now write python code:
"from flask Import Flask
app = Flask(name)
@app.route('/')
def index():
return '< h1>Hello World< /h1>' - Now open the link 🔗 http://127.0.0.1:500/
OUTPUT
Hello World
🕸️ Flask:
Flask is a web application framework. It is a collection of modules and packages which help the user to create a dynamic web application. It helps in the creation, development, and deployment of web applications. Flask is based on WSGI and Jinja2.
WSGI: (Web Server Gateway Interface) is an interface between web servers and web applications.
Jinja2: Jinja2 combines a template with a certain data source to render dynamic web pages.
Today, I install the flask module using 'pip install flask' inside the virtual environment (venv) in python.
I learned to create the 'hello world' of the flask.
👉 First I import flask
👉 create the App object
👉 route the '/' to index() function
👉 define the index() function and return the HTML code to display 'Hello world!'
👉 set the FLASK_APP to the .py file and run the flask to display the HTML content in the browser.
Then, I write some HTML code inside the given code and display it on my browser.(link) 😃 📚
Understanding & Building Simple Application in Flask
Flask
There are many modules or frameworks which allows to build your webpage using python like bottle, django, flask etc. But the real popular ones are Flask. Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It makes the process of designing a web application simpler. Flask lets us focus on what the users are requesting and what sort of response to give back.
Web Server Gateway Interface (WSGI)
The Web Server Gateway Interface (WSGI) is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python programming language.
Jinja2
Jinja is a web template engine for the Python programming language. A web templating system combines a template with a certain data source to render dynamic web pages.
My work
-
I have created a small application using flask to understand how flask works.
-
I started with the creating a simple web application which display “Hello World!”. Further, I created templates and two HTML pages and connected both the HTML pages with a navigation.
-
I have explained step by step approach in here
You can find detailed explanation of the flask as well as simple application which I have built using flask here in this repository.
What is Flask?
-
Flask is an API of Python that allows us to build up web-applications. It was developed by Armin Ronacher. Flask’s framework is more explicit than Django’s framework and is also easier to learn because it has less base code to implement a simple web-Application. A Web-Application Framework or Web Framework is the collection of modules and libraries that helps the developer to write applications without writing the low-level codes such as protocols, thread management, etc. Flask is based on WSGI(Web Server Gateway Interface) toolkit and Jinja2 template engine.
-
Flask is a web framework that provides libraries to build lightweight web applications in python. It is developed by Armin
Ronacher who leads an international group of python enthusiasts (POCCO). -
It is based on WSGI toolkit and jinja2 template engine.
-
Initially i got some error while installing flask but i search the error on google i was able resolve it then i started working on creating web page i got lot more errors and i dont know why theses errors are coming. I tried allot but still errors coming
We need to have python installed to use flask. Once you have python install flask using the following command.
pip install Flask
We can use HTTP methods in Flask. HTTP protocol is the foundation of data communication in world wide web. Different methods of data retrieval from specified URL are defined in this protocol. The methods are described down below.
1 GET It is the most common method which can be used to send data in the unencrypted form to the server.
2 HEAD It is similar to the GET but used without the response body.
3 POST It is used to send the form data to the server. The server does not cache the data transmitted using the post method.
4 PUT It is used to replace all the current representation of the target resource with the uploaded content.
5 DELETE It is used to delete all the current representation of the target resource specified in the URL.
By default, the Flask route responds to the GET requests. However, this can be altered by providing methods argument to route() decorator. Python and HTML code is here.
https://github.com/SathvickN/Flask_beginner
What is Flask
Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.
Installation
Type 'pip install flask' in command prompt
My Work
- Firstly, i installed Flask on my system .
- Then i created a virtual environment 'my_app' on my system.
- In the 'app.py' file, i imported Flask and return render_template on my index function.
- I returned html file in my index function which was linked to a CSS file.
Code Link
https://github.com/Ajay0511/DevIncept-AI/tree/main/Task_1_FlaskApp
Flask-App-Building-Steps
Flask initial task
Flask App Building with Python
- Start the app building by going to folder where you want to save code. In my case:
`D:>cd D:\Study\Internsala\Devincept\Devincept Intership AI
D:\Study\Internsala\Devincept\Devincept Intership AI>mkdir app5
D:\Study\Internsala\Devincept\Devincept Intership AI>cd app5`
-
Created a new environment as venv
py -3 -m venv venv
-
Activated with the following script
venv\Scripts\activate
-
Installed the flask package using:
pip install flask
-
Written the flask code in python using spyder interface as app.py. [https://github.com/jaikushwaha7/Flask-App-Building-Steps/blob/main/app2.py].
-
Set the flask app by connecting to app2.py and running it.
set FLASK_APP=app2.py
flask run
-
On running the code and link was provided where the output of the app was displayed in a HTML format.
So Flask a simple python web application framework that allows us to build up web-applications.
What is Flask?
Flask is a web framework, it’s a Python module that lets you develop web applications easily. It’s has a small and easy-to-extend core: it’s a microframework that doesn’t include an ORM (Object Relational Manager) or such features.
It does have many cool features like routing, template engine. It is a WSGI web app framework.
Related course: Python Flask: Create Web Apps with Flask
What is a Web Framework?
A Web Application Framework or a simply a Web Framework represents a collection of libraries and modules that enable web application developers to write applications without worrying about low-level details such as protocol, thread management, and so on.
What is Flask?
Flask is a web application framework written in Python. It was developed by Armin Ronacher, who led a team of international Python enthusiasts called Poocco. Flask is based on the Werkzeg WSGI toolkit and the Jinja2 template engine. Both are Pocco projects.
WSGI
The Web Server Gateway Interface (Web Server Gateway Interface, WSGI) has been used as a standard for Python web application development. WSGI is the specification of a common interface between web servers and web applications.
Werkzeug
Werkzeug is a WSGI toolkit that implements requests, response objects, and utility functions. This enables a web frame to be built on it. The Flask framework uses Werkzeg as one of its bases.
jinja2
jinja2 is a popular template engine for Python. A web template system combines a template with a specific data source to render a dynamic web page.
Flask 1 .docx
http://127.0.0.1:5000/ shows the output "Hello World"