SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Larry cai <larry.caiyu@gmail.com>
Agenda
 Introduction
 Exercise 1: Flask’s “Hello World”
 Exercise 2: Routing for URL
 Exercise 3:Template (Jinja2)
 Exercise 4: Handle form
 Exercise 5: Database (Flask-SQLAlchemy)
 Reference
Learn flask in 90 minutes2 04/13/15
http://flask.pocoo.org/
Environment Preparation (docker)
 Boot2docker Installer (127M)
 Contains latest docker already, fast
 Container persistence via disk automount on /var/lib/docker
 $ docker -v
 User/Passwd: docker/tcuser (192.168.59.103)
 Download python:2 docker image
 $ docker pull python:2
 (Windows/Mac) Clone the code from github to your user
directory (~/git)
https://github.com/larrycai/codingwithme-flask
 Notepad++ & MobaXterm are recommended
Learn flask in 90 minutes3 04/13/15
http://boot2docker.io/
Introduction
 Flask is a microframework for Python based on
Werkzeug, Jinja 2 and good intentions.
 Similar to Play framework (Java/Scala), Sinatra (Ruby),
 Also Django vs Ruby on Rails vs Grail ..
Learn flask in 90 minutes4 04/13/15
http://flask.pocoo.org/
Exercise 1: Hello World
 Run first app inside docker environment
$ cd /c/Users/<id>/git/codingwithme-flask
$ docker run -it -v $PWD:/code -w /code -p 5000:5000 python:2 bash
# pip install flask
# cd exer1
# python app.py
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
192.168.59.3 - - [08/Apr/2015 15:50:29] "GET / HTTP/1.1" 200 -
192.168.59.3 - - [08/Apr/2015 15:50:29] "GET /favicon.ico HTTP/1.1" 404
-
 Browse: http://192.168.59.103:5000
 Edit the app.py to print your name “Hello
Larry” instead of “Hello World”
 Tips: edit in windows, don’t need restart
(debug=True) Learn flask in 90 minutes5 04/13/15
Routing/Redirect for URL
 Routing is defined by the route() decorator
 Dynamic routing using variable
 Redirect to another one using redirect, url_for
Learn flask in 90 minutes6 04/13/15
Exercise 2: Routing
 Implement /user to suggest use /user/<name>
 Implement /user/<name>, say hello if it is Larry, otherwise
say byebye
 Redirect /users/ to /user with error indication
# cd ../exer2
# python app.py
 Tips: check console log redirect 302 (redirect)
Learn flask in 90 minutes7 04/13/15
Template using Jinja2
 Flask configures the Jinja2 template engine automatically
 render_template
 Jinja2 is a full featured template engine for Python. 
 Map python variable into template
 Template inheritance makes it possible to use the same
or a similar layout for all templates
Learn flask in 90 minutes8 04/13/15
http://jinja.pocoo.org/
Exercise 3: Jinja2 Template
 Use template instead of string for function hello_user()
# cd ../exer3
# python app.py
Learn flask in 90 minutes9 04/13/15
Handle form
 request.form is good enough for simple form
 Flask-WTF module can be used if needed complex
handling for the form (like validation)
Learn flask in 90 minutes10 04/13/15
Exercise 4: Web Form
 Provide form to user as started in user()
# cd ../exer4
# python app.py
 Check whether it is in allowed list and redirect to hello
page, otherwise show error message (“xxx, who bring
you here ?”)
 Tips: form is normal html form
Learn flask in 90 minutes11 04/13/15
Database ORM – Flask-SQLAlchemy
 Flask-SQLAlchemy is extension provides a wrapper
for the SQLAlchemy project, which is an Object
Relational Mapper or ORM
 Support different database (SQLite, MySQL, Postgre ….)
 Command Steps
 Define database
 The data model
 Create database
 Operate database ..
Learn flask in 90 minutes12 04/13/15
Exercise 5: ORM for Database
 Start with base comments app and understand
# cd ../exer5
# pip install flask-sqlalchemy
# python app.py
 Add extra item “nickname”
 Bonus:
Initialize the database (/create) if database doesn’t exist (remove
*.sqlite3 first)
Reset the database (/drop)
Tips:
db.create_all(app=app) & db.drop_all(app=app)
Learn flask in 90 minutes13 04/13/15
example is based on: http://runnable.com/UhcUVQ7XwdEqAAPg/using-
sqlalchemy-in-flask-for-python
Summary
 Flask is python web framework, similar exists in other
language
 Routing/Template/Form/Database are the base
knowledge.
 Simple but powerful (together with css/javascript)
 Learn more sample, and try to build your own !!
Learn flask in 90 minutes14 04/13/15
Reference
 http://flask.pocoo.org/
 Book: Flask Web Development:
http://oreilly.com/catalog/9781449372620/
 Miguel Grinberg’s Blog: http://blog.miguelgrinberg.com/
 Base example for exer5 :
http://runnable.com/UhcUVQ7XwdEqAAPg/using-sqlalchemy-
in-flask-for-python

Learn flask in 90 minutes15 04/13/15
ChangeLog
 2015/04/12: first release
 2015/04/13: after first pilot, summary is added
Learn docker in 90 minutes16 04/13/15

Weitere ähnliche Inhalte

Was ist angesagt?

A Basic Django Introduction
A Basic Django IntroductionA Basic Django Introduction
A Basic Django Introduction
Ganga Ram
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
Jakub Kubrynski
 

Was ist angesagt? (20)

Flask for cs students
Flask for cs studentsFlask for cs students
Flask for cs students
 
Flask Basics
Flask BasicsFlask Basics
Flask Basics
 
Spring boot
Spring bootSpring boot
Spring boot
 
Maven Introduction
Maven IntroductionMaven Introduction
Maven Introduction
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Laravel ppt
Laravel pptLaravel ppt
Laravel ppt
 
.Net Core
.Net Core.Net Core
.Net Core
 
A Basic Django Introduction
A Basic Django IntroductionA Basic Django Introduction
A Basic Django Introduction
 
Php tutorial
Php  tutorialPhp  tutorial
Php tutorial
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
 
Django로 쇼핑몰 만들자
Django로 쇼핑몰 만들자Django로 쇼핑몰 만들자
Django로 쇼핑몰 만들자
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
MySQLi
MySQLiMySQLi
MySQLi
 
The World of PHP PSR Standards
The World of PHP PSR StandardsThe World of PHP PSR Standards
The World of PHP PSR Standards
 
Module design pattern i.e. express js
Module design pattern i.e. express jsModule design pattern i.e. express js
Module design pattern i.e. express js
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
 
Mongoose: MongoDB object modelling for Node.js
Mongoose: MongoDB object modelling for Node.jsMongoose: MongoDB object modelling for Node.js
Mongoose: MongoDB object modelling for Node.js
 

Andere mochten auch

Flask入門
Flask入門Flask入門
Flask入門
aoshiman
 
Flask patterns
Flask patternsFlask patterns
Flask patterns
it-people
 

Andere mochten auch (12)

Rest in flask
Rest in flaskRest in flask
Rest in flask
 
Flask SQLAlchemy
Flask SQLAlchemy Flask SQLAlchemy
Flask SQLAlchemy
 
