SlideShare a Scribd company logo
1 of 37
By:Mohamed Essam
Flask
Flask
What Flask is ?
 Flask is a small framework by most standards, small enough to be called a “microframework.”
But being small does not mean that it does less than other frameworks.
Flask was designed as an extensible framework from the ground up; it provides a solid core with the
basic services, while extensions provide the rest
Flask
Flask
What Flask is ?
 Flask is a web application framework written in Python. It is developed by Armin Ronacher, who
leads an international group of Python enthusiasts named Pocco.
 Flask is based on the Werkzeug WSGI toolkit and Jinja2 template engine. Both are Pocco
projects.
 WSGI is a specification for a universal interface between the web server and the web
applications, It is a WSGI toolkit, which implements requests, response objects, and other utility
functions. This enables building a web framework on top of it.
 The Flask framework uses Werkzeug as one of its bases.
 Jinja2 is a popular templating engine for Python. A web templating system combines a template
with a certain data source to render dynamic web pages.
Flask
What Flask is ?
 Flask also implements a particular design pattern, or way that our program and code is
organized. For Flask, the design pattern is generally MVC, or Model–view–controller:
Flask
What Flask is ?
 Flask also implements a particular design pattern, or way that our program and code is
organized. For Flask, the design pattern is generally MVC, or Model–view–controller:
 The controller is our logic and code that manages our application overall, given user input. In
Flask, this will be our Python code.
The view is the user interface, like the HTML and CSS that the user will see and interact with.
The model is our application’s data, such as a SQL database or CSV file.
Flask
Install Flask in this environment
 Most Python packages are installed with the pip utility, which virtualenv automatically adds to all
virtual environments upon creation. When a virtual environment is activated, the location of the
pip utility is added to the PATH.
Flask
Install Flask in this environment
 All Flask applications must create an application instance. The web server passes all requests it
receives from clients to this object for handling, using a protocol called Web Server Gateway
Interface (WSGI). The application instance is an object of class Flask, usually created as follows:
 The only required argument to the Flask class constructor is the name of the main module or
package of the application. For most applications, Python’s __name__ variable is the correct
value.
Flask
Install Flask in this environment
 All Flask applications must create an application instance. The web server passes all requests it
receives from clients to this object for handling, using a protocol called Web Server Gateway
Interface (WSGI). The application instance is an object of class Flask, usually created as follows:
 The only required argument to the Flask class constructor is the name of the main module or
package of the application. For most applications, Python’s __name__ variable is the correct
value.
Flask
Install Flask in this environment
 All Flask applications must create an application instance. The web server passes all requests it
receives from clients to this object for handling, using a protocol called Web Server Gateway
Interface (WSGI). The application instance is an object of class Flask, usually created as follows:
 The only required argument to the Flask class constructor is the name of the main module or
package of the application. For most applications, Python’s __name__ variable is the correct
value.
Flask
Routes and View Functions
 Clients such as web browsers send requests to the web server, which in turn sends them to the
Flask application instance. The application instance needs to know what code needs to run for
each URL requested, so it keeps a mapping of URLs to Python functions. The association
between a URL and the function that handles it is called a route.
 The most convenient way to define a route in a Flask application is through the app.route
decorator exposed by the application instance, which registers the decorated function as a route.
The following example shows how a route is declared using this decorator:
Flask
Routes and View Functions
 The portion enclosed in angle brackets is the dynamic part, so any URLs that match the static
portions will be mapped to this route. When the view function is invoked, Flask sends the
dynamic component as an argument. In the earlier example view function, this argument is used
to generate a personalized greeting as a response.
 The dynamic components in routes are strings by default but can also be defined with a type.
For example, route /user/ would match only URLs that have an integer in the id dynamic
segment. Flask supports types int, float, and path for routes. The path type also represents a
string but does not consider slashes as separators and instead considers them part of the
dynamic component.
Flask
Server Startup
 The __name__ == '__main__' Python idiom is used here to ensure that the development web
server is started only when the script is executed directly. When the script is imported by another
script, it is assumed that the parent script will launch a different server, so the app.run() call is
skipped.
 Once the server starts up, it goes into a loop that waits for requests and services them. This loop
