SlideShare a Scribd company logo
QuickFlask
Flasking all the things!
And then some
Created by /Justin Spain @jwsmusic
What is Flask?
A MicroFramework written in Python for the web
Micro meaning simple core but highly extensible
Used to create websites and api's
Other Python Frameworks
Django
Bottle
CherryPy
Pecan
Pyramid
web.py
...
Why Flask
Easy to learn
Small but can easily handle large apps
Awesome documentation
*
Written in Python
Routes by decorators
Import only what you need
Pythonic
New plugins every day
Flask-Migrate using Alembic
Vast array of extensions
Flask VS. Django
Django - batteries included:
Can be overkill for small projects
Built-in template engine, ORM, and Admin
Steeper learning curve
Flask - one drop at a time
Install what you need, when you need it
Can add complexity as needed
Install different template engine, ORM, and Admin easily
Easy to learn
My Development
Environment
Xubuntu 14.04
Python 2.7.6
Virtualenv - creates isolated Python environments
Pip - installs & manages Python packages
Project Structure
mkdirQuick-Flask
cdQuick-Flask
mkdirChadevPython
mkdirChadevPython/static
mkdirChadevPython/templates
touchREADME.md
touchChadevPython/app.py
//makeprojecteasilyexecutable
chmod+xChadevPython/app.py
Create and activate virtual
environment
virtualenvenv
.env/bin/activate
Do the Flask Install Dance!!!
pipinstallFlask
At terminal run project
./app.py
Open http://localhost:5000
Add code to app.py
#!/usr/bin/envpython
fromflaskimportFlask
app=Flask(__name__)
@app.route("/")
@app.route("/index")
defindex():
return"HelloChadevPythonGroup!"
if__name__=="__main__":
app.run()
Done
You can write Flask apps
now!
Add another route
@app.route("/python")
defpython():
return"Pythonallthethings!"
At terminal run project
./app.py
Open http://localhost:5000
Return simple html
@app.route("/flask")
defflask():
return"<h1>Flaskalltheweb!</h1>"
Jinja awesomness with
templates
Add code to base.html and index.html
//base.html
<divclass="container">
<h2>ChadevPythonusergroup!</h2>
<ahref="{{url_for('index')}}">Home|</a>
<ahref="{{url_for('python')}}">Python|</a>
<ahref="{{url_for('flask')}}">Flask|</a>
<ahref="{{url_for('extensions')}}">Extensions</a>
<br><br><br>
{%blockcontent%}{%endblock%}
<br><br><br><br>
<h2>LearningFlaskthings</h2>
</div>
Syntax for pulling in a variable from Flask
{{some_variable}}
Jinja awesomness with
templates
Extending base.html and putting content inside a block
//index.html
{%extends"base.html"%}
{%blockcontent%}
HelloChadevPythonusergroup!
{%endblock%}
Syntax to add logic to templates
{%for%}
{%endfor%}
Michael Scott Regional Manager
Dwight Schrute Assistant 'to the' Regional Manager
Jim Halpert Salesman
Jinja delimeters
Jinja has two kinds of delimiters. {% ... %} and {{ ... }}.
The first one is used to execute statements such as for-loops or assign
values, the latter prints the result of the expression to the template.
<ul>
{%foruserinusers%}
<li><ahref="{{user.url}}">{{user.username}}</a>{{user.title}}</li>
{%endfor%}
</ul>
Result:
Using Macros
DRY templates with Jinja
Macros
//macros.html
{{%macronav_link(endpoint,name)%}
{%ifrequest.endpoint.endswith(endpoint)%}
<liclass="active"><ahref="{{url_for(endpoint)}}">{{name}}</a></li>
{%else%}
<li><ahref="{{url_for(endpoint)}}">{{name}}</a></li>
{%endif%}
{%endmacro%}
//base.html
{%from"macros.html"importnav_linkwithcontext%}
..
<ulclass="navnavbar-nav">
{{nav_link('index','Home')}}
{{nav_link('python','Python')}}
{{nav_link('flask','Flask')}}
{{nav_link('extensions','Extensions')}}
{{nav_link('gifs','Gifs')}}
</ul>
update app.py
fromflaskimportrender_template
.
@app.route("/")
@app.route("/index")
defindex():
returnrender_template('index.html')
Iterate the list in the template
More Jinja Awesomeness
Send a list to our template
//app.py
..
@app.route("/extns")
defextensions():
extns=['Flask','Jinja2','Flask-SQLAlchemy','Flask-Admin',
'Flask-Classy','Flask-Raptor']
returnrender_template('extensions.html',extns=extns)
//extensions.html
{%extends"base.html"%}
{%blockcontent%}
<h3>AwesomeFlaskextensions</h3>
<br>
<ul>
{%forextinexts%}
<li>{{ext}}</li>
{%endfor%}
</ul>
<h3>
Viewmore<ahref="http://flask.pocoo.org/extensions/">Flaskextensions</a>
</h3>
{%endblock%}
Fun with Gifs!
Create an input form to search images
Pull an image from giphy api with python
Send image and form to template
Profit
Create an input form to search
images
Install Flask-WTF
At terminal run: pip install Flask-WTF
//forms.py
fromflask.ext.wtfimportForm
fromwtformsimportStringField
fromwtforms.validatorsimportDataRequired
classSearchForm(Form):
query=StringField('query',validators=[DataRequired()])
Pull an image from giphy api
with python
importurllib,json
importrandom
..
defget_image(query):
url='http://api.giphy.com/v1/gifs/search?q='
api_key='dc6zaTOxFJmzC&limit=5'
data=json.loads(urllib.urlopen(url+query+'&api_key='+api_key).read())
item=random.choice(data['data'])
gif=item['images']['original']['url']
returngif
Send the image and form to
our template
fromformsimportSearchForm
app=Flask(__name__)
app.secret_key='some_secret_key'
..
@app.route("/gifs",methods=['GET','POST'])
defgifs():
form=SearchForm()
ifform.validate_on_submit():
query=form.query.data
gif=get_image(query)
returnrender_template('gifs.html',gif=gif,form=form)
gif='static/img/dwight.gif'
returnrender_template('gifs.html',gif=gif,form=form)
gifs.html template
//gifs.html
{%extends"base.html"%}
{%blockcontent%}
<formaction=""method="post"name="query">
{{form.hidden_tag()}}
<p>
Searchforgifs:<br>
{{form.query(size=20)}}<br>
{%forerrorinform.query.errors%}
<spanstyle="color:red;">[{{error}}]</span>
{%endfor%}<br>
<inputtype="submit"value="Submit">
</p>
</form>
<imgsrc="{{gif}}">
{%endblock%}
Links
Source code on GitHub
Follow me on Twitter
Chadev Python Meetup
http://flaskbook.com/
http://blog.miguelgrinberg.com/index
http://jinja.pocoo.org/docs/dev/
http://flask.pocoo.org/
https://exploreflask.com/index.html
https://www.hakkalabs.co/articles/django-and-flask
http://www.fullstackpython.com/flask.html
https://pythonhosted.org/Flask-Classy/
http://flask-script.readthedocs.org/en/latest/
https://github.com/realpython/discover-flask
Go explore all these things
Questions
THE END

More Related Content

What's hot (20)

PPTX
jQuery PPT
Dominic Arrojado
 
PDF
Spring boot introduction
Rasheed Waraich
 
PPTX
Spring boot
Gyanendra Yadav
 
PPT
PHP Workshop Notes
Pamela Fox
 
PDF
Hibernate Presentation
guest11106b
 
PPTX
Spring boot Introduction
Jeevesh Pandey
 
PDF
HTML5: features with examples
Alfredo Torre
 
PPTX
Spring boot
sdeeg
 
PDF
Spring boot
Bhagwat Kumar
 
PDF
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
PPT
Javascript
mussawir20
 
PPT
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
PDF
Spring Boot
HongSeong Jeon
 
PPT
jQuery Ajax
Anand Kumar Rajana
 
PPT
Java Basics
Sunil OS
 
PPTX
Express js
Manav Prasad
 
PPTX
Main method in java
Hitesh Kumar
 
PDF
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
PPTX
Laravel Tutorial PPT
Piyush Aggarwal
 
jQuery PPT
Dominic Arrojado
 
Spring boot introduction
Rasheed Waraich
 
Spring boot
Gyanendra Yadav
 
PHP Workshop Notes
Pamela Fox
 
Hibernate Presentation
guest11106b
 
Spring boot Introduction
Jeevesh Pandey
 
HTML5: features with examples
Alfredo Torre
 
Spring boot
sdeeg
 
Spring boot
Bhagwat Kumar
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
Javascript
mussawir20
 
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
Spring Boot
HongSeong Jeon
 
jQuery Ajax
Anand Kumar Rajana
 
Java Basics
Sunil OS
 
Express js
Manav Prasad
 
Main method in java
Hitesh Kumar
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
Laravel Tutorial PPT
Piyush Aggarwal
 

Viewers also liked (18)

PDF
Build website in_django
swee meng ng
 
PDF
Flask patterns
it-people
 
PDF
Writing your first web app using Python and Flask
Danielle Madeley
 
KEY
LvivPy - Flask in details
Max Klymyshyn
 
PDF
Flask Full Stack - Desenvolvendo um CMS com Flask e MongoDB
Bruno Rocha
 
PPTX
Quokka CMS - Desenvolvendo web apps com Flask e MongoDB - grupy - Outubro 2015
Bruno Rocha
 
PDF
Rest in flask
Hamid Feizabadi
 
PDF
Flask SQLAlchemy
Eueung Mulyana
 
PDF
Python and GIS
Andrii Mishkovskyi
 
PPTX
Introduction to Google App Engine with Python
Brian Lyttle
 
PPT
Learn flask in 90mins
Larry Cai
 
PPTX
Django vs Flask
Rachel Sanders
 
PDF
Making use of OpenStreetMap data with Python
Andrii Mishkovskyi
 
PDF
Rest in flask
Yehor Nazarkin
 
PDF
Flask Introduction - Python Meetup
Areski Belaid
 
ODP
Django와 flask
Jiho Lee
 
PPTX
OpenStreetMap in 3D using Python
Martin Christen
 
PDF
Developing RESTful Web APIs with Python, Flask and MongoDB
Nicola Iarocci
 
Build website in_django
swee meng ng
 
Flask patterns
it-people
 
Writing your first web app using Python and Flask
Danielle Madeley
 
LvivPy - Flask in details
Max Klymyshyn
 
Flask Full Stack - Desenvolvendo um CMS com Flask e MongoDB
Bruno Rocha
 
Quokka CMS - Desenvolvendo web apps com Flask e MongoDB - grupy - Outubro 2015
Bruno Rocha
 
Rest in flask
Hamid Feizabadi
 
Flask SQLAlchemy
Eueung Mulyana
 
Python and GIS
Andrii Mishkovskyi
 
Introduction to Google App Engine with Python
Brian Lyttle
 
Learn flask in 90mins
Larry Cai
 
Django vs Flask
Rachel Sanders
 
Making use of OpenStreetMap data with Python
Andrii Mishkovskyi
 
Rest in flask
Yehor Nazarkin
 
Flask Introduction - Python Meetup
Areski Belaid
 
Django와 flask
Jiho Lee
 
OpenStreetMap in 3D using Python
Martin Christen
 
Developing RESTful Web APIs with Python, Flask and MongoDB
Nicola Iarocci
 
Ad

Similar to Quick flask an intro to flask (20)

PPTX
Phalcon 2 - PHP Brazil Conference
Jackson F. de A. Mafra
 
PPTX
Use Symfony2 components inside WordPress
Maurizio Pelizzone
 
PDF
Php Conference Brazil - Phalcon Giant Killer
Jackson F. de A. Mafra
 
PPTX
Phalcon - Giant Killer
Jackson F. de A. Mafra
 
PDF
Django Introduction & Tutorial
之宇 趙
 
PDF
Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)
Mark Hamstra
 