Learning python with flask (PyLadies Malaysia 2017 Workshop #1)
Learning python with flask (PyLadies Malaysia 2017 Workshop #1)Learning python with flask (PyLadies Malaysia 2017 Workshop #1)
Learning python with flask (PyLadies Malaysia 2017 Workshop #1)
 
webアプリケーションフレームワークの話
webアプリケーションフレームワークの話webアプリケーションフレームワークの話
webアプリケーションフレームワークの話
 
Webアプリケーションフレームワーク flask
Webアプリケーションフレームワーク flaskWebアプリケーションフレームワーク flask
Webアプリケーションフレームワーク flask
 
Flask入門
Flask入門Flask入門
Flask入門
 
Writing your first web app using Python and Flask
Writing your first web app using Python and FlaskWriting your first web app using Python and Flask
Writing your first web app using Python and Flask
 
Flask patterns
Flask patternsFlask patterns
Flask patterns
 
Flask勉強会その1
Flask勉強会その1Flask勉強会その1
Flask勉強会その1
 
Python and you
Python and youPython and you
Python and you
 
Rest in flask
Rest in flaskRest in flask
Rest in flask
 
Developing RESTful Web APIs with Python, Flask and MongoDB
Developing RESTful Web APIs with Python, Flask and MongoDBDeveloping RESTful Web APIs with Python, Flask and MongoDB
Developing RESTful Web APIs with Python, Flask and MongoDB
 

Ähnlich wie Learn flask in 90mins

How to? Drupal developer toolkit. Dennis Povshedny.
How to? Drupal developer toolkit. Dennis Povshedny.How to? Drupal developer toolkit. Dennis Povshedny.
How to? Drupal developer toolkit. Dennis Povshedny.
DrupalCampDN
 
Drush. Why should it be used?
Drush. Why should it be used?Drush. Why should it be used?
Drush. Why should it be used?
Sergei Stryukov
 

Ähnlich wie Learn flask in 90mins (20)

Drupal development
Drupal development Drupal development
Drupal development
 
How to? Drupal developer toolkit. Dennis Povshedny.
How to? Drupal developer toolkit. Dennis Povshedny.How to? Drupal developer toolkit. Dennis Povshedny.
How to? Drupal developer toolkit. Dennis Povshedny.
 
How To Install Openbravo ERP 2.50 MP43 in Ubuntu
How To Install Openbravo ERP 2.50 MP43 in UbuntuHow To Install Openbravo ERP 2.50 MP43 in Ubuntu
How To Install Openbravo ERP 2.50 MP43 in Ubuntu
 
Development Setup of B-Translator
Development Setup of B-TranslatorDevelopment Setup of B-Translator
Development Setup of B-Translator
 
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
 
Learn basic ansible using docker
Learn basic ansible using dockerLearn basic ansible using docker
Learn basic ansible using docker
 
Introduction to PowerShell
Introduction to PowerShellIntroduction to PowerShell
Introduction to PowerShell
 
Server(less) Swift at SwiftCloudWorkshop 3
Server(less) Swift at SwiftCloudWorkshop 3Server(less) Swift at SwiftCloudWorkshop 3
Server(less) Swift at SwiftCloudWorkshop 3
 
Developing IT infrastructures with Puppet
Developing IT infrastructures with PuppetDeveloping IT infrastructures with Puppet
Developing IT infrastructures with Puppet
 
Docker perl build
Docker perl buildDocker perl build
Docker perl build
 
IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...
IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...
IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...
 
Drush. Why should it be used?
Drush. Why should it be used?Drush. Why should it be used?
Drush. Why should it be used?
 
Zero Downtime Deployment with Ansible
Zero Downtime Deployment with AnsibleZero Downtime Deployment with Ansible
Zero Downtime Deployment with Ansible
 
Oracle applications 11i dba faq
Oracle applications 11i dba faqOracle applications 11i dba faq
Oracle applications 11i dba faq
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
 
Introduction to Drupal - Installation, Anatomy, Terminologies
Introduction to Drupal - Installation, Anatomy, TerminologiesIntroduction to Drupal - Installation, Anatomy, Terminologies
Introduction to Drupal - Installation, Anatomy, Terminologies
 
Docker Introduction.pdf
Docker Introduction.pdfDocker Introduction.pdf
Docker Introduction.pdf
 
Phalcon - Giant Killer
Phalcon - Giant KillerPhalcon - Giant Killer
Phalcon - Giant Killer
 
ScalaUA - distage: Staged Dependency Injection
ScalaUA - distage: Staged Dependency InjectionScalaUA - distage: Staged Dependency Injection
ScalaUA - distage: Staged Dependency Injection
 

Mehr von Larry Cai

Learn RabbitMQ with Python in 90mins
Learn RabbitMQ with Python in 90minsLearn RabbitMQ with Python in 90mins
Learn RabbitMQ with Python in 90mins
Larry Cai
 
Learn REST API with Python
Learn REST API with PythonLearn REST API with Python
Learn REST API with Python
Larry Cai
 
Practical way to experience of Specification by Example
Practical way to experience of Specification by ExamplePractical way to experience of Specification by Example
Practical way to experience of Specification by Example
Larry Cai
 
Experience from specification_by_examples
Experience from specification_by_examplesExperience from specification_by_examples
Experience from specification_by_examples
Larry Cai
 

Mehr von Larry Cai (19)

Learn kubernetes in 90 minutes
Learn kubernetes in 90 minutesLearn kubernetes in 90 minutes
Learn kubernetes in 90 minutes
 
Learn jobDSL for Jenkins
Learn jobDSL for JenkinsLearn jobDSL for Jenkins
Learn jobDSL for Jenkins
 
Learn RabbitMQ with Python in 90mins
Learn RabbitMQ with Python in 90minsLearn RabbitMQ with Python in 90mins
Learn RabbitMQ with Python in 90mins
 
Learn ELK in docker
Learn ELK in dockerLearn ELK in docker
Learn ELK in docker
 
Software Engineer Talk
Software Engineer TalkSoftware Engineer Talk
Software Engineer Talk
 
Learn nginx in 90mins
Learn nginx in 90minsLearn nginx in 90mins
Learn nginx in 90mins
 
Build service with_docker_in_90mins
Build service with_docker_in_90minsBuild service with_docker_in_90mins
Build service with_docker_in_90mins
 
Learn docker in 90 minutes
Learn docker in 90 minutesLearn docker in 90 minutes
Learn docker in 90 minutes
 
Learn Dashing Widget in 90 minutes
Learn Dashing Widget in 90 minutesLearn Dashing Widget in 90 minutes
Learn Dashing Widget in 90 minutes
 
Learn REST API with Python
Learn REST API with PythonLearn REST API with Python
Learn REST API with Python
 
Jenkins Scriptler in 90mins
Jenkins Scriptler in 90minsJenkins Scriptler in 90mins
Jenkins Scriptler in 90mins
 
Python virtualenv & pip in 90 minutes
Python virtualenv & pip in 90 minutesPython virtualenv & pip in 90 minutes
Python virtualenv & pip in 90 minutes
 
Lead changes in software development
Lead changes in software developmentLead changes in software development
Lead changes in software development
 
Python in 90mins
Python in 90minsPython in 90mins
Python in 90mins
 
Practical way to experience of Specification by Example
Practical way to experience of Specification by ExamplePractical way to experience of Specification by Example
Practical way to experience of Specification by Example
 
Experience from specification_by_examples
Experience from specification_by_examplesExperience from specification_by_examples
Experience from specification_by_examples
 
Write book in markdown
Write book in markdownWrite book in markdown
Write book in markdown
 
Continuous Integration Introduction
Continuous Integration IntroductionContinuous Integration Introduction
Continuous Integration Introduction
 
Agile & ALM tools
Agile & ALM toolsAgile & ALM tools
Agile & ALM tools
 

Kürzlich hochgeladen

+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
 

Kürzlich hochgeladen (20)

CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
+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...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
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...
 
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...
 
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
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
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
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
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
 
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, ...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 

Learn flask in 90mins

  • 2. Agenda  Introduction  Exercise 1: Flask’s “Hello World”  Exercise 2: Routing for URL  Exercise 3:Template (Jinja2)  Exercise 4: Handle form  Exercise 5: Database (Flask-SQLAlchemy)  Reference Learn flask in 90 minutes2 04/13/15 http://flask.pocoo.org/
  • 3. Environment Preparation (docker)  Boot2docker Installer (127M)  Contains latest docker already, fast  Container persistence via disk automount on /var/lib/docker  $ docker -v  User/Passwd: docker/tcuser (192.168.59.103)  Download python:2 docker image  $ docker pull python:2  (Windows/Mac) Clone the code from github to your user directory (~/git) https://github.com/larrycai/codingwithme-flask  Notepad++ & MobaXterm are recommended Learn flask in 90 minutes3 04/13/15 http://boot2docker.io/
  • 4. Introduction  Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions.  Similar to Play framework (Java/Scala), Sinatra (Ruby),  Also Django vs Ruby on Rails vs Grail .. Learn flask in 90 minutes4 04/13/15 http://flask.pocoo.org/
  • 5. Exercise 1: Hello World  Run first app inside docker environment $ cd /c/Users/<id>/git/codingwithme-flask $ docker run -it -v $PWD:/code -w /code -p 5000:5000 python:2 bash # pip install flask # cd exer1 # python app.py * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) 192.168.59.3 - - [08/Apr/2015 15:50:29] "GET / HTTP/1.1" 200 - 192.168.59.3 - - [08/Apr/2015 15:50:29] "GET /favicon.ico HTTP/1.1" 404 -  Browse: http://192.168.59.103:5000  Edit the app.py to print your name “Hello Larry” instead of “Hello World”  Tips: edit in windows, don’t need restart (debug=True) Learn flask in 90 minutes5 04/13/15
  • 6. Routing/Redirect for URL  Routing is defined by the route() decorator  Dynamic routing using variable  Redirect to another one using redirect, url_for Learn flask in 90 minutes6 04/13/15
  • 7. Exercise 2: Routing  Implement /user to suggest use /user/<name>  Implement /user/<name>, say hello if it is Larry, otherwise say byebye  Redirect /users/ to /user with error indication # cd ../exer2 # python app.py  Tips: check console log redirect 302 (redirect) Learn flask in 90 minutes7 04/13/15
  • 8. Template using Jinja2  Flask configures the Jinja2 template engine automatically  render_template  Jinja2 is a full featured template engine for Python.   Map python variable into template  Template inheritance makes it possible to use the same or a similar layout for all templates Learn flask in 90 minutes8 04/13/15 http://jinja.pocoo.org/
  • 9. Exercise 3: Jinja2 Template  Use template instead of string for function hello_user() # cd ../exer3 # python app.py Learn flask in 90 minutes9 04/13/15
  • 10. Handle form  request.form is good enough for simple form  Flask-WTF module can be used if needed complex handling for the form (like validation) Learn flask in 90 minutes10 04/13/15
  • 11. Exercise 4: Web Form  Provide form to user as started in user() # cd ../exer4 # python app.py  Check whether it is in allowed list and redirect to hello page, otherwise show error message (“xxx, who bring you here ?”)  Tips: form is normal html form Learn flask in 90 minutes11 04/13/15
  • 12. Database ORM – Flask-SQLAlchemy  Flask-SQLAlchemy is extension provides a wrapper for the SQLAlchemy project, which is an Object Relational Mapper or ORM  Support different database (SQLite, MySQL, Postgre ….)  Command Steps  Define database  The data model  Create database  Operate database .. Learn flask in 90 minutes12 04/13/15
  • 13. Exercise 5: ORM for Database  Start with base comments app and understand # cd ../exer5 # pip install flask-sqlalchemy # python app.py  Add extra item “nickname”  Bonus: Initialize the database (/create) if database doesn’t exist (remove *.sqlite3 first) Reset the database (/drop) Tips: db.create_all(app=app) & db.drop_all(app=app) Learn flask in 90 minutes13 04/13/15 example is based on: http://runnable.com/UhcUVQ7XwdEqAAPg/using- sqlalchemy-in-flask-for-python
  • 14. Summary  Flask is python web framework, similar exists in other language  Routing/Template/Form/Database are the base knowledge.  Simple but powerful (together with css/javascript)  Learn more sample, and try to build your own !! Learn flask in 90 minutes14 04/13/15
  • 15. Reference  http://flask.pocoo.org/  Book: Flask Web Development: http://oreilly.com/catalog/9781449372620/  Miguel Grinberg’s Blog: http://blog.miguelgrinberg.com/  Base example for exer5 : http://runnable.com/UhcUVQ7XwdEqAAPg/using-sqlalchemy- in-flask-for-python  Learn flask in 90 minutes15 04/13/15
  • 16. ChangeLog  2015/04/12: first release  2015/04/13: after first pilot, summary is added Learn docker in 90 minutes16 04/13/15