continues until the application is stopped, for example by hitting Ctrl-C. There are several option
arguments that can be given to app.run() to configure the mode of operation of the web server.
During development, it is convenient to enable debug mode, which among other things activates
the debugger and the reloader. This is done by passing the argument debug set to True
Flask
Responses
 When Flask invokes a view function, it expects its return value to be the response to the request.
In most cases the response is a simple string that is sent back to the client as an HTML page.
 But the HTTP protocol requires more than a string as a response to a request. A very important
part of the HTTP response is the status code, which Flask by default sets to 200, the code that
indicates that the request was carried out successfully.
 When a view function needs to respond with a different status code, it can add the numeric code
as a second return value after the response text. For example, the following view function
returns a 400 status code, the code for a bad request error:
Flask
Responses-HTTP
HTTP (Hypertext Transfer Protocol) is the system the internet uses to interact and communicate
between computers and servers. When a URL is entered into a browser, an HTTP request is sent to
a server, which interprets the request and sends appropriate HTTP response, which, if all goes as
expected, contains the requested information to be displayed by the web browser.
Flask
Responses
 Flask view functions have the option of returning a Response object. The make_response()
function takes one, two, or three arguments, the same values that can be returned from a view
function, and returns a Response object. Sometimes it is useful to perform this conversion inside
the.
Flask
Responses
 The request cookie is what is send from the client to the server (thus what the browser
provides). The response cookie are the cookies that you want to place in the browser. The next
connection from the browser that accepted the cookie from the response object will provide the
cookie in the request object.
Flask
Responses
 There is a special type of response called a redirect. This response does not include a page
document; it just gives the browser a new URL from which to load a new page. Redirects are
commonly used with web forms.
 A redirect is typically indicated with a 302 response status code and the URL to redirect to given
in a Location header.
Flask
Host command
 The --host argument is a useful option because it tells the web server what network interface to
listen to for connections from clients. By default, Flask’s development web server listens for
connections on localhost, so only connections originating from within the computer running the
server are accepted. The following command makes the web server listen for connections on the
public network interface, enabling other computers in the network to connect as well
Flask
Templating Language
 A template is a file that contains the text of a response, with placeholder variables for the
dynamic parts that will be known only in the context of a request. The process that replaces the
variables with actual values and returns a final response string is called rendering. For the task
of rendering templates, Flask uses a powerful template engine called Jinja2.
Flask
Rendering Templates
 By default Flask looks for templates in a templates subfolder located inside the applica‐ tion
folder. For the next version of hello.py, you need to store the templates defined earlier in a new
templates folder as index.html and user.html
Flask
Rendering Templates- Variables
 The {{ name }} construct used in the template references a variable, a special placeholder that
tells the template engine that the value that goes in that place should be obtained from data
provided at the time the template is rendered. Jinja2 recognizes variables of any type, even
complex types such as lists, dictionaries and objects. The following are some more examples of
variables used in templates:
Flask
Rendering Templates- Control Structures
 Jinja2 offers several control structures that can be used to alter the flow of the template. This
section introduces some of the most useful ones with simple examples. The following example
shows how conditional statements can be entered in a template
Flask
Rendering Templates- Control Structures
 Another common need in templates is to render a list of elements. This example shows how this
can be done with a for loop:
Flask
Rendering Templates- Control Structures
 Another common need in templates is to render a list of elements. This example shows how this
can be done with a for loop:
Flask
Rendering Templates- Control Structures
 Another common need in templates is to render a list of elements. This example shows how this
can be done with a for loop:
Flask
Rendering Templates- Control Structures
 Another common need in templates is to render a list of elements. This example shows how this
can be done with a for loop:
Flask
Static Files
 Web applications are not made of Python code and templates alone. Most applications also use
static files such as images, JavaScript source files, and CSS that are referenced from the HTML
code. You may recall that when the hello.py application’s a static entry appeared in it. This is so
because references to static files are treated as a special route defined as /static/. For example,
a call to url_for('static', filename='css/styles.css', _external=True) would return
http://localhost:5000/static/css/styles.css.
Flask
Static Files
 Web applications are not made of Python code and templates alone. Most applications also use
