So let's do a quick introduction of flask.
I'm assuming you know Python I'm assuming you know she meant well flask is that glue in between both.
And it's really powerful is very simple at first but it can be as powerful as you want but it's wonderful.
I use it all the time and I wanna make sure I'm going to use in this course I want to make sure we're
on the same page so I may jump to Wikipedia though a flask with framer and read the first paragraph
flask is a micro web framework written in Python is classified as a micro framework because it does
not require particular tools or libraries it has no database abstraction layer form validation or any
other components where pre-existing third party libraries provide common functions.
However flash supports extensions that can add application features as if they were implemented in flask
itself extensions exist for relational mappings form validation upload handling various open authentication
technologies and several common framework related relate tool.
So basically you can add all sorts of things.
What they're saying here is that it's a micro framework meaning as a small you start with something
very small it's very light.
That means it's nimble it runs fast and take a lot of memory and just add what you need right.
If you want a database will you add a database to the library and it can suddenly run with run with
a light with a database but you'll need to if you don't need it you don't need it right.
So we're gonna be looking at two simple examples we're not gonna add any of that stuff we're gonna keep
it simple went out a few things but we keep it simple.
Honestly it's like the first building block when you're customizing something and you just add what
you need.
And here mentioned that that you know Pinterest use it linked and use it.
So it's powerful.
It's very popular.
It's supposedly the saying it's the most popular web framework in Python so it means it's more popular
than Django.
I've worked with active search pages when I was at Microsoft for many years.
This is so much nicer.
It's simple it's easy it's lightweight act sort of pages it was big was bloated right.
I mean I like it too but but definitely this is no flask.
I love flask so.
But before we dive into these examples what is a web server that's important.
So we all know what web hosting service is actually.
I got an example here every github account gets this GitHub that I owe is basically one a free account
where you can host a site I've been hosting my my data science site for six years I get a lot of traffic
here people people really appreciate this I love it too unfortunately it's a web hosting service it's
not a web surfing service meaning I don't control the backend I can't even run a YouTube video here
I can even put that into code so you have very little control of what's going on.
So if you have a web server platform meaning you can do a lot more customization and you can do real
real dynamic custom content so meaning let's second example let's say I visit a site a Web server site
knows who I am either through cookies or I logged in and and it knows I like the stock market it's going
to show me stock market stuff rise stock market charts news and I say other people like Suzy and Paul
they they they don't like stock market but they like sports when they go it's not going to show on the
stock market it's going to show them a sports right that's dynamic content I need a web server to show
that dynamic content more important a simple example is like banking is I like that right you want make
sure that when I go in I don't see other people's accounts and I don't pay other people to see my account
right.
It's all dynamic web surfing so flask is very powerful and you can do a lot of things and it's really
a glue.
What I really like about it really is that it's you can write your stuff in Python use flask and you
can easily then export your findings your models whatever it is you're doing in Python to the Web it's
super easy so that that's what I like.
So like I said flask comes with the bare minimum we're going to be we'll be using a very simple example
extremely simple.
You're going to see how simple it is.
So let me jump to oh and my entire site viral email dot com is all flash driven.
And if you go to the tools button I have some SAS applications that do.
Machine learning and modeling they're all SAS driven rights so they're all flask driven now using flask
to show that information so very powerful but you really can do anything as much as you want to do.
So you get more information in a Drupal notebook the extract will take a very simple example.
EMMA Schmidt take it directly from the this one the pilot projects dot com P for flash flask.
The link is in the notebook.
The first time you're using we're running flash your flash.
You'll have to install it.
So it's a pip install flask.
However you normally install it on your local machine whether it's your in Windows Unix Linux or Mac
right use the right command but you need to install the flask libraries is super simple like I say it's
kind of lightweight so it's easy to do.
So what we're going to do is we're going to open up you know open up your favorite.
Here let me just start from scratch here.
I mean remove this so we can do it all together there.
So open up your favorite text editor notepad plus plus or you know sublime in my case and I should do
that.
That's the next example I won't do it here there and you'll see this code here.
So copy this code after you install a flask on your local machine.
Copy this example this example it's the same example from this site here so you can do either one does
it matter here.
This is pretty I mean take that one.
I'm going to paste it and they were they they asked you to save it as actually I might kill this and
I'm to open a new one.
Yeah.
They say to save it as hello uh P Y.
Load up p y.
Here we go.
Save it.
And this is basically a flask web application.
It's very simple but is the real deal.
You're importing flask.
We import a few other things that escape your characters and requests because it allows you to show
how it is you can.
In India into your well we'll be able to put a variable is going to read it to Nesta request object.
More importantly you have to instantiate flask you pass it for instance and you call flask and you assign
to app the only part of you to use this app is here.
It's for the routing.
So the routing means that whenever you have a U.R.L. right it could say hello it could say you know
index or HMO.
Right.
This is this is where flask was going to root whatever traffic comes to the right to function in this
case is just you know the route you and in and it's gonna be routing it to this function call hello.
And there is going to get the name if there is one.
And it's going to return Hello it's gonna return of strength.
We're not going to create a dynamic web page.
We're not gonna pull a template but we are creating a dynamic dynamic web page simply passing it a string
and you'll see how this works.
So save this as Hello.
Pete y and we'll follow the instructions here.
It says you know we're gonna gonna run it in our environment.
You call environment.
Indeed a flask on a score app and you know equals held up your wife classical runs.
That's kind of a command you would normally do it if you don't want to have you if you don't have virtual
environment you don't wanna run it in a new environment.
Nick's example doesn't he doesn't use it.
I usually don't use it because I am using flask all the time.
So then you open your command window any command window and make sure you're pointing to this flat to
the fire.
We just create.
Here it is Hello.
Hello.
P Y.
And we're going to call the environment flask on the score app.
We pass it.
Hello P Y flask.
And we want to run it this game just kind of copying what was an example and it's going to return a
you are real.
Here it is it's 120 Sim it's a local your own that's fine we're really just serving to ourselves not
to the cloud quite yet and we'll do that and other examples.
And here it is.
Hello world.
And as you can see there is a code there is the you know there is the ability of taking a request a
request variable.
So here all we have to do is you know is added to the end.
I think we have to do this as it
name equals.
And I put my name right.
And here it is.
Hello.
Manual right.
So super simple.
But this is a dynamic which we just dynamically served content where we gave it the name and it posted
that name a simple Web page that the static can't do that right.
So that's pretty cool right.
That's a very simple functioning of course.
That's not very useful.
The main power of flask is to work with templates and that's what we're gonna see next.
Let me jump back to here but there is another way you can run a faster way to run this and away way
if you don't want to run it in the in a virtual environment.
I'm going to show you how it is done.
So instead of calling the flask engine on the command line you can you can just default it right here.
So anytime you know anytime it knows that you're running locally it's going to call the app to run directly
here and there's an advantage of doing this is it's a bit easier when you're starting so to kill our
old instances control see.
And now all I've got to do is go python.
In my case I do Python 3 but however you call Python normally and hello but P wise I was a bit simpler
to call but the same deal is started and you know we have our Web page is still the same right I can
go viral them out there violent.
All right.
So this is a different way to run it.
And you can see this this way in the notes as well.
Okay but still this is still overly simplistic right.
The wished we were still not using the concept of templates and that's really the power of flash.
So let's keep going here in this example closing it down is control C and now let's build the structure
of a real flask web app web app.
Right.
All my web apps use a same kind of structure.
Let me stop this.
My my local serving of the web page and let me go here so briefly.
There is the brains the python file in that case we had held up.
Why don't you call mine main up b y but you can really call them however you want and it depends on
the cloud serving some need specific names some don't care or some you can customize it.
I usually go with made up why they'll be two folders underneath and they have to be named exactly like
this static which as a name implies whole static information.
So usually there's an images folder which is going to hold images but let's say you have a comma delimited
file or downloadable.
That's not changing.
You could put it in a static folder.
That means that you know if that information can be pulled directly from the A.T.M. and you have a templates
with an s at the end templates folder and that holds A.T.M. files that have a that are a mixture of
static HMO and you know placeholders for dynamic content that's important.
We're going to do a brief example right now.
So we're going to create this right now.
So go ahead open a you know a folder.
We're going to create one file called may not be why create underneath that folder create a static folder
and underneath create a templates folder I guess a lot of garbage here that we don't need.
Let me remove this.
Remove all the stuff there and and we'll I'm going to show you exactly how to do this.
So go back to your favorite you know edit text editor and let me remove all that so I can walk you through
it and we'll going to start with the maned P Y file.
So copy and paste the entire code to your to your main up UI file.
All right.
So what do we have here.
We have a few more things I don't need this escape someone to remove it.
We actually don't need to request but I'll leave it.
Because usually you need it.
Requests really allows you to you know format incoming data.
But this is a new one we're adding is called a render template and this is basically it takes a static
template that has placeholder dynamic place to place holders in it and knows how to process them.
So we adding this and because gonna me serving real dynamic content that's important right we want to
make this kind of useful I'm adding a day time.
So same deal right.
We we instantiate our we instantiate our flask.
We take the the app the are instantiated instance for the routing and also for for running it because
now we're running our instance locally right we don't have to do the command and a command line we're
doing it through here.
And I also have a debug flag to true it is a call out.
This will allow when we're running it if there's an error we're going to see the messages show up there.
So pretty cool stuff.
So but this is the important part here.
So hopefully you can see it.
Let me make it a little larger.
So this should be familiar with your python.
I'm simply calling the daytime object which I called here that library I'm asking for the universal
time and I'm formatting it in a clean way.
So we don't have all this.
We all have all sorts of milliseconds and all that stuff.
So now I have a representation of this time and I'm simply going to pass this to the templates.
So when you know we're calling a template so we have an HMO files and template we haven't built it yet.
When a built in the second and we're going to say you know when somebody comes to the root to your well
pass the dysfunction.
Create a time call this template a index that AML and pass it this variable and that's how you pass
things dynamically to a specific template and then it's going to to you know to show that template to
the requester.
It's that simple.
So next we need to go to we to create our index that A.T.M. file basically a template in this case.
And that's also super simple.
Just copy and paste this code and it's just simple typical Web page right as an e-mail a body.
We have you know an H1 tag where we are saying hello or universal time is and look at that double open
double curly bracket our dynamic variable double closed double quote this double double curly bracket
close and this basically is the where Python can inject its dynamic variable so you can have very complex
things.
It could be an entire web page it could be a full you know add an image right.
It could be all sorts and we'll see images how to create dynamic images in an action in some chapters
down the line.
But this is kind of in essence how you pass dynamic content content that's not that that's not known.
That is dynamic every time to a web page was really cool right.
And let's see how this works or save that save your main file as well.
Right.
And now bring back your command line and make sure I'm in the right place now and make sure you are
under you know your main folder where you have your mind up.
Why am I going to use my other method to start this up.
I will go Python 3 maned up why.
Hopefully it works if it doesn't work you'll see an error message like you saw earlier where there was
an error message and you can see that because we're saying debug true normally in production or anywhere
you don't want to show your error messages because it could reveal you know a backdoor or something.
Right so you want to turn that off.
But here it's fine for now for for testing because nobody can access it but us because it's a local
it's kind of not on our local system.
So I'm going to cut and paste this and paste it in here.
I'll look at that.
I says Hello Universal Time is and this is the actual Universal Time is dynamic if I'm running again
you guys here.
Time change 27 right.
You say that change.
Now on top of that put an image I forgot to mention the image but the image is something you just copy
and you were to copy an image and put it right here.
I put it in my static images folder.
So that's where an image would go and you can put anything you wanted to make sure you put the name
correctly from you know into the the index that H.M. file so it knows what to pull but that's basically
the structure the a skeleton of a well-formed folder structure for flask.
You have your brains the python that that that instantiating your flask instance and doing all the routing
a static folder if you have images or other stuff and a templates folder which has your template and
you can have you know as many temples as it needs.
What's the key here is that in this this template that the index or AML is called by this function.
So anybody coming by the route is automatically routed to hello to the function hello you create whatever
dynamic content you want in reality probably is something more complex you're reading a database you're
you're getting information from the web and then you call rent return or rent a template that means
go in the templates folder pull the index on a shemale file write right here and past is variable and
this variable should be here right it's right here.
Right.
So that's that's a bit how it works that's how that's the kind of a very brief very very very brief
primer flask flask can be very very complicated like I said I'm using them to create some very very
complex SAS tools where in this case you put your job that you have and it's going to compare it to
that fuzzy logic compared against data a data dataset and tell you the closest job and the probabilities
of it being automate in the future so you can do all sorts of things just classes read powerful but
we're going to use that next to do a contact form.