SlideShare a Scribd company logo
1 of 38
Download to read offline
Introduction to 
Flask Micro Framework 
Mohammad Reza Kamalifard 
@itmard
Codes 
https://github.com/itmard/zconf_flask_intro
What is a Web Application Framework?
What is a Web Application Framework? 
• URL Routing
What is a Web Application Framework? 
• URL Routing 
• Request and Response Objects
What is a Web Application Framework? 
• URL Routing 
• Request and Response Objects 
• Template Engine
What is a Web Application Framework? 
• URL Routing 
• Request and Response Objects 
• Template Engine 
• Development Web Server
What is a Web Application Framework? 
• URL Routing 
• Request and Response Objects 
• Template Engine 
• Development Web Server 
• Database Object Relational Mapper
What is a Web Application Framework? 
• URL Routing 
• Request and Response Objects 
• Template Engine 
• Development Web Server 
• Database Object Relational Mapper 
• Form Validation
Python for web development
Why Python? 
• Python is an easy to learn 
• Powerful 
• Clean syntax and code readability 
• Open Source 
• Cross-platform 
• Rich set of libraries 
• Large Number of open source tools
Python web frameworks 
• Django 
• Flask 
• Werkzeug 
• Tornado 
• Pyramid 
• Bottle 
•
Flask 
• Micro Framework
Flask 
• Micro Framework 
• Simple but extensible core
Flask 
• Micro Framework 
• Simple but extensible core 
• Werkzeug WSGI toolkit
Flask 
• Micro Framework 
• Simple but extensible core 
• Werkzeug WSGI toolkit 
• Jinja2 template engine
Flask 
• Micro Framework 
• Simple but extensible core 
• Werkzeug WSGI toolkit 
• Jinja2 template engine 
• Provides a simple template for web development
Flask 
• Micro Framework 
• Simple but extensible core 
• Werkzeug WSGI toolkit 
• Jinja2 template engine 
• Provides a simple template for web development 
• No ORM
Flask 
• Micro Framework 
• Simple but extensible core 
• Werkzeug WSGI toolkit 
• Jinja2 template engine 
• Provides a simple template for web development 
• No ORM 
• No Form validation
Flask 
• Micro Framework 
• Simple but extensible core 
• Werkzeug WSGI toolkit 
• Jinja2 template engine 
• Provides a simple template for web development 
• No ORM 
• No Form validation 
• Supports extensions
Simple Web Application with Flask 
from flask import Flask 
app = Flask(__name__) 
@app.route("/") 
def hello(): 
return "Welcome to ZCONF 5" 
if __name__ == "__main__": 
app.run()
Simple Web Application with Flask 
from flask import Flask 
app = Flask(__name__) 
@app.route("/") 
def hello(): 
return "Welcome to ZCONF 5" 
if __name__ == "__main__": 
app.run()
Features 
• Built in development server and debugger 
• Integrated unit testing support 
• RESTful request dispatching 
• Uses Jinja2 templating 
• Support for secure cookies (client side sessions) 
• 100% WSGI 1.0 compliant 
• Unicode based 
• Extensively documented
Flask Routing 
• Modern web applications have beautiful URLs 
• Routing using decorators 
• route( ) decorator is used to bind a function to a URL 
• Make certain parts of the URL dynamic 
• Attach multiple rules to a function
Flask Routing 
• Modern web applications have beautiful URLs 
• Routing using decorators 
• route( ) decorator is used to bind a function to a URL 
• Make certain parts of the URL dynamic 
• Attach multiple rules to a function 
@app.route('/') 
def index(): 
return 'Index Page' 
@app.route('/hello/') 
def hello(): 
return 'Hello World'
Variable Rules 
• <variable_name> 
@app.route('/user/<username>') 
def show_user_profile(username): 
return 'User %s' % username 
• <converter:variable_name> 
@app.route('/post/<int:post_id>') 
def show_post(post_id): 
return 'Post %d' % post_id
Request Object 
• Easy way to access request data 
@app.route('/login', methods=['POST', 'GET']) 
def login(): 
error = None 
if request.method == 'POST': 
if valid_login(request.form['username'],request.form['password']): 
return log_the_user_in(request.form['username']) 
else: 
error = 'Invalid username/password' 
return render_template(' login.html ', error=error)
Template Engine 
• Jinja2 (default) 
from flask import render_template 
@app.route(' /hello/ ') 
@app.route(' /hello/<name> ') 
def hello(name=None): 
return render_template(' hello.html', name=name)
hello.html 
<!doctype html> 
<title>Hello from Flask</title> 
{% if name %} 
<h1>Hello {{ name }}!</h1> 
{% else %} 
<h1>Hello World!</h1> 
{% endif %}
Development server and debugger 
• Easy to use local development server 
• Interactive Error page with debug mode
Database management 
• No ORM inside flask 
• Extensions: 
• SQLAlchemy 
• Peewee 
• Mongoengine 
• MongoKIT 
• PyMongo 
• MongoAlchemy 
• Flask-CouchDB 
• Flask-FluidDB 
• Flask-CouchDBKit 
• Flask-FluidDB
#sqlalchemy 
from flask import Flask 
from flask.ext.sqlalchemy import SQLAlchemy 
app = Flask(__name__) 
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' 
db = SQLAlchemy(app) 
class User(db.Model): 
id = db.Column(db.Integer, primary_key=True) 
username = db.Column(db.String(80), unique=True) 
email = db.Column(db.String(120), unique=True) 
#peewee 
import datetime 
from peewee import * 
class Note(db.Model): 
message = TextField() 
created = DateTimeField(default=datetime.datetime.now)
Form validation 
• Flask WTForm 
#class based forms 
from flask_wtf import Form 
from wtforms import StringField 
from wtforms.validators import DataRequired 
class MyForm(Form): 
name = StringField('name', validators=[DataRequired()]) 
#view 
@app.route('/submit', methods=('GET', 'POST')) 
def submit(): 
form = MyForm() 
if form.validate_on_submit(): 
return redirect('/success') 
return render_template('submit.html', form=form)
Architectural Pattern 
• Single file application 
• MVC , MV* patterns 
project 
--apps 
----------app1 
----------------models.py 
----------------views.py 
---------------forms.py 
---------------apis.py 
---statics 
---templates 
-----models 
-----views 
-----forms 
-----statics 
-----templates
Mohammad Efazati 
Mehdi Khoshnody 
Reza Shalbafzadeh 
K1 Hedayati 
Thank you :)
Copyright 2014 Mohammad Reza Kamalifard 
All rights reserved. 
Introduction to Flask by Mohammad Reza Kamalifard is licensed under a 
Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International 
License. 
ZCONF 5