static files such as images, JavaScript source files, and CSS that are referenced from the HTML
code. You may recall that when the hello.py application’s a static entry appeared in it. This is so
because references to static files are treated as a special route defined as /static/. For example,
a call to url_for('static', filename='css/styles.css', _external=True) would return
http://localhost:5000/static/css/styles.css.
Flask
Static Files
 Web applications are not made of Python code and templates alone. Most applications also use
static files such as images, JavaScript source files, and CSS that are referenced from the HTML
code. You may recall that when the hello.py application’s a static entry appeared in it. This is so
because references to static files are treated as a special route defined as /static/. For example,
a call to url_for('static', filename='css/styles.css', _external=True) would return
http://localhost:5000/static/css/styles.css.
Flask
url_for with variable
Flask
Rendering Templates- Inhertance
 Portions of template code that need to be repeated in several places can be stored in a separate
file and included from all the templates to avoid repetition:
Bootstrap
Starter template
Setup some environment variables
Flask Request object
In the client-server architecture, the request object contains all the data
that is sent from the client to the server. As we have already discussed in
the tutorial, we can retrieve the data at the server side using the HTTP
methods.
Flask Request object
In the client-server architecture, the request object contains all the data
that is sent from the client to the server. As we have already discussed in
the tutorial, we can retrieve the data at the server side using the HTTP
methods.

More Related Content

What's hot

.NET Core, ASP.NET Core Course, Session 19
 .NET Core, ASP.NET Core Course, Session 19 .NET Core, ASP.NET Core Course, Session 19
.NET Core, ASP.NET Core Course, Session 19aminmesbahi
 
Share point review qustions
Share point review qustionsShare point review qustions
Share point review qustionsthan sare
 
Create Home Directories on Storage Using WFA and ServiceNow integration
Create Home Directories on Storage Using WFA and ServiceNow integrationCreate Home Directories on Storage Using WFA and ServiceNow integration
Create Home Directories on Storage Using WFA and ServiceNow integrationRutul Shah
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiTiago Knoch
 
.NET Core, ASP.NET Core Course, Session 7
.NET Core, ASP.NET Core Course, Session 7.NET Core, ASP.NET Core Course, Session 7
.NET Core, ASP.NET Core Course, Session 7aminmesbahi
 
Enterprise Software Architecture
Enterprise Software ArchitectureEnterprise Software Architecture
Enterprise Software Architecturerahmed_sct
 
How to call REST API without knowing any programming languages
How to call REST API without knowing any programming languages How to call REST API without knowing any programming languages
How to call REST API without knowing any programming languages Marc Leinbach
 
Spring review_for Semester II of Year 4
Spring review_for Semester II of Year 4Spring review_for Semester II of Year 4
Spring review_for Semester II of Year 4than sare
 
Session And Cookies In Servlets - Java
Session And Cookies In Servlets - JavaSession And Cookies In Servlets - Java
Session And Cookies In Servlets - JavaJainamParikh3
 
Mule ESB Interview or Certification questions
Mule ESB Interview or Certification questionsMule ESB Interview or Certification questions
Mule ESB Interview or Certification questionsTechieVarsity
 
40+ tips to use Postman more efficiently
40+ tips to use Postman more efficiently40+ tips to use Postman more efficiently
40+ tips to use Postman more efficientlypostmanclient
 

What's hot (20)

.NET Core, ASP.NET Core Course, Session 19
 .NET Core, ASP.NET Core Course, Session 19 .NET Core, ASP.NET Core Course, Session 19
.NET Core, ASP.NET Core Course, Session 19
 
Share point review qustions
Share point review qustionsShare point review qustions
Share point review qustions
 
ASP.NET WEB API Training
ASP.NET WEB API TrainingASP.NET WEB API Training
ASP.NET WEB API Training
 
Create Home Directories on Storage Using WFA and ServiceNow integration
Create Home Directories on Storage Using WFA and ServiceNow integrationCreate Home Directories on Storage Using WFA and ServiceNow integration
Create Home Directories on Storage Using WFA and ServiceNow integration
 
Http and REST APIs.
Http and REST APIs.Http and REST APIs.
Http and REST APIs.
 
Rest web service
Rest web serviceRest web service
Rest web service
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web api
 
Web services - REST and SOAP
Web services - REST and SOAPWeb services - REST and SOAP
Web services - REST and SOAP
 
