learning python with flask (pyladies malaysia 2017 workshop #1)

69
learning python with flask P Y L A D I E S 2 0 1 7 W O R K S H O P # 1 MALAYSIA

Upload: sian-lerk-lau

Post on 06-Apr-2017

76 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

learningpython with flaskP Y L A D I E S 2 0 1 7 W O R K S H O P # 1

MALAYSIA

Page 2: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

before we start

Page 3: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

whypyladies

M A L A Y S I A

MALAYSIA

Page 4: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

howpyladies

W O R K S

MALAYSIA

Page 5: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

pyladiesneed you!

B E P A R T O F T H E H I S T O R Y M A K E R S

MALAYSIA

Page 6: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)
Page 7: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

Just right before we start

Page 8: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

@OnApp https://facebook.com/OnApp

A SHOUT OUT TO OUR VENUE SPONSOR

Page 9: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

introduction

Page 10: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

SIAN LERK LAUlinkedin.com/in/sianlerk

@kiawin

Page 11: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

∗ Amateur guitarist

∗ Ex-Intern at OnApp KL

∗ UM undergraduate student (3rd year)

∗ Python newbie, 8 months old

FADHIL YAACOB@sdil

Page 12: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

a shout out to ourpyladies mentors

ALIES YAPJANICE SHIU

PEI PEI

MALAYSIA

Page 13: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

jom!

Page 14: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)
Page 15: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

R UREHDYTO PYTHON*

Page 16: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

DO.* WHY PYTHON* THE ENVIRONMENT (PART 1 & 2)* PYTHON 101* CONTROL STRUCTURES* A REAL PROGRAM

Page 17: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

WHY* CODE IN PYTHON

Page 18: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

EASY TO UNDERSTANDCONCISE SYNTAXMULTI-PURPOSE STRENGTH OF PYTHON

Page 19: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

GOOGLE-ABLE!WELL SUPPORTED LIBSFAST TO DELIVER STRENGTH OF PYTHON

Page 20: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

ENV*PYTHON ENVIRONMENT (PART 1)

Page 21: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

# Python interactive shell$ python

# Python interactive shell # (on steroid!)$ pip install ipython$ ipython

# QUIZ: What version of python # are you currently using?

ENV* - EASIEST WAY TO PYTHON

Page 22: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

101*PYTHON - DATA TYPES

Page 23: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

# DECLARE-LESS TYPED>>> a = 1>>> print a1>>> type(a)<type 'int'>

101* - DATA TYPES

Page 24: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

# MORE TYPES - '' and "">>> b = 'a'>>> c = 'abc'>>> d = "abc"# QUIZ: WHAT DATA TYPES ARE b, c and d?>>> type(b)>>> type(c)>>> type(d)101* - DATA TYPES

Page 25: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

# MORE TYPES# bool, NoneType, float, long>>> e = True>>> f = False>>> g = None>>> h = 1.0>>> i = 1L# QUIZ: WHAT IS None?

101* - DATA TYPES

Page 26: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

101*PYTHON - COLLECTIONS

Page 27: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

# COLLECTION 1: LIST>>> a = [1, 2, 3]>>> b = list()>>> b.append(1)>>> b.append(2)>>> b.append(3)# QUIZ: How do we retrieve the value?# QUIZ: Is a and b same?

101* - DATA TYPES

Page 28: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

# SAME - EQUALITY or IDENTITY?>>> a == bTrue>>> a is bFalse

# QUIZ: WHAT IS THE DIFF # BETWEEN == AND is

101* - DATA TYPES

Page 29: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

# COLLECTION 2 - TUPLE>>> c = (1, 2, 3)>>> d = 1, 2, 3

# QUIZ: SO AGAIN, IS c SAME with d?

101* - DATA TYPES

Page 30: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

# COLLECTION 3: dict>>> e = {1: 11, 2: 22}>>> e[1]>>> e[2]

# QUIZ: IS THIS AN array?# QUIZ: MUST THE key BE int?

101* - DATA TYPES

Page 31: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

# COLLECTION 4: set>>> f = set()>>> f.add(1)>>> f.add(2)>>> f.add(3)

# QUIZ: WHAT IS THE DIFF # BETWEEN list AND set?

101* - DATA TYPES

Page 32: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

101*PYTHON - CONTROL STRUCTURES

Page 33: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

101* - DATA TYPES

# if, else, elif>>> a = 2>>> if a == 1:... print "hello"... elif a == 2:... print "world"

>>>

Page 34: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

101* - DATA TYPES

# if, else, elif CONTINUES>>> a = 1>>> if a == 1:... print "hello"... else:... print "world"

>>>

Page 35: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

101* - DATA TYPES

# for LOOP>>> for i in [1, 2, 3]:... print i

>>> for j in range(1,3):... print j

# QUIZ: WHAT DO YOU SEE WHEN print j

Page 36: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

101* - DATA TYPES

# for LOOP>>> for i in (1, 2, 3):... print i

>>> for k,v in {1: 11, 2: 22}.iteritems():... print k, v

# QUIZ: WHAT DO YOU SEE WHEN print k, v

Page 37: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

101* - DATA TYPES

# list comprehension>>> a = [1,2,3,4,5]>>> b = [i+1 for i in a]

# QUIZ: WHAT IS THE VALUE OF b?

Page 38: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

101* - DATA TYPES

# list comprehension>>> a = [1,2,3,4,5]>>> b = [i for i in a if i % 2 == 0]

# QUIZ: WHAT IS THE VALUE OF b?

Page 39: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

ENV*PYTHON ENVIRONMENT (PART 2)

Page 40: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

pip∗ Package management system used to install and

manage software packages written in Python∗ Why?

Easy package installations, updating, configuring and removals with a single command

