Writing your first web app using Python and Flask

9,168 views
9,232 views

Published on

This tutorial will help you create your first webapp using Python and the Flask microframework. We will cover the following topics:

* What is Flask and why would you use it
* Rendering your first response
* Handling forms and POST data
* Using SQLAlchemy
* Writing your first RESTful API
* Holding open a connection to emit updates
* Testing your app with py.test
* Deploying to Redhat's Openshift platform

Familiarity with Python is assumed. A passing familiarity with Javascript is recommended by not essential.

https://github.com/danni/linux-conf-au-flask-tute

http://mirror.linux.org.au/linux.conf.au/2014/Thursday/87-Writing_your_first_web_app_using_Python_and_Flask_-_Danielle_Madeley.mp4

Published in: Technology, Sports
1 Comment
9 Likes
Statistics
Notes
  • Something is wrong with page N°17.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
9,168
On SlideShare
0
From Embeds
0
Number of Embeds
3,764
Actions
Shares
0
Downloads
124
Comments
1
Likes
9
Embeds 0
No embeds

No notes for slide

Writing your first web app using Python and Flask

  1. 1.  niugnepinn ad inn ad/gro.emong.sgolb  ksalF dna nohtyP gnisu ppa bew tsrif ruoy gnitirW flickr.com/photos/kevinomara/769 egnahcxofnI ,yeledaM elleinaD
  2. 2. tnemnorivne ruoy pu tes ot EMDAER eht wolloF etut-ksalf-ua-fnoc-xunil/innad/moc.buhtig//:tig enolc tig ...ydaerla t'nevah uoy fI
  3. 3. mroftalp tfihsnepO s'tahdeR ot gniyolpeD tset.yp htiw ppa ruoy gnitseT setadpu time ot noitcennoc a nepo gnidloH IPA lufTSER tsrif ruoy gnitirW ymehclALQS gnisU atad TSOP dna smrof gnildnaH esnopser tsrif ruoy gniredneR ti esu uoy dluow yhw dna ksalF si tahW
  4. 4. !gniddik tsuJ igc tropmi
  5. 5. ?ksalF si tahw tsuJ flickr.com/photos/markstos/35791
  6. 6. esnopser tsrif ruoy gniredneR
  7. 7. #/s/i/n pto !urbnev yhn fo fakipr Fak rm ls mot ls ap=Fak_nm_) p ls(_ae_ i _nm_ = '_an_: f _ae_ = _mi_' aprndbgTu) p.u(eu=re
  8. 8. #/s/i/n pto !urbnev yhn fo fakipr Fak rm ls mot ls ap=Fak_nm_) p ls(_ae_ @p.ot(/) aprue'' dfidx) e ne(: ""oeae" "Hmpg"" rtr "el,lnxcn.u eun Hlo iu.ofa" i _nm_ = '_an_: f _ae_ = _mi_' aprndbgTu) p.u(eu=re
  9. 9. yp tini ppabew nohtyp txt stnemeriuqer r llatsni pip elpmaxe tuokcehc tig etavitca nib vne nohtyp
  10. 10. fo fakipr rdrc,rne_epae rm ls mot eiet edrtmlt @p.ot(/-eiet) aprue'ardrc' dfardrc(: e _eiet) ""eietteue"" "Rdrc h sr" rtr rdrc(OEUL eun eietSM_R) @p.ot(/-epae) aprue'atmlt' dfatmlt(: e _epae) ""edrapg uigaJna tmlt"" "Rne ae sn ig2 epae" rtr rne_epae'epaehm' eun edrtmlt(tmlt.tl)
  11. 11. lmth.etalpmet /setalpmet yp.__tini__ /ppabew :yrotcerid /setalpmet s'eludom eht ni detacol era setalpmet :tluafed yB
  12. 12. yp tini ppabew nohtyp txt stnemeriuqer r llatsni pip elpmaxe tuokcehc tig
  13. 13. sgnirts yreuq gnildnah
  14. 14. fo fakipr rqet rm ls mot eus @p.ot(/) aprue'' dfidx) e ne(: ""euntenm qeyagmn"" "Rtr h ae ur ruet" rtr rqetag[nm' eun eus.rs'ae]
  15. 15. yllufTSER ti gniod ro
  16. 16. @p.ot(/tm<n:k' aprue'ie/itp>) dfgtie(k: e e_tmp) ""eunteie rfre t b p"" "Rtr h tm eerd o y k" #.. .
  17. 17. rorrEailartsuAuxuniLosatsiluogAsgraitseuqer ye seseaerp tnn rpittsg sxun Lseuqer tn s o ts lon sgra tseuqer te gra t uaKfnocixrniltue eilarteu sgra tseuqer ua.fnoc.xunil=uo&ailartsuA+xuniL=uo?
  18. 18. atad TSOP dna smrof gnildnah
  19. 19. fo fakipr rqet rm ls mot eus @p.ot(/) aprue'' dfidx) e ne(: ""euntenm PS vle" "Rtr h ae OT au"" rtr rqetfr[nm' eun eus.om'ae]
  20. 20. yrarbil smrof a esu llits retteb ro FTW ksalF llatsni pip
  21. 21. fo fakipr Fak rne_epae rm ls mot ls, edrtmlt fo faketwfipr Fr rm ls.x.t mot om fo wfrsipr TxFed rm tom mot etil casRgFr(om: ls eoomFr) "" sml rg fr"" "A ipe eo om" eal=TxFed'mi' mi etil(Eal) @p.ot(/eitr,mtos(GT,'OT) aprue'rgse' ehd='E' PS') dfgtrgse(: e e_eitr) ""adetergsrto fr"" "Hnl h eitain om" fr =RgFr( om eoom) i fr.aiaeo_umt) f omvldt_nsbi(: rtr "ucs" eun Sces rtr rne_epae'epaehm' fr=om eun edrtmlt(tmlt.tl, omfr) i _nm_ = '_an_: f _ae_ = _mi_' apsce_e ='HSI RAL SCE' p.ertky TI S ELY ERT aprndbgTu) p.u(eu=re
  22. 22. fo faketwfipr Fr rm ls.x.t mot om fo wfrsipr TxFed vldtr rm tom mot etil, aiaos casRgFr(om: ls eoomFr) "" sml rg fr"" "A ipe eo om" eal=TxFed'mi' mi etil(Eal vldtr=vldtr.aaeu aiaos(aiaosDtRq vldtr.mi() aiaosEal)
  23. 23. <ommto=ps" fr ehd"ot> { fr.idntg)} { frCR. iprat # { omhde_a( } # o SF. motn! } { fr.mi.ae } { omeallbl } { fr.mi } { omeal } { i fr.mi.ros% % f omealerr } <l u> { frerri fr.mi.ros% % o ro n omealerr } <i{ err}<l> l>{ ro }/i { edo % % nfr } <u> /l { edf% % ni } <nu tp=sbi" ipt ye"umt> <fr> /om
  24. 24. ymehcla LQS ymehclALQS ksalF llatsni pip
  25. 25. fo faketsllhm ipr SLlhm rm ls.x.qacey mot QAcey d =SLlhm(p) b QAceyap casUe(bMdl: ls srd.oe) "" ue i m dtbs"" "A sr n y aaae" _tbeae_='sr' _alnm_ ues i =d.ound.nee,piaykyTu) d bClm(bItgr rmr_e=re #.. .
  26. 26. i _nm_ = "_an_: f _ae_ = _mi_" apcni[SLLHM_AAAEUI]= p.ofg'QACEYDTBS_R' 'otrsl/uenm:asodlclotma psgeq:/sraepswr@oahs/y aprndbgTu) p.u(eu=re
  27. 27. ue =d.eso.ur(sr sr bssinqeyUe) .itrUe.sri = ue_d fle(srue_d = sri) .n( oe)
  28. 28. etargiM ksalF llatsni pip )cibmelA htiw( snoitargim +
  29. 29. tpircS-ksalF :ruoteD
  30. 30. fo fakipr Fak rm ls mot ls fo faketsrp ipr Mngr rm ls.x.cit mot aae ap=Fak_nm_) p ls(_ae_ mngr=Mngrap aae aae(p) i _nm_ = '_an_: f _ae_ = _mi_' mngrrn) aae.u(
  31. 31. redaoler htiw gnitratseR ptth no gninnuR revresnur yp tini nohtyp tixe dna egassem pleh siht wohs pleh h stnemugra lanoitpo nur ppa e i revres tnempoleved ksalF eht snuR revresnur txetnoc noitacilppa ksalF edisni llehs nohtyP a snuR llehs revresnur llehs stnemugra lanoitisop revresnur llehs h yp tini egasu yp tini nohtyp
  32. 32. fo fakipr Fak rm ls mot ls fo faketmgaeipr Mgae Mgaeomn rm ls.x.irt mot irt, irtCmad fo faketsrp ipr Mngr rm ls.x.cit mot aae fo faketsllhm ipr SLlhm rm ls.x.qacey mot QAcey ap=Fak_nm_) p ls(_ae_ mngr=Mngrap aae aae(p) d =SLlhm(p) b QAceyap mgae=Mgaeap d) irt irt(p, b mngradcmad'b,Mgaeomn) aae.d_omn(d' irtCmad
  33. 33. tini bd yp snoitargim dda tig tini ppabew nohtyp
  34. 34. edargpu bd yp tini ppabew nohtyp snoisrev snoitargim dda tig etargim bd yp tini ppabew nohtyp
  35. 35. smroF dna sledoM :raf os rehtegot lla ti eiT
  36. 36. fo faketwfipr Fr rm ls.x.t mot om fo wfrsetsllhm.r ipr mdlfr rm tom.x.qaceyom mot oe_om casUe(bMdl: ls srd.oe) "" ue"" "A sr" _tbeae_='sr' _alnm_ ues i =d.ound.nee,piaykyTu) d bClm(bItgr rmr_e=re eal=d.ound.tig26,uiu=re mi bClm(bSrn(5) nqeTu) UeFr =mdlfr(sr bs_ls=om srom oe_omUe, aecasFr)
  37. 37. fo faketwfipr Fr rm ls.x.t mot om fo wfrsipr vldtr rm tom mot aiaos fo wfrsetsllhm.r ipr mdlfr rm tom.x.qaceyom mot oe_om UeFr =mdlfr(sr bs_ls=om fedag= srom oe_omUe, aecasFr, il_rs{ 'mi' { eal: 'aiaos:[aiaosEal), vldtr' vldtr.mi(] } , } )
  38. 38. fo fakipr Fak rqet rne_epae rdrc,ulfr rm ls mot ls, eus, edrtmlt, eiet r_o @p.ot(/eitr,mtos(GT,'OT) aprue'rgse' ehd='E' PS') dfrgse(: e eitr) ""eitranwue"" "Rgse e sr" oj=Ue( b sr) fr =UeFr(eus.om oj om sromrqetfr, b) i fr.aiaeo_umt) f omvldt_nsbi(: fr.ouaeojoj ompplt_b(b) d.eso.d(b) bssinadoj d.eso.omt) bssincmi( rtr rdrc(r_o(rgse') eun eietulfr'eitr) rtr rne_epae'epaehm' eun edrtmlt(tmlt.tl, fr=om omfr, uesUe.ur.l() sr=srqeyal)
  39. 39. )smaerts rehto dna( stneve gnittimE
  40. 40. srotareneG :ruoteD
  41. 41. dffbnci) e ioac(: ""eeaea ifnt Fbncisqec"" "Gnrt n niie ioac eune" a=0 b=1 wieTu: hl re yedb il c=a+b a=b b=c
  42. 42. frii fbnci) o n ioac(: piti rn #WRIG wl nvred ANN: il ee n!
  43. 43. dffbncin1) e ioac(=0: ""eeaea neeet o teFbncisqe "Gnrt n lmns f h ioac eun a=0 b=1 frii xag() o n rnen: yedb il c=a+b a=b b=c
  44. 44. frii fbnci) o n ioac(: piti rn
  45. 45. !esnopser eht time ot rotareneg a esu nac eW
  46. 46. fo fakipr Rsos,sra_ihcnet rm ls mot epne temwt_otx @p.ot(/vnssra' aprue'eet/tem) dfgteet(: e e_vns) ""eunasra o eet"" "Rtr tem f vns" @temwt_otx sra_ihcnet dfgnrt(: e eeae) "" " Agnrtrta rtrsasnl JO-noe eeao ht eun ige SNecd eet floe b a epyln. vn, olwd y n mt ie "" " wieTu: hl re yedgteet) il e_vn( rtr Rsos(eeae) eun epnegnrt(, mmtp=tx/vn-tem) ieye'eteetsra'
  47. 47. dedaerht revresnur yp tini ppabew nohtyp txt stnemeriuqer r llatsni pip elpmaxe tuokcehc tig
  48. 48. !tiaw tuB
  49. 49. .hsinif reven lliw tseuqer enO .stseuqer hsinif yllufecarg lliw ksalF
  50. 50. fo faketsrp ipr (aae, rm ls.x.cit mot Mngr Sre a SreCmad evr s evromn casSre(evromn) ls evrSreCmad: dfhnl(ef *rs *kag) e adesl, ag, *wrs: aprnig=Tu p.unn re sprSre,sl)hnl(ag,*kag) ue(evr ef.ade*rs *wrs pit"htigdw" rn Sutn on aprnig=Fle p.unn as mngradcmad'usre' Sre) aae.d_omn(rnevr, evr
  51. 51. fo Qeeipr Qee rm uu mot uu qee=Qee) uu uu( @temwt_otx sra_ihcnet dfgnrt(: e eeae) "" " YedJO-noe eet il SNecdd vns "" " wieaprnig hl p.unn: ty r: ie =qeegttmot1 tm uu.e(ieu=) yedfra_esgs[tm) il omtmsae(ie] qeets_oe) #etteqeeie uu.akdn( a h uu tm ecp Epy xet mt: ps as
  52. 52. edis rehto eht daeha tpircsavaj :gninraw
  53. 53. <il>vn Sra<tte tteEet tem/il> <li=msae" u d"esgs> { frmsaei gtfahdmsae( % % o esg n e_lse_esgs) } <i{ msae}<l> l>{ esg }/i { edo % % nfr } <u> /l <citsc"sai/oe/qeyj"tp=tx/aacit>/cit srp r=/ttcbwrjur.s ye"etjvsrp"<srp> <citsc"sai/oe/qeyeetorej"tp=tx/aacit>/c srp r=/ttcbwrjur.vnsuc.s ye"etjvsrp"<s <cittp=tx/aacit> srp ye"etjvsrp" $dcmn)rayfnto( { (ouet.ed(ucin) $eetore{ .vnsuc( lbl 'onc' ae: cnet, dtTp:'sn, aaye jo' ul 'eet/tem, r: /vnssra' msae fnto(aa { esg: ucindt) $ec(aa fnto( { .ahdt, ucin) $'l>,{tx:ti }.pedo'msae'; (<i' et hs )apnT(#esgs) }; ) } } ) }; ) <srp> /cit
  54. 54. citatstcelloc yp tini ppabew nohtyp oi rewob ptth B N llatsni rewob txt stnemeriuqer r llatsni pip elpmaxe tuokcehc tig
  55. 55. stekcos-ksalf/ztierhtennek/moc.buhtig ta kool nac uoy stekcosbew otni er'uoy fI
  56. 56. tset.yp htiw gnitseT
  57. 57. yp.tsetfnoc/stset ipr pts mot yet fo wbp ipr (p a fakap rm eap mot ap s ls_p, d a fakd) b s ls_b @yetfxuesoe'eso' pts.itr(cp=ssin) dfd(: e b) ""e u tedtbs"" "St p h aaae" fakapcni[TSIG]=Tu ls_p.ofg'ETN' re fakapcni[SLLHM_AAAEUI]=.. ls_p.ofg'QACEYDTBS_R' . fakd.rpal) ls_bdo_l( fakd.raeal) ls_bcet_l( rtr fakd eun ls_b @yetfxuesoe'eso' pts.itr(cp=ssin) dfapd) e p(b: ""e u teFakts cin"" "St p h ls et let" rtr fakapts_let) eun ls_p.etcin(
  58. 58. yp.sweiv_tset/stset "" " apadd aeaalbei tets soe p n b r vial n h et cp "" " dfts_ne(p) e etidxap: ""etIcngtteidxpg"" "Ts a e h ne ae" r =apgt'' v p.e(/) pitr.aa rn vdt asr 'tte'i r.aa set <il> n vdt
  59. 59. stset tset yp txt stnemeriuqer r llatsni pip elpmaxe tuokcehc tig
  60. 60. tfihSnepO ot gniyolped
  61. 61. nohtyp/srepoleved/moc.tfihsnepo.www noitacilppa.noitacilppa.igsw snur tfihSnepO stseuqer evres oT yp.putes level-pot ruoy snur tfihSnepO yolped oT
  62. 62. yp.putes ipr o mot s fo stpol ipr stp fn_akgs rm eutos mot eu, idpcae POETRO =o.nio.e(OESITRP_I' RJC_OT sevrngt'PNHF_EODR, o.ahdraeo.ahasah_fl_)) spt.inm(spt.bpt(_ie_) wt oe(spt.onPOETRO,'eurmnstt) a fl_ ih pno.ahji(RJC_OT rqieet.x') s ie: rqieet =[e.ti( frrqi fl_xedie(] eurmns rqsrp) o e n ie.ralns) stpnm=eape, eu(ae'xml' vrin'.' eso=00, ato=Dnel Mdly, uhr'aile aee' ato_mi=dnel@aee.da' uhreal'ailemdlyi.u, ul'tp:/ihbcmdnilnxcn-ufaktt' r=hts/gtu.o/an/iu-ofa-ls-ue, dsrpin'xml dpo t OeSit, ecito=Eape ely o pnhf' isalrqie=eurmns ntl_eursrqieet, )
  63. 63. yp.noitacilppa/igsw ipr o mot s ipr ss mot y #adlclcd d oa oe sspt.pedo.ahji(sevrn'PNHF_EODR]) y.ahapn(spt.ono.nio[OESITRP_I') #iiils vrulevrnet ntaie ita niomn vrev=o.nio[OESITHMDR]+'yhn27vrev' itn sevrn'PNHF_OEI' pto-./itn/ o.nio[PTO_G_AH' =o.ahji(itn,'i/yhn./iep sevrn'YHNEGCCE] spt.onvrev lbpto27st-a vruln =o.ahji(itn,'i/ciaeti.y) itaev spt.onvrev bnatvt_hsp' ty r: eefl(itaev dc(_ie_vruln) xcievruln, it_fl_=itaev) ecp IErr xet Oro: ps as #ipr adcniueWG apiain mot n ofgr SI plcto fo wbp ipr apa apiain rm eap mot p s plcto
  64. 64. ap=Fak_nm_) p ls(_ae_ apsce_e =o.nio.e(OESITSCE_OE' p.ertky sevrngt'PNHF_ERTTKN, 'HSI RAL SCE' TI S ELY ERT) apcni[SLLHM_AAAEUI]= p.ofg'QACEYDTBS_R' o.nio.e(OESITPSGEQ_BUL, sevrngt'PNHF_OTRSLD_R' 'qie//.apd' slt:/./p.b) ty r: apsai_odr=o.ahji(sevrn'PNHF_EODR] 'si p.ttcfle spt.ono.nio[OESITRP_I', wg' ecp Kyro: xet eErr ps as
  65. 65. retsam elpmaxe a elpmaxe ecrof chr hsup tig lqsergtsop c dda egdirtrac chr tig elpmaxe tig elpmaxe hss chr dda etomer tig tig on nohtyp elpmaxe etaerc ppa chr )tfihsnepO pu tes ev'uoy gnimussa(
  66. 66. lmth.etalpmet lmth.stneve setalpmet yp.citatstcelloc yp.sweiv yp.sledom yp.reganam yp.__tini__ ppabew yp.eganam yp.putes
  67. 67. reggiB gnitteG rehtruF gnioG
  68. 68. sweiv desab ssalc fo fakvesipr MtoVe rm ls.iw mot ehdiw casUeAIMtoVe) ls srP(ehdiw: dfgtsl) e e(ef: ues=Ue.ur.l( sr srqeyal) .. . dfps(ef: e otsl) ue =Ue.rmfr_aarqetfr) sr srfo_omdt(eus.om .. . apadulrl(/sr/, p.d_r_ue'ues' ve_ucUeAIa_iw'sr') iwfn=srP.sve(ues)
  69. 69. stnirpeulb fo fakipr Bupit rne_epae rm ls mot lern, edrtmlt sml_ae=Bupit'ipepg' _nm_, ipepg lern(sml_ae, _ae_ tmlt_odr'epae' epaefle=tmlts) @ipepg.ot(/pg>) sml_aerue'<ae' dfso(ae: e hwpg) rtr rne_epae'ae/shm'%pg) eun edrtmlt(pgs%.tl ae <pg> /ae
  70. 70. stnirpeulb fo fakipr Fak rm ls mot ls fo mdl.ipepg ipr sml_ae rm ouesml_ae mot ipepg ap=Fak_nm_) p ls(_ae_ aprgse_lern(ipepg) p.eitrbupitsml_ae
  71. 71.  flickr.com/photos/mau3ry/376364 niugnepinn ad inn ad/gro.emong.sgolb )-; nif  etut-ksalf-ua-fnoc-xunil/innad/moc.buhtig

×