.NET Core, ASP.NET Core Course, Session 7
.NET Core, ASP.NET Core Course, Session 7.NET Core, ASP.NET Core Course, Session 7
.NET Core, ASP.NET Core Course, Session 7
 
Java part 3
Java part  3Java part  3
Java part 3
 
Marata
MarataMarata
Marata
 
Enterprise Software Architecture
Enterprise Software ArchitectureEnterprise Software Architecture
Enterprise Software Architecture
 
How to call REST API without knowing any programming languages
How to call REST API without knowing any programming languages How to call REST API without knowing any programming languages
How to call REST API without knowing any programming languages
 
Spring review_for Semester II of Year 4
Spring review_for Semester II of Year 4Spring review_for Semester II of Year 4
Spring review_for Semester II of Year 4
 
Session And Cookies In Servlets - Java
Session And Cookies In Servlets - JavaSession And Cookies In Servlets - Java
Session And Cookies In Servlets - Java
 
Apache error
Apache errorApache error
Apache error
 
Mule ESB Interview or Certification questions
Mule ESB Interview or Certification questionsMule ESB Interview or Certification questions
Mule ESB Interview or Certification questions
 
40+ tips to use Postman more efficiently
40+ tips to use Postman more efficiently40+ tips to use Postman more efficiently
40+ tips to use Postman more efficiently
 
Advanced Java
Advanced JavaAdvanced Java
Advanced Java
 
ASP.NET WEB API
ASP.NET WEB APIASP.NET WEB API
ASP.NET WEB API
 

Similar to Intro to flask2

Web Server and how we can design app in C#
Web Server and how we can design app  in C#Web Server and how we can design app  in C#
Web Server and how we can design app in C#caohansnnuedu
 
Networked APIs with swift
Networked APIs with swiftNetworked APIs with swift
Networked APIs with swiftTim Burks
 
Xamarin Workshop Noob to Master – Week 5
Xamarin Workshop Noob to Master – Week 5Xamarin Workshop Noob to Master – Week 5
Xamarin Workshop Noob to Master – Week 5Charlin Agramonte
 
Using Ajax In Domino Web Applications
Using Ajax In Domino Web ApplicationsUsing Ajax In Domino Web Applications
Using Ajax In Domino Web Applicationsdominion
 
Asynchronous reading and writing http r equest
Asynchronous reading and writing http r equestAsynchronous reading and writing http r equest
Asynchronous reading and writing http r equestPragyanshis Patnaik
 
Distributed System by Pratik Tambekar
Distributed System by Pratik TambekarDistributed System by Pratik Tambekar
Distributed System by Pratik TambekarPratik Tambekar
 
Xml web services
Xml web servicesXml web services
Xml web servicesRaghu nath
 
Drupal 8 meets to symphony
Drupal 8 meets to symphonyDrupal 8 meets to symphony
Drupal 8 meets to symphonyBrahampal Singh
 
Server Side Programming
Server Side ProgrammingServer Side Programming
Server Side ProgrammingMilan Thapa
 
Lightning web components
Lightning web components Lightning web components
Lightning web components Cloud Analogy
 
SynapseIndia dotnet development ajax client library
SynapseIndia dotnet development ajax client librarySynapseIndia dotnet development ajax client library
SynapseIndia dotnet development ajax client librarySynapseindiappsdevelopment
 
Web container and Apache Tomcat
Web container and Apache TomcatWeb container and Apache Tomcat
Web container and Apache TomcatAuwal Amshi
 

Similar to Intro to flask2 (20)

Web Server and how we can design app in C#
Web Server and how we can design app  in C#Web Server and how we can design app  in C#
Web Server and how we can design app in C#
 
Networked APIs with swift
Networked APIs with swiftNetworked APIs with swift
Networked APIs with swift
 
Xamarin Workshop Noob to Master – Week 5
Xamarin Workshop Noob to Master – Week 5Xamarin Workshop Noob to Master – Week 5
Xamarin Workshop Noob to Master – Week 5
 
Using Ajax In Domino Web Applications
Using Ajax In Domino Web ApplicationsUsing Ajax In Domino Web Applications
Using Ajax In Domino Web Applications
 
