Python Flask Notes
Python Flask Notes
Corey Schafer
Python Flask
-
a
program in Python for web development
INITIATION
the Flask class
from flask import Flask < importing
app =
Flask)--name ) Creating __
an "app" variable , setting
it to an instance of this Flask class
-
-
when run directly w/ Python , --name----- main - - visit next page for example
INITIALIZES YOUR FLASK APPLICATION SO IT CAN BE USED FOR ROUTES THELPS FLASK KNOW WHERE TO ,
route decorators help write functions that show info on certain roulers
&app (")")
J-router
route
decorators : both ways URL)
.
↓
↓
O
↑
text too small
I-creating
& app . roure ("/about")
def about 1) :
the roue decorators for the About page
Py
flask run
type intoterminal :
-
flask run
"Debugger
"
-
Method 2 :
-
if -name-----main-- :
J-only true when run directly through Python (like this) (visit previous page)
app . can
(debug True) = -
running app variable (website) in
debug mode
PYTHON FLASK-2
-
the flask .
py contains 2 roules , home + about ,
and w/ more routes lots of HTML code would
overlap
-
so templates can be used (by creating a templates directory within the project folder)
PYTHON-flask by
FLASK ~ home html
.
= home route template (info unique to the home roure)
-
TEMPLATES
I about html =about
template (info unique route
:
S
route to the about
&
main css ,
flask py -
info
importing render-template to use
I
def home 1) :
-
return render -
template (about html) .
I-rendering
from
the flask
about htm/ (a
.
website's
template)
template
↑
code for ctic
a
(non-changing , fixed) HTML
page
DUMMY DATA
flask PY .
-
a list of dictionaries containing
data about
dummy posts
home html .
&% for post in posts you for loop to print posts one at a time
I
< H1)[E post , tiHe 33 </h1)
<p >
By &G post . author 33 on ES Post date-posted 33 /P
,
< > - for each post within the posts date ,
home .
html/about : h+m/
is then
2%
1
else %3 if no title is provided ,
then the title will be "Flask Blog
< titles Flask Blog > /titles
G
default title
TEMPLATE INHERITANCE
it's inconvenient to their shared code needs to be made ,
update all files (templates) when a
change in
-
home html .
and about Html . will inherit from layout utml .
I
layout . htm) (SHARED CODE) BLOCK
.
< DOCTYPE HTM1 > -creating a "content" block
2 o if title % z
body >
< title > Flask Blog &d title 33 /titles html)
(inheriting from layout
-
<
home Html .
.
word
9% else %b using the key
</ha)
<h 1) About Page
8 % endblock content % 3
BOOTSTRAP
-
layout , him/
contains
styling elements
for interactive
contain JavaScript
components (dropdown menus , etc . )
-
can run it using
adiv class = "container"s and < /div > around the content block
RESULT
F E
ADDING A NAV BAR
helps define intro content+ set new nav links thelps w/ customization
functional
centers content
creating +
styling the nav bar -creating a
navigation bar
styles link adds
, margins and adds Home Link
,
links
login a register right of
nav on the
creating
the hav bar
SIDEBAR
clarifies "main" content + adds horizontal padding for a consistent layout
creates horizontally aligned columns
-creating a sidebar w/
creating a class that will take $12 of (holding main content)
[
up parts the row or screens
starting content block
creating a column that will take up 4/12 parts of the row on screens (holding sidebar content
columns using Bootstrap
a custom class for styling purposes
replaces
-
a heading for the sidebar section
a
lighter color text
creating a list group to display content
"Container" >
,
elements
/div >
CUSTOM STYLES/MAIN .
CSS)
contains CSS code for custom fonts colors designs a styles
-
, , ,
-
in order to import to layout . Html (layout template) ,
the Flask function , url- for needs to be used
url-for will find exact location of routesruns behind the scenes)
-
flask py .
↓
from flask import Flask ,
render-template for
layout .
him
clink rel =
"Stylesheet" type "text/ess" =
uref = &G url-for I'static' filename ,
=
'main css') 33
.
home Utm/ .