ODP
Drupal development
Dennis Povshedny
 
ODP
Web Development in Django
Lakshman Prasad
 
PDF
Use Xdebug to profile PHP
Seravo
 
PPT
How to? Drupal developer toolkit. Dennis Povshedny.
DrupalCampDN
 
PPTX
Joomla! Performance on Steroids
SiteGround.com
 
PPT
Html5 drupal7 with mandakini kumari(1)
Mandakini Kumari
 
PDF
WordPress At Scale. WordCamp Dhaka 2019
Anam Ahmed
 
ODP
A Good PHP Framework For Beginners Like Me!
Muhammad Ghazali
 
PPTX
PHP Conference - Phalcon hands-on
Jackson F. de A. Mafra
 
PDF
Magento Performance Optimization 101
Angus Li
 
PPT
WordPress Plugin Development- Rich Media Institute Workshop
Brendan Sera-Shriar
 
PDF
Plugin-based software design with Ruby and RubyGems
Sadayuki Furuhashi
 
PPTX
Web development with Python
Raman Balyan
 
Phalcon 2 - PHP Brazil Conference
Jackson F. de A. Mafra
 
Use Symfony2 components inside WordPress
Maurizio Pelizzone
 
Php Conference Brazil - Phalcon Giant Killer
Jackson F. de A. Mafra
 