Asynchronous reading and writing http r equest
Asynchronous reading and writing http r equestAsynchronous reading and writing http r equest
Asynchronous reading and writing http r equest
 
Distributed System by Pratik Tambekar
Distributed System by Pratik TambekarDistributed System by Pratik Tambekar
Distributed System by Pratik Tambekar
 
Xml web services
Xml web servicesXml web services
Xml web services
 
Web Service
Web ServiceWeb Service
Web Service
 
Drupal 8 meets to symphony
Drupal 8 meets to symphonyDrupal 8 meets to symphony
Drupal 8 meets to symphony
 
Server Side Programming
Server Side ProgrammingServer Side Programming
Server Side Programming
 
Flask
FlaskFlask
Flask
 
Ds
DsDs
Ds
 
Lightning web components
Lightning web components Lightning web components
Lightning web components
 
25250716 seminar-on-ajax text
25250716 seminar-on-ajax text25250716 seminar-on-ajax text
25250716 seminar-on-ajax text
 
sveltekit-en.pdf
sveltekit-en.pdfsveltekit-en.pdf
sveltekit-en.pdf
 
SynapseIndia dotnet development ajax client library
SynapseIndia dotnet development ajax client librarySynapseIndia dotnet development ajax client library
SynapseIndia dotnet development ajax client library
 
Adobe Flex4
Adobe Flex4 Adobe Flex4
Adobe Flex4
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
SCDJWS 6. REST JAX-P
SCDJWS 6. REST  JAX-PSCDJWS 6. REST  JAX-P
SCDJWS 6. REST JAX-P
 
Web container and Apache Tomcat
Web container and Apache TomcatWeb container and Apache Tomcat
Web container and Apache Tomcat
 

More from Mohamed Essam

Data Science Crash course
Data Science Crash courseData Science Crash course
Data Science Crash courseMohamed Essam
 
2.Feature Extraction
2.Feature Extraction2.Feature Extraction
2.Feature ExtractionMohamed Essam
 
Introduction to Robotics.pptx
Introduction to Robotics.pptxIntroduction to Robotics.pptx
Introduction to Robotics.pptxMohamed Essam
 
Introduction_to_Gui_with_tkinter.pptx
Introduction_to_Gui_with_tkinter.pptxIntroduction_to_Gui_with_tkinter.pptx
Introduction_to_Gui_with_tkinter.pptxMohamed Essam
 
Getting_Started_with_DL_in_Keras.pptx
Getting_Started_with_DL_in_Keras.pptxGetting_Started_with_DL_in_Keras.pptx
Getting_Started_with_DL_in_Keras.pptxMohamed Essam
 
Let_s_Dive_to_Deep_Learning.pptx
Let_s_Dive_to_Deep_Learning.pptxLet_s_Dive_to_Deep_Learning.pptx
Let_s_Dive_to_Deep_Learning.pptxMohamed Essam
 
OOP-Advanced_Programming.pptx
OOP-Advanced_Programming.pptxOOP-Advanced_Programming.pptx
OOP-Advanced_Programming.pptxMohamed Essam
 
Regularization_BY_MOHAMED_ESSAM.pptx
Regularization_BY_MOHAMED_ESSAM.pptxRegularization_BY_MOHAMED_ESSAM.pptx
Regularization_BY_MOHAMED_ESSAM.pptxMohamed Essam
 
1.What_if_Adham_Nour_tried_to_make_a_Machine_Learning_Model_at_Home.pptx
1.What_if_Adham_Nour_tried_to_make_a_Machine_Learning_Model_at_Home.pptx1.What_if_Adham_Nour_tried_to_make_a_Machine_Learning_Model_at_Home.pptx
1.What_if_Adham_Nour_tried_to_make_a_Machine_Learning_Model_at_Home.pptxMohamed Essam
 
2.Data_Strucures_and_modules.pptx
2.Data_Strucures_and_modules.pptx2.Data_Strucures_and_modules.pptx
2.Data_Strucures_and_modules.pptxMohamed Essam
 
Activation_function.pptx
Activation_function.pptxActivation_function.pptx
Activation_function.pptxMohamed Essam
 
Deep_Learning_Frameworks
Deep_Learning_FrameworksDeep_Learning_Frameworks
Deep_Learning_FrameworksMohamed Essam
 

