Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Flask - Python web microframework

Flask - Python web microframework

uvod u Flask

Zdeslav Vojkovic

November 24, 2012
Tweet

More Decks by Zdeslav Vojkovic

Other Decks in Programming

Transcript

  1. FLASK: ISPOD HAUBE Jinja2: template biblioteka Werkzeug: WSGI (PEP333) <

    1000 linija koda detaljna dokumentacija čega nema: ORM, Forme... $ p i p i n s t a l l F l a s k
  2. FLASK U PRAKSI f r o m f l a

    s k i m p o r t F l a s k a p p = F l a s k ( _ _ n a m e _ _ ) @ a p p . r o u t e ( ' / i n d e x ' ) d e f h e l l o _ w o r l d ( ) : r e t u r n ' H e l l o W o r l d ! ' i f _ _ n a m e _ _ = = ' _ _ m a i n _ _ ' : a p p . r u n ( ) ( e n v ) $ p y t h o n t e s t a p p . p y * R u n n i n g o n h t t p : / / 1 2 7 . 0 . 0 . 1 : 5 0 0 0 /
  3. ANATOMIJA VIEW METODE @ a p p . r o

    u t e ( ' i n v o i c e / n e w ' , m e t h o d s = [ ' G E T ' , ' P O S T ' ] ) d e f n e w _ i n v o i c e ( ) : i f r e q u e s t . m e t h o d = = ' P O S T ' : i n v c = I n v o i c e ( g . u s e r _ i d , r e q u e s t . f o r m ) d b . s e s s i o n . a d d ( i n v c ) r e t u r n r e d i r e c t ( u r l _ f o r ( ' s h o w _ i n v o i c e ' , i n v o i c e _ i d = i n v c . i d ) ) r e t u r n r e n d e r _ t e m p l a t e ( ' n e w _ i n v o i c e . h t m l ' ) @ a p p . r o u t e ( ' / i n v o i c e / < i n t : i n v o i c e _ i d > ' ) d e f s h o w _ i n v o i c e ( i n v o i c e _ i d ) : i n v o i c e = I n v o i c e . q u e r y . g e t _ o r _ 4 0 4 ( i n v o i c e _ i d ) r e t u r n r e n d e r _ t e m p l a t e ( ' i n v o i c e . h t m l ' , i n v o i c e = i n v o i c e )
  4. TEMPLATES Konverzija u markup (autoescaping) Kompleksna logika Flash poruke Globalne

    varijable (g, request, session...) { % e x t e n d s " l a y o u t . h t m l " % } { % b l o c k b o d y % } { { g . u s e r _ n a m e } } < u l c l a s s = e n t r i e s > { % f o r e n t r y i n e n t r i e s % } < l i > < h 2 > { { e n t r y . t i t l e } } < / h 2 > { { e n t r y . t e x t | s a f e } } { % e l s e % } < l i > < e m > U n b e l i e v a b l e . N o e n t r i e s h e r e s o f a r < / e m > { % e n d f o r % } < / u l > { % e n d b l o c k % }
  5. KONFIGURIRANJE f r o m f l a s k

    i m p o r t F l a s k D E B U G = T r u e S E C R E T _ K E Y = ' d e v e l o p m e n t k e y ' a p p = F l a s k ( _ _ n a m e _ _ ) a p p . c o n f i g . f r o m _ o b j e c t ( _ _ n a m e _ _ ) a p p . c o n f i g [ ' T E S T I N G ' ] = T r u e a p p . c o n f i g . u p d a t e ( T E S T I N G = T r u e , S E C R E T _ K E Y = ' . . . ' ) # a p p . c o n f i g . f r o m _ e n v v a r ( ' A P P _ S E T T I N G S ' , s i l e n t = T r u e ) # a p p . c o n f i g . f r o m _ p y f i l e ( ' a p p l i c a t i o n . c f g ' , s i l e n t = T r u e )
  6. MIDDLEWARE Alternativa: signali i m p o r t s

    q l i t e 3 f r o m f l a s k i m p o r t g @ a p p . b e f o r e _ r e q u e s t d e f b e f o r e _ r e q u e s t ( ) : g . d b = s q l i t e 3 . c o n n e c t ( ) @ a p p . t e a r d o w n _ r e q u e s t d e f t e a r d o w n _ r e q u e s t ( e x c e p t i o n ) : g . d b . c l o s e ( ) d e f l o g _ r e q u e s t ( s e n d e r , * * e x t r a ) : s e n d e r . l o g g e r . d e b u g ( ' R e q u e s t c o n t e x t i s s e t u p ' ) f r o m f l a s k i m p o r t r e q u e s t _ s t a r t e d r e q u e s t _ s t a r t e d . c o n n e c t ( l o g _ r e q u e s t , a p p )
  7. STREAMING Potencijalni problemi: WSGI middleware, upload datoteka @ a p

    p . r o u t e ( ' / l a r g e . c s v ' ) d e f g e n e r a t e _ l a r g e _ c s v ( ) : d e f g e n e r a t e ( ) : f o r r o w i n i t e r _ a l l _ r o w s ( ) : y i e l d ' , ' . j o i n ( r o w ) + ' \ n ' r e t u r n R e s p o n s e ( g e n e r a t e ( ) , m i m e t y p e = ' t e x t / c s v ' )
  8. TESTIRANJE f r o m t e s t a

    p p i m p o r t a p p c l a s s F l a s k T e s t C a s e ( u n i t t e s t . T e s t C a s e ) : d e f t e s t _ i n v o i c e _ a c c e s s ( s e l f ) : a p p . t e s t i n g = T r u e # b o l j e d o d a t i u k o n f i g u r a c i j u w i t h a p p . t e s t _ c l i e n t ( ) a s a p p : r v = a p p . g e t ( ' / i n v o i c e / 3 ? f m t = j s o n ' ) a s s e r t ' I n v o i c e 1 - 0 0 1 ' i n r v . d a t a a s s e r t r v . s t a t u s _ c o d e = = 2 0 0 a s s e r t f l a s k . s e s s i o n [ ' l a s t _ i n v o i c e ' ] = = 3 a s s e r t r e q u e s t . a r g s [ ' f m t ' ] = = ' j s o n '
  9. "NOT INVENTED HERE" Ekstenzije Baze podataka: SQLAlchemy, Mongo, CouchDB... Development:

    DebugToolbar, Testing, Exceptional... A&A: Login, Principal, OAuth, OpenID WSGI middleware f r o m f l a s k . e x t . s q l a l c h e m y i m p o r t S Q L A l c h e m y f r o m f l a s k . e x t . r e s t l e s s i m p o r t A P I M a n a g e r a p p = F l a s k ( _ _ n a m e _ _ ) d b = S Q L A l c h e m y ( a p p ) m a n a g e r = A P I M a n a g e r ( a p p , f l a s k _ s q l a l c h e m y _ d b = d b ) # P r o d u c t j e S Q L A l c h e m y D B m o d e l m a n a g e r . c r e a t e _ a p i ( P r o d u c t , m e t h o d s = [ ' G E T ' , ' P O S T ' , ' D E L E T E ' ] ) a p p . w s g i _ a p p = M y M i d d l e w a r e ( a p p . w s g i _ a p p )
  10. MODULARNE APLIKACIJE Blueprints b o o k m a r

    k s = B l u e p r i n t ( ' b o o k m a r k s ' , _ _ n a m e _ _ ) @ b o o k m a r k s . r o u t e ( ' / ' ) d e f l i s t ( ) : r e t u r n o b j e c t _ l i s t ( ' b o o k m a r k s / i n d e x . h t m l ' , B o o k m a r k . s e l e c t ( ) ) @ b o o k m a r k s . r o u t e ( ' / a d d / ' ) d e f a d d ( ) : B o o k m a r k . g e t _ o r _ c r e a t e ( u r l = r e q u e s t . a r g s . g e t ( ' u r l ' ) ) r e t u r n r e d i r e c t ( u r l o r u r l _ f o r ( ' b o o k m a r k s . l i s t ' ) ) a p p . r e g i s t e r _ b l u e p r i n t ( b o o k m a r k s )
  11. FLASK VS DJANGO Django ORM vs SQLAlchemy/nešto treće Migracije Administratorsko

    sučelje Testiranje/debugiranje “while you were arguing about Rails vs Django vs Flask, a bunch of people shipped with PHP”