More Related Content

What's hot

An Introduction to Tornado
An Introduction to TornadoAn Introduction to Tornado
An Introduction to Tornado
Gavin Roy
 

What's hot (20)

Rest api with Python
Rest api with PythonRest api with Python
Rest api with Python
 
BUILDING MODERN PYTHON WEB FRAMEWORKS USING FLASK WITH NEIL GREY
BUILDING MODERN PYTHON WEB FRAMEWORKS USING FLASK WITH NEIL GREYBUILDING MODERN PYTHON WEB FRAMEWORKS USING FLASK WITH NEIL GREY
BUILDING MODERN PYTHON WEB FRAMEWORKS USING FLASK WITH NEIL GREY
 
Flask restfulservices
Flask restfulservicesFlask restfulservices
Flask restfulservices
 
8 Minutes On Rack
8 Minutes On Rack8 Minutes On Rack
8 Minutes On Rack
 
Node.js API 서버 성능 개선기
Node.js API 서버 성능 개선기Node.js API 서버 성능 개선기
Node.js API 서버 성능 개선기
 
Beyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the codeBeyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the code
 
Real time server
Real time serverReal time server
Real time server
 
When dynamic becomes static: the next step in web caching techniques
When dynamic becomes static: the next step in web caching techniquesWhen dynamic becomes static: the next step in web caching techniques
When dynamic becomes static: the next step in web caching techniques
 
An Introduction to Tornado
An Introduction to TornadoAn Introduction to Tornado
An Introduction to Tornado
 
Rack Middleware
Rack MiddlewareRack Middleware
Rack Middleware
 
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
 