More from Mohamed Essam (20)

Data Science Crash course
Data Science Crash courseData Science Crash course
Data Science Crash course
 
2.Feature Extraction
2.Feature Extraction2.Feature Extraction
2.Feature Extraction
 
Data Science
Data ScienceData Science
Data Science
 
Introduction to Robotics.pptx
Introduction to Robotics.pptxIntroduction to Robotics.pptx
Introduction to Robotics.pptx
 
Introduction_to_Gui_with_tkinter.pptx
Introduction_to_Gui_with_tkinter.pptxIntroduction_to_Gui_with_tkinter.pptx
Introduction_to_Gui_with_tkinter.pptx
 
Getting_Started_with_DL_in_Keras.pptx
Getting_Started_with_DL_in_Keras.pptxGetting_Started_with_DL_in_Keras.pptx
Getting_Started_with_DL_in_Keras.pptx
 
Linear_algebra.pptx
Linear_algebra.pptxLinear_algebra.pptx
Linear_algebra.pptx
 
Let_s_Dive_to_Deep_Learning.pptx
Let_s_Dive_to_Deep_Learning.pptxLet_s_Dive_to_Deep_Learning.pptx
Let_s_Dive_to_Deep_Learning.pptx
 
OOP-Advanced_Programming.pptx
OOP-Advanced_Programming.pptxOOP-Advanced_Programming.pptx
OOP-Advanced_Programming.pptx
 
1.Basic_Syntax
1.Basic_Syntax1.Basic_Syntax
1.Basic_Syntax
 
KNN.pptx
KNN.pptxKNN.pptx
KNN.pptx
 
Regularization_BY_MOHAMED_ESSAM.pptx
Regularization_BY_MOHAMED_ESSAM.pptxRegularization_BY_MOHAMED_ESSAM.pptx
Regularization_BY_MOHAMED_ESSAM.pptx
 
1.What_if_Adham_Nour_tried_to_make_a_Machine_Learning_Model_at_Home.pptx
1.What_if_Adham_Nour_tried_to_make_a_Machine_Learning_Model_at_Home.pptx1.What_if_Adham_Nour_tried_to_make_a_Machine_Learning_Model_at_Home.pptx
1.What_if_Adham_Nour_tried_to_make_a_Machine_Learning_Model_at_Home.pptx
 
Clean_Code
Clean_CodeClean_Code
Clean_Code
 
Linear_Regression
Linear_RegressionLinear_Regression
Linear_Regression
 
2.Data_Strucures_and_modules.pptx
2.Data_Strucures_and_modules.pptx2.Data_Strucures_and_modules.pptx
2.Data_Strucures_and_modules.pptx
 
Naieve_Bayee.pptx
Naieve_Bayee.pptxNaieve_Bayee.pptx
Naieve_Bayee.pptx
 
Activation_function.pptx
Activation_function.pptxActivation_function.pptx
Activation_function.pptx
 
Deep_Learning_Frameworks
Deep_Learning_FrameworksDeep_Learning_Frameworks
Deep_Learning_Frameworks
 
Neural_Network
Neural_NetworkNeural_Network
Neural_Network
 

Recently uploaded

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 