Phalcon - Giant Killer
Jackson F. de A. Mafra
 
Django Introduction & Tutorial
之宇 趙
 
Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)
Mark Hamstra
 
Drupal development
Dennis Povshedny
 
Web Development in Django
Lakshman Prasad
 
Use Xdebug to profile PHP
Seravo
 
How to? Drupal developer toolkit. Dennis Povshedny.
DrupalCampDN
 
Joomla! Performance on Steroids
SiteGround.com
 
Html5 drupal7 with mandakini kumari(1)
Mandakini Kumari
 
WordPress At Scale. WordCamp Dhaka 2019
Anam Ahmed
 
A Good PHP Framework For Beginners Like Me!
Muhammad Ghazali
 
PHP Conference - Phalcon hands-on
Jackson F. de A. Mafra
 
Magento Performance Optimization 101
Angus Li
 
WordPress Plugin Development- Rich Media Institute Workshop
Brendan Sera-Shriar
 
Plugin-based software design with Ruby and RubyGems
Sadayuki Furuhashi
 
Web development with Python
Raman Balyan
 
Ad

Recently uploaded (20)

PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PDF
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PPTX
Wondershare Filmora Crack Free Download 2025
josanj305
 
PPTX
Securing Model Context Protocol with Keycloak: AuthN/AuthZ for MCP Servers
Hitachi, Ltd. OSS Solution Center.
 
PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
Software Development Company Keene Systems, Inc (1).pdf
Custom Software Development Company | Keene Systems, Inc.
 
PDF
NASA A Researcher’s Guide to International Space Station : Earth Observations
Dr. PANKAJ DHUSSA
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
NASA A Researcher’s Guide to International Space Station : Fundamental Physics
Dr. PANKAJ DHUSSA
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Wondershare Filmora Crack Free Download 2025
josanj305
 
Securing Model Context Protocol with Keycloak: AuthN/AuthZ for MCP Servers
Hitachi, Ltd. OSS Solution Center.
 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Software Development Company Keene Systems, Inc (1).pdf
Custom Software Development Company | Keene Systems, Inc.
 
NASA A Researcher’s Guide to International Space Station : Earth Observations
Dr. PANKAJ DHUSSA
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Digital Circuits, important subject in CS
contactparinay1
 
NASA A Researcher’s Guide to International Space Station : Fundamental Physics
Dr. PANKAJ DHUSSA
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 

Quick flask an intro to flask