∗ Over 86,000 Python packages can be accessed through PyPI

∗ Sample commands:$ pip install flask$ pip uninstall flask

Page 41: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

python packages∗ Mathematical Analytics: Numpy∗ DB Connectors: MySQL, MongoDB,

PostgreSQL, etc.∗ Web Frameworks: Pyramid, Django∗ Twitter API client∗ Many more…“It’s like Swiss-army toolchain”

Page 42: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

virtualenv∗ Is an isolated working copy of Python which allows

you to work on a specific project without worry of affecting other projects

∗ Why?We might wants to maintain the package version (prefer not upgrading it to newer version)

“Do not fix things that aren’t broken”

Page 43: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

THE BIG PICTUREPython3

virtualenv1 virtualenv2 virtualenv3

package1 v1

package2 v1

package3 v1

package1 v2

package2 v2

package3 v2

package1 v1

package2 v2

package3 v1

Page 44: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

virtualenv# INSTALL VIRTUALENV$ pip install virtualenv

# CREATE A NEW VENV$ virtualenv Project1

Page 45: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

virtualenv# ACTIVATE A VENV ON LINUXworkon Project1# ON WINDOWS$ /path/to/folder/Scripts/activate.bat

# LIST INSTALLED PACKAGES(test)$ pip freeze

Page 46: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

PROG*A REAL PROGRAM, NOW

Page 47: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

flask∗ Flask is a micro web framework written in Python∗ Micro framework means it does not require

particular tools or libraries and it just works

∗ Suitable for minimal website or a full featured website

∗ Many extensions freely available:User authenticationsFlask-SecurityFile UploadsDatabase

Page 48: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

Let’s get hand dirty

∗ “Hello World!”

Page 49: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

PROG* - HELLO WORLD!

# INSTALL FLASK MODULE$ pip install flask

# QUIZ: WHAT DO YOU SEE IN RESULT# OF THE ABOVE COMMAND

Page 50: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

PROG* - HELLO WORLD!

# EDIT A NEW FILE USING YOUR FAVOURITE# TEXT EDITOR$ vim hello.py

Page 51: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

PROG* - HELLO WORLD!

# KEY IN THE FOLLOWING INTO THE FILEfrom flask import Flaskapp = Flask(__name__)

@app.route("/")def hello(): return "Hello World!"

Page 52: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

NOOO WAY… R U KIDDING ME?

CONGRATULATIONS!U HV COMPLETED

A WORKING PROGRAM WEB SERVICE

Page 53: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

# LET’S START THE WEB SERVICE$ export FLASK_APP=hello.py$ flask run

# IF WINDOWS$ set FLASK_APP=hello.py$ flask run

# QUIZ: WHAT’S NEXT?

PROG* - HELLO WORLD!

Page 54: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

localhost:5000

BEAM ME UP, SCOTTY

Hello World!

Page 55: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

SIMPLICITYProgramming can be easy and fun

(Using python)

Page 56: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

PROG*WHAT’S NEXT? MOOOOOOORE~

Page 57: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

localhost:5000

BEAM ME UP, SCOTTY… AGAIN

Not Hello World!

Page 58: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

localhost:5000/hello

HECTOR AND THE SEARCH FOR HAPPINESS

Hello World!

Page 59: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

PROG* - MOOOOOOORE~

# KEY IN THE FOLLOWING INTO THE FILEfrom flask import Flaskapp = Flask(__name__)

@app.route("/")def index(): return "Not Hello World!"

@app.route("/hello")def hello(): return "Hello World!"

# QUIZ: MUST THE ROUTE NAME SAME WITH # THE METHOD NAME?

Page 60: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

PROG* - MOOOOOOOORE~

# QUIZ: CAN I DON’T RESTART FLASK# EVERY TIME I MODIFY MY FILE?$ export FLASK_DEBUG=1$ flask run

# QUIZ: WHAT IS THE COMMAND IF YOU USE# WINDOWS?

Page 61: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

PROG* - MOOOOOOORE~

# KEY IN THE FOLLOWING INTO THE FILEfrom flask import Flaskapp = Flask(__name__)

@app.route("/")def index(): return "Index!"

@app.route("/user/<username>")def user(username): return "Hello %s!" % username

# QUIZ: WHAT HAPPEN WHEN YOU VISIT# http://localhost:5000/user

Page 62: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

localhost:5000/user/kiawin

WHO AM I

Hello kiawin!

Page 63: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

PROG* - MOOOOOOORE~

# FRUIT TIME!from flask import Flaskapp = Flask(__name__)

@app.route("/fruit/<fruit_name>")def fruit(fruit_name): if fruit_name in ["durian", "jackfruit"]: return "Nooooo..." else: return "Yummy..."

# QUIZ: WHAT HAPPEN IF YOU VISIT # http://localhost:5000/

Page 64: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

localhost:5000/fruit/durian

KING OF FRUITS

Nooooo...

Page 65: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

localhost:5000/fruit/mangosteen

HOW ABOUT MANGOSTEEN

Yummy...

Page 66: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

localhost:5000/fruit/DURIAN

WHAT IF?

Nooooo...

Page 67: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

PROG* - MOOOOOOORE~

# FrUiT TiMe!from flask import Flaskapp = Flask(__name__)

@app.route("/fruit/<fruit_name>")def fruit(fruit_name): if fruit_name.lower() in ["durian", "jackfruit"]: return "Nooooo..." else: return "Yummy..."

Page 68: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

STUFF*SLIDESslideshare.net/kiawin/learning-python-with-flask

SOURCEflask.pocoo.org/docs/0.12/quickstart

Page 69: Learning python with flask (PyLadies Malaysia 2017 Workshop #1)

Q&A*ASK US ANYTHING