FLASK
FLASK
● It is easy to learn and use. Flask is “beginner-friendly” because it does not have
boilerplate code or dependencies, which can distract from the primary function of
an application.
Features of Flask
● Some features which make Flask an ideal framework for web application
development are:
○ Many extensions are available for Flask, which can be used to enhance its functionalities.
What is micro framework?
● Micro-frameworks are the opposite of full-stack frameworks, which also offer
● Such features are instead provided by special Python packages called Flask
extensions.
● The extensions integrate seamlessly with Flask so that they appear as if they were
part of Flask itself. For example, Flask doesn't provide a page template engine, but
health.
Who is using flask?
● MIT’s technology stack also
● Mozilla has used Flask in
python library.
● Then that library gets updated to a newer version, the update has new
dependencies!
environment.
Use the terminal or an Anaconda Prompt for the following steps:
● To create an environment:
● Conda may then ask you to proceed (y/n)? Go ahead and press y to
continue.
Virtual environment
● To create an environment with a specific package included:
● Click on that if you are not in the environment you have activated and select the
environment accordingly.
Selecting the environment in VS code
● You will see the following output and select the virtual environment (myflaskenv)
Selecting the environment in VS code
● After selecting, you will finally land in your virtual environment you have created
“hello world” site
● Let’s create our first website with Flask!
python filename
helloWorld.py
“hello world” site
● flask is package and Flask is class imported from flask package
● app.route(‘/’) : route to homepage by default or you can specify the page you want to
land to also
● __name__ == ‘__main__’ means if name variable is equal to main file(the current file
you are running) then run this application (app.run()) runs the file.
Route in flask
Basic Route
● We just saw how to create a web application with a single page
● The string parameter passed into the decorator determines the URL
http://127.0.0.1:5000/
@app.route(“/some_page”)
● http://127.0.0.1:5000/some_page
basicRoute.py
Dynamic Route
• Often we will want URL route extensions to be dynamic based on the situation.
• For example we may want a page per user, so that the extension is in the form:
• www.site.com/user/unique_user_name
● When you are ready for deployment, debug mode should be set to false.
● You will be creating a simple web page that converts puppy names into puppy latin (a
route:
● /puppy_latin/<name>
● Flask will automatically look for HTML Templates in the templates directory.
● Create static folder and store image file under this folder.
basic.html
● Create a python file which will render html file inside the templates folder
basic_template.py
Output
Template variables
● We want a way to be able to use Python code in our app, changing and updating variables
● Jinja templating will let us directly insert variables from our Python code to the HTML
file.
{{ some_variable}}
Template variables
● We can pass in python strings, lists, dictionaries, and more into the templates.
● We set parameters (of our choice) in the render_template function and then use
templates_variable.html
Output
Template control flow
● With Jinja templating we can pass variables using {{ variable }} syntax.
● We also have access to control flow syntax in our templates such as for loops and
if statements.
● But that still means we need to make an HTML template for every page.
● Usually pages across a web application already share a lot of features (e.g.
● We set up a base.html template file with the re-usable aspects of the site.
Derived
from Base
page
Home page
puppy.html
Embedding static Bokeh
plots in flask
bokeh.embed
● Provide functions for embedding Bokeh standalone and server content in web
pages.
● Component
Return HTML components to embed a Bokeh plot. The data for the plot is stored
code and CSS resources should be located, loaded, and embedded in Bokeh documents.
● js_files
● css_files
From basic_line.py,
import variables
which are used to
load Bokeh plot in
flask
index.html
main.css under
static/css folder
Running the file and output
python app.py
filename
Embedding interactive
Bokeh server plot in flask
bokeh.client
● pull_session: Create a session by loading the current server-side document.
● url=http://localhost:5006/bokeh_labelWidget
bokeh.embed
● server_session: Return a script tag that embeds content from a specific existing
● This function is typically only useful for serving from a specific session that was
widgets
index.html
Running the file
● You are running Bokeh server on port 5006 and flask on port 5000
● Because of some security feature issue, to embed Bokeh server plot in flask, you
need to run the following command to initialize Bokeh server plot
Filename of Bokeh
server plot
The command means giving permission
to url “127.0.0.1:5000” to run Bokeh server app
Running the file
● After you run the command, Bokeh server plot will be initialized and you need to
run your flask app
python app.py
filename
Output
any questions??
Find me at pemawangmo.gcit@rub.edu.bt