Recently uploaded (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 

Intro to flask2

  • 2. Flask What Flask is ?  Flask is a small framework by most standards, small enough to be called a “microframework.” But being small does not mean that it does less than other frameworks. Flask was designed as an extensible framework from the ground up; it provides a solid core with the basic services, while extensions provide the rest
  • 4. Flask What Flask is ?  Flask is a web application framework written in Python. It is developed by Armin Ronacher, who leads an international group of Python enthusiasts named Pocco.  Flask is based on the Werkzeug WSGI toolkit and Jinja2 template engine. Both are Pocco projects.  WSGI is a specification for a universal interface between the web server and the web applications, It is a WSGI toolkit, which implements requests, response objects, and other utility functions. This enables building a web framework on top of it.  The Flask framework uses Werkzeug as one of its bases.  Jinja2 is a popular templating engine for Python. A web templating system combines a template with a certain data source to render dynamic web pages.
  • 5. Flask What Flask is ?  Flask also implements a particular design pattern, or way that our program and code is organized. For Flask, the design pattern is generally MVC, or Model–view–controller:
  • 6. Flask What Flask is ?  Flask also implements a particular design pattern, or way that our program and code is organized. For Flask, the design pattern is generally MVC, or Model–view–controller:  The controller is our logic and code that manages our application overall, given user input. In Flask, this will be our Python code. The view is the user interface, like the HTML and CSS that the user will see and interact with. The model is our application’s data, such as a SQL database or CSV file.
  • 7. Flask Install Flask in this environment  Most Python packages are installed with the pip utility, which virtualenv automatically adds to all virtual environments upon creation. When a virtual environment is activated, the location of the pip utility is added to the PATH.
  • 8. Flask Install Flask in this environment  All Flask applications must create an application instance. The web server passes all requests it receives from clients to this object for handling, using a protocol called Web Server Gateway Interface (WSGI). The application instance is an object of class Flask, usually created as follows:  The only required argument to the Flask class constructor is the name of the main module or package of the application. For most applications, Python’s __name__ variable is the correct value.
  • 9. Flask Install Flask in this environment  All Flask applications must create an application instance. The web server passes all requests it receives from clients to this object for handling, using a protocol called Web Server Gateway Interface (WSGI). The application instance is an object of class Flask, usually created as follows:  The only required argument to the Flask class constructor is the name of the main module or package of the application. For most applications, Python’s __name__ variable is the correct value.
  • 10. Flask Install Flask in this environment  All Flask applications must create an application instance. The web server passes all requests it receives from clients to this object for handling, using a protocol called Web Server Gateway Interface (WSGI). The application instance is an object of class Flask, usually created as follows:  The only required argument to the Flask class constructor is the name of the main module or package of the application. For most applications, Python’s __name__ variable is the correct value.
  • 11. Flask Routes and View Functions  Clients such as web browsers send requests to the web server, which in turn sends them to the Flask application instance. The application instance needs to know what code needs to run for each URL requested, so it keeps a mapping of URLs to Python functions. The association between a URL and the function that handles it is called a route.  The most convenient way to define a route in a Flask application is through the app.route decorator exposed by the application instance, which registers the decorated function as a route. The following example shows how a route is declared using this decorator:
  • 12. Flask Routes and View Functions  The portion enclosed in angle brackets is the dynamic part, so any URLs that match the static portions will be mapped to this route. When the view function is invoked, Flask sends the dynamic component as an argument. In the earlier example view function, this argument is used to generate a personalized greeting as a response.  The dynamic components in routes are strings by default but can also be defined with a type. For example, route /user/ would match only URLs that have an integer in the id dynamic segment. Flask supports types int, float, and path for routes. The path type also represents a string but does not consider slashes as separators and instead considers them part of the dynamic component.
  • 13. Flask Server Startup  The __name__ == '__main__' Python idiom is used here to ensure that the development web server is started only when the script is executed directly. When the script is imported by another script, it is assumed that the parent script will launch a different server, so the app.run() call is skipped.  Once the server starts up, it goes into a loop that waits for requests and services them. This loop continues until the application is stopped, for example by hitting Ctrl-C. There are several option arguments that can be given to app.run() to configure the mode of operation of the web server. During development, it is convenient to enable debug mode, which among other things activates the debugger and the reloader. This is done by passing the argument debug set to True
  • 14. Flask Responses  When Flask invokes a view function, it expects its return value to be the response to the request. In most cases the response is a simple string that is sent back to the client as an HTML page.  But the HTTP protocol requires more than a string as a response to a request. A very important part of the HTTP response is the status code, which Flask by default sets to 200, the code that indicates that the request was carried out successfully.  When a view function needs to respond with a different status code, it can add the numeric code as a second return value after the response text. For example, the following view function returns a 400 status code, the code for a bad request error:
  • 15. Flask Responses-HTTP HTTP (Hypertext Transfer Protocol) is the system the internet uses to interact and communicate between computers and servers. When a URL is entered into a browser, an HTTP request is sent to a server, which interprets the request and sends appropriate HTTP response, which, if all goes as expected, contains the requested information to be displayed by the web browser.
  • 16. Flask Responses  Flask view functions have the option of returning a Response object. The make_response() function takes one, two, or three arguments, the same values that can be returned from a view function, and returns a Response object. Sometimes it is useful to perform this conversion inside the.
  • 17. Flask Responses  The request cookie is what is send from the client to the server (thus what the browser provides). The response cookie are the cookies that you want to place in the browser. The next connection from the browser that accepted the cookie from the response object will provide the cookie in the request object.
  • 18. Flask Responses  There is a special type of response called a redirect. This response does not include a page document; it just gives the browser a new URL from which to load a new page. Redirects are commonly used with web forms.  A redirect is typically indicated with a 302 response status code and the URL to redirect to given in a Location header.
  • 19. Flask Host command  The --host argument is a useful option because it tells the web server what network interface to listen to for connections from clients. By default, Flask’s development web server listens for connections on localhost, so only connections originating from within the computer running the server are accepted. The following command makes the web server listen for connections on the public network interface, enabling other computers in the network to connect as well
  • 20. Flask Templating Language  A template is a file that contains the text of a response, with placeholder variables for the dynamic parts that will be known only in the context of a request. The process that replaces the variables with actual values and returns a final response string is called rendering. For the task of rendering templates, Flask uses a powerful template engine called Jinja2.
  • 21. Flask Rendering Templates  By default Flask looks for templates in a templates subfolder located inside the applica‐ tion folder. For the next version of hello.py, you need to store the templates defined earlier in a new templates folder as index.html and user.html
  • 22. Flask Rendering Templates- Variables  The {{ name }} construct used in the template references a variable, a special placeholder that tells the template engine that the value that goes in that place should be obtained from data provided at the time the template is rendered. Jinja2 recognizes variables of any type, even complex types such as lists, dictionaries and objects. The following are some more examples of variables used in templates:
  • 23. Flask Rendering Templates- Control Structures  Jinja2 offers several control structures that can be used to alter the flow of the template. This section introduces some of the most useful ones with simple examples. The following example shows how conditional statements can be entered in a template
  • 24. Flask Rendering Templates- Control Structures  Another common need in templates is to render a list of elements. This example shows how this can be done with a for loop:
  • 25. Flask Rendering Templates- Control Structures  Another common need in templates is to render a list of elements. This example shows how this can be done with a for loop:
  • 26. Flask Rendering Templates- Control Structures  Another common need in templates is to render a list of elements. This example shows how this can be done with a for loop:
  • 27. Flask Rendering Templates- Control Structures  Another common need in templates is to render a list of elements. This example shows how this can be done with a for loop:
  • 28. Flask Static Files  Web applications are not made of Python code and templates alone. Most applications also use static files such as images, JavaScript source files, and CSS that are referenced from the HTML code. You may recall that when the hello.py application’s a static entry appeared in it. This is so because references to static files are treated as a special route defined as /static/. For example, a call to url_for('static', filename='css/styles.css', _external=True) would return http://localhost:5000/static/css/styles.css.
  • 29. Flask Static Files  Web applications are not made of Python code and templates alone. Most applications also use static files such as images, JavaScript source files, and CSS that are referenced from the HTML code. You may recall that when the hello.py application’s a static entry appeared in it. This is so because references to static files are treated as a special route defined as /static/. For example, a call to url_for('static', filename='css/styles.css', _external=True) would return http://localhost:5000/static/css/styles.css.
  • 30. Flask Static Files  Web applications are not made of Python code and templates alone. Most applications also use static files such as images, JavaScript source files, and CSS that are referenced from the HTML code. You may recall that when the hello.py application’s a static entry appeared in it. This is so because references to static files are treated as a special route defined as /static/. For example, a call to url_for('static', filename='css/styles.css', _external=True) would return http://localhost:5000/static/css/styles.css.
  • 32. Flask Rendering Templates- Inhertance  Portions of template code that need to be repeated in several places can be stored in a separate file and included from all the templates to avoid repetition:
  • 36. Flask Request object In the client-server architecture, the request object contains all the data that is sent from the client to the server. As we have already discussed in the tutorial, we can retrieve the data at the server side using the HTTP methods.
  • 37. Flask Request object In the client-server architecture, the request object contains all the data that is sent from the client to the server. As we have already discussed in the tutorial, we can retrieve the data at the server side using the HTTP methods.