Bootstrapping multidc observability stack
Bootstrapping multidc observability stackBootstrapping multidc observability stack
Bootstrapping multidc observability stack
 
Securing Prometheus exporters using HashiCorp Vault
Securing Prometheus exporters using HashiCorp VaultSecuring Prometheus exporters using HashiCorp Vault
Securing Prometheus exporters using HashiCorp Vault
 
Beyond Phoenix
Beyond PhoenixBeyond Phoenix
Beyond Phoenix
 
Why and How Powershell will rule the Command Line - Barcamp LA 4
Why and How Powershell will rule the Command Line - Barcamp LA 4Why and How Powershell will rule the Command Line - Barcamp LA 4
Why and How Powershell will rule the Command Line - Barcamp LA 4
 
What happens in laravel 4 bootstraping
What happens in laravel 4 bootstrapingWhat happens in laravel 4 bootstraping
What happens in laravel 4 bootstraping
 
Asynchronous web apps with the Play Framework 2.0
Asynchronous web apps with the Play Framework 2.0Asynchronous web apps with the Play Framework 2.0
Asynchronous web apps with the Play Framework 2.0
 
Building a real life application in node js
Building a real life application in node jsBuilding a real life application in node js
Building a real life application in node js
 
Powershell Demo Presentation
Powershell Demo PresentationPowershell Demo Presentation
Powershell Demo Presentation
 
Roll Your Own API Management Platform with nginx and Lua
Roll Your Own API Management Platform with nginx and LuaRoll Your Own API Management Platform with nginx and Lua
Roll Your Own API Management Platform with nginx and Lua
 

Similar to Introduction to Flask Micro Framework

Tech io spa_angularjs_20130814_v0.9.5
Tech io spa_angularjs_20130814_v0.9.5Tech io spa_angularjs_20130814_v0.9.5
Tech io spa_angularjs_20130814_v0.9.5
Ganesh Kondal
 
Microsoft Power Page for Developer - ภาษาไทย
Microsoft Power Page for Developer - ภาษาไทยMicrosoft Power Page for Developer - ภาษาไทย
Microsoft Power Page for Developer - ภาษาไทย
Teerasej Jiraphatchandej
 
Web Application Frameworks (WAF)
Web Application Frameworks (WAF)Web Application Frameworks (WAF)
Web Application Frameworks (WAF)
Ako Kaman
 
Intro to node and mongodb 1
Intro to node and mongodb   1Intro to node and mongodb   1
Intro to node and mongodb 1
Mohammad Qureshi
 

Similar to Introduction to Flask Micro Framework (20)

DEF CON 24 - workshop - Craig Young - brainwashing embedded systems
DEF CON 24 - workshop - Craig Young - brainwashing embedded systemsDEF CON 24 - workshop - Craig Young - brainwashing embedded systems
DEF CON 24 - workshop - Craig Young - brainwashing embedded systems
 
Tech io spa_angularjs_20130814_v0.9.5
Tech io spa_angularjs_20130814_v0.9.5Tech io spa_angularjs_20130814_v0.9.5
Tech io spa_angularjs_20130814_v0.9.5
 
web2py:Web development like a boss
web2py:Web development like a bossweb2py:Web development like a boss
web2py:Web development like a boss
 
How to JavaOne 2016 - Generate Customized Java 8 Code from Your Database [TUT...
How to JavaOne 2016 - Generate Customized Java 8 Code from Your Database [TUT...How to JavaOne 2016 - Generate Customized Java 8 Code from Your Database [TUT...
How to JavaOne 2016 - Generate Customized Java 8 Code from Your Database [TUT...
 
JavaOne2016 - How to Generate Customized Java 8 Code from Your Database [TUT4...
JavaOne2016 - How to Generate Customized Java 8 Code from Your Database [TUT4...JavaOne2016 - How to Generate Customized Java 8 Code from Your Database [TUT4...
JavaOne2016 - How to Generate Customized Java 8 Code from Your Database [TUT4...
 
Next Generation Spring MVC with Spring Roo
Next Generation Spring MVC with Spring RooNext Generation Spring MVC with Spring Roo
Next Generation Spring MVC with Spring Roo
 
Rhodes
RhodesRhodes
Rhodes
 
Node.js to the rescue
Node.js to the rescueNode.js to the rescue
Node.js to the rescue
 
Microsoft Power Page for Developer - ภาษาไทย
Microsoft Power Page for Developer - ภาษาไทยMicrosoft Power Page for Developer - ภาษาไทย
Microsoft Power Page for Developer - ภาษาไทย
 
Integrating Alfresco with Portals
Integrating Alfresco with PortalsIntegrating Alfresco with Portals
Integrating Alfresco with Portals
 
CFWheels - Pragmatic, Beautiful Code
CFWheels - Pragmatic, Beautiful CodeCFWheels - Pragmatic, Beautiful Code
CFWheels - Pragmatic, Beautiful Code
 
Create Rest API in Nodejs
Create Rest API in Nodejs Create Rest API in Nodejs
Create Rest API in Nodejs
 
SOLID Programming with Portable Class Libraries
SOLID Programming with Portable Class LibrariesSOLID Programming with Portable Class Libraries
SOLID Programming with Portable Class Libraries
 
Structured Functional Automated Web Service Testing
Structured Functional Automated Web Service TestingStructured Functional Automated Web Service Testing
Structured Functional Automated Web Service Testing
 
Web Application Frameworks (WAF)
Web Application Frameworks (WAF)Web Application Frameworks (WAF)
Web Application Frameworks (WAF)
 
PLAT-7 Spring Web Scripts and Spring Surf
PLAT-7 Spring Web Scripts and Spring SurfPLAT-7 Spring Web Scripts and Spring Surf
PLAT-7 Spring Web Scripts and Spring Surf
 
PLAT-7 Spring Web Scripts and Spring Surf
PLAT-7 Spring Web Scripts and Spring SurfPLAT-7 Spring Web Scripts and Spring Surf
PLAT-7 Spring Web Scripts and Spring Surf
 
Groovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentationGroovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentation
 
Intro to node and mongodb 1
Intro to node and mongodb   1Intro to node and mongodb   1
Intro to node and mongodb 1
 
ITB2017 - Keynote
ITB2017 - KeynoteITB2017 - Keynote
ITB2017 - Keynote
 

More from Mohammad Reza Kamalifard

اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
Mohammad Reza Kamalifard
 
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونیاسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
Mohammad Reza Kamalifard
 

More from Mohammad Reza Kamalifard (20)

Internet Age
Internet AgeInternet Age
Internet Age
 
Tehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacy
Tehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacyTehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacy
Tehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacy
 
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
 
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
 
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
 
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
 
اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی
اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونیاسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی
اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی
 
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
 
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
 
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
 
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونیاسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

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
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

Introduction to Flask Micro Framework

  • 1. Introduction to Flask Micro Framework Mohammad Reza Kamalifard @itmard
  • 3. What is a Web Application Framework?
  • 4. What is a Web Application Framework? • URL Routing
  • 5. What is a Web Application Framework? • URL Routing • Request and Response Objects
  • 6. What is a Web Application Framework? • URL Routing • Request and Response Objects • Template Engine
  • 7. What is a Web Application Framework? • URL Routing • Request and Response Objects • Template Engine • Development Web Server
  • 8. What is a Web Application Framework? • URL Routing • Request and Response Objects • Template Engine • Development Web Server • Database Object Relational Mapper
  • 9. What is a Web Application Framework? • URL Routing • Request and Response Objects • Template Engine • Development Web Server • Database Object Relational Mapper • Form Validation
  • 10. Python for web development
  • 11. Why Python? • Python is an easy to learn • Powerful • Clean syntax and code readability • Open Source • Cross-platform • Rich set of libraries • Large Number of open source tools
  • 12. Python web frameworks • Django • Flask • Werkzeug • Tornado • Pyramid • Bottle •
  • 13.
  • 14. Flask • Micro Framework
  • 15. Flask • Micro Framework • Simple but extensible core
  • 16. Flask • Micro Framework • Simple but extensible core • Werkzeug WSGI toolkit
  • 17. Flask • Micro Framework • Simple but extensible core • Werkzeug WSGI toolkit • Jinja2 template engine
  • 18. Flask • Micro Framework • Simple but extensible core • Werkzeug WSGI toolkit • Jinja2 template engine • Provides a simple template for web development
  • 19. Flask • Micro Framework • Simple but extensible core • Werkzeug WSGI toolkit • Jinja2 template engine • Provides a simple template for web development • No ORM
  • 20. Flask • Micro Framework • Simple but extensible core • Werkzeug WSGI toolkit • Jinja2 template engine • Provides a simple template for web development • No ORM • No Form validation
  • 21. Flask • Micro Framework • Simple but extensible core • Werkzeug WSGI toolkit • Jinja2 template engine • Provides a simple template for web development • No ORM • No Form validation • Supports extensions
  • 22. Simple Web Application with Flask from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Welcome to ZCONF 5" if __name__ == "__main__": app.run()
  • 23. Simple Web Application with Flask from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Welcome to ZCONF 5" if __name__ == "__main__": app.run()
  • 24. Features • Built in development server and debugger • Integrated unit testing support • RESTful request dispatching • Uses Jinja2 templating • Support for secure cookies (client side sessions) • 100% WSGI 1.0 compliant • Unicode based • Extensively documented
  • 25. Flask Routing • Modern web applications have beautiful URLs • Routing using decorators • route( ) decorator is used to bind a function to a URL • Make certain parts of the URL dynamic • Attach multiple rules to a function
  • 26. Flask Routing • Modern web applications have beautiful URLs • Routing using decorators • route( ) decorator is used to bind a function to a URL • Make certain parts of the URL dynamic • Attach multiple rules to a function @app.route('/') def index(): return 'Index Page' @app.route('/hello/') def hello(): return 'Hello World'
  • 27. Variable Rules • <variable_name> @app.route('/user/<username>') def show_user_profile(username): return 'User %s' % username • <converter:variable_name> @app.route('/post/<int:post_id>') def show_post(post_id): return 'Post %d' % post_id
  • 28. Request Object • Easy way to access request data @app.route('/login', methods=['POST', 'GET']) def login(): error = None if request.method == 'POST': if valid_login(request.form['username'],request.form['password']): return log_the_user_in(request.form['username']) else: error = 'Invalid username/password' return render_template(' login.html ', error=error)
  • 29. Template Engine • Jinja2 (default) from flask import render_template @app.route(' /hello/ ') @app.route(' /hello/<name> ') def hello(name=None): return render_template(' hello.html', name=name)
  • 30. hello.html <!doctype html> <title>Hello from Flask</title> {% if name %} <h1>Hello {{ name }}!</h1> {% else %} <h1>Hello World!</h1> {% endif %}
  • 31. Development server and debugger • Easy to use local development server • Interactive Error page with debug mode
  • 32.
  • 33. Database management • No ORM inside flask • Extensions: • SQLAlchemy • Peewee • Mongoengine • MongoKIT • PyMongo • MongoAlchemy • Flask-CouchDB • Flask-FluidDB • Flask-CouchDBKit • Flask-FluidDB
  • 34. #sqlalchemy from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' db = SQLAlchemy(app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) email = db.Column(db.String(120), unique=True) #peewee import datetime from peewee import * class Note(db.Model): message = TextField() created = DateTimeField(default=datetime.datetime.now)
  • 35. Form validation • Flask WTForm #class based forms from flask_wtf import Form from wtforms import StringField from wtforms.validators import DataRequired class MyForm(Form): name = StringField('name', validators=[DataRequired()]) #view @app.route('/submit', methods=('GET', 'POST')) def submit(): form = MyForm() if form.validate_on_submit(): return redirect('/success') return render_template('submit.html', form=form)
  • 36. Architectural Pattern • Single file application • MVC , MV* patterns project --apps ----------app1 ----------------models.py ----------------views.py ---------------forms.py ---------------apis.py ---statics ---templates -----models -----views -----forms -----statics -----templates
  • 37. Mohammad Efazati Mehdi Khoshnody Reza Shalbafzadeh K1 Hedayati Thank you :)
  • 38. Copyright 2014 Mohammad Reza Kamalifard All rights reserved. Introduction to Flask by Mohammad Reza Kamalifard is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. ZCONF 5