Effective PyCharm Transcripts
Chapter: Server-side Python web apps
Lecture: Creating server-side projects
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's go and create a web application, a server side Python based web app in PyCharm. Now, just like before I could go and find a folder and drop it in
0:10
here and just start writing code. But with the web apps sometimes there's a little more setup and configuration and it might
0:17
actually be easier to start from here to be honest. I still often start from scratch if I'm doing something like Flask or FastAPI but
0:23
if I was doing pyramid, I would start with either this or maybe cookie cutter So we go over here,
0:28
we click new project and then these are service side web apps. We could do Django and you can see what's going to create a virtual environment wherever
0:36
we want that to go or pick an existing template. And then if you expand out this more settings,
0:42
you can see we could pick the template language to be either Django templates or Jinja2 templates, name the folder,
0:49
give the application name and decide whether or not. To enable the admin backend. We go over to pyramid. The settings are a little bit different.
0:57
Same thing virtual environment. But then we give it a name. We choose from its selection of template languages,
1:04
Chameleon my favorite. And you can even choose the database back end. Is it ZODB, SQLAlchemy or bring your own,
1:11
you know, Mongo DB or whatever you want to write. But we're going to do Flask.
1:15
I think Flask is the right mix of really popular and well known and not too
1:19
complicated. So we're going to create one of these and really Jinja2 to is the
1:24
primary option here create a new virtual environment and I've already copied the path into our GitHub repository into this web server side thing.
1:33
And let's give it a name, first flask or something like that. Let's go see what we get. Did you see that? It was actually downloading flask.
1:46
So in addition to just creating the files, the sort of starter files and start a structure here. It also realized that we depend upon Flask.
1:54
So it automatically added that. Let's go ahead and let it add what it thinks it needs to run here. So it's created a few things you can see our
2:02
'app.py' this is your sort of Hello World flask app. And quite literally it says Hello world.
2:08
And then we have our static folder where we could put stuff like if we wanted
2:11
we could come over here and make a directory called CSS and into the directory
2:16
we could put something like 'site.css' when you create our HTML templates, we're gonna put them over here. So let's go out and do that.
2:26
Let's convert this from Hello World to actually use template and we can see some
2:31
of the cool features. So notice it's already detected that it's a Flask app. If we hit the edit configuration,
2:38
you can see it has certain things like here's the target. We're going to use this Flask Environment Flask debug and so on.
2:45
So it's going to use the Flask run style instead of running this app directly. Although you could run it with this right here.
2:54
So we go, you can see up here that it shows the settings, here's the app, here's the environment and here is whether or not the bug is
3:00
turned on and let's just click it. See what we get. Hello world. A little underwhelming. Not the most incredible thing in the whole wide world,
3:07
is it? Let's go and actually add something to do with templates. Some HTML because really that's the way things are supposed to be done.
3:17
So first of all this is forward slash(/) I like to name this index and just say a little flask,
3:24
something like that. And then we could add some sort of secondary thing, but let's just start by having an index or here so we can add HTML5
3:34
call this index. I like this method name to match the file name. Right, So we can set the title and let's just go put an H1 its
3:43
capital expand that will say hello to the flask. World from PyCharm now over here. We need to do something like Flask,
3:57
render template. What are we gonna do? We got to give it a template name.
4:02
Right, check this out. I told you there's this cool integration between the template folder. If it's marked as a template directory.
4:09
That's the purple in the main framework. So I go over here at this command space. It auto completes. Why did auto complete?
4:16
Well there's only one option there. If I get it to type it out of my type,
4:19
I see it knows into that templates folder that this is the one if there's subdirectories
4:25
it knows about those. So we can come over here make a new directory called call this home when you put this in there.
4:33
For example, PyCharm actually automatically did that. Re factoring when I did that file move,
4:42
which is pretty awesome. But let's just go and type it again just to see what happens. So if I type home then I type this,
4:48
see it auto completes and then right here there's this to allow us to jump over
4:53
there and we can jump back so we can navigate super quick between this view method
4:58
and that. Let's make sure things still work the first time that you run this it's going to say we already have this app running you can see is running
5:06
down here, we're gonna have to start over in order for it to take up the changes that we did and that's just how Python works.
5:12
That's how we can't share port things like that. So we'll just help to do this now. If we hit this, check it out.
5:18
Hello to the flask world from PyCharm. Super. Cool. Right, so we're now using this template,
5:25
The final thing that might be interesting to do here is to pass some sort of data. So let's just go over here and put a random number.
5:34
So we'll put a number, this is going to be something from random which we can import create a random randint between 50 and 100.
5:44
We'll just say number=num. So we'll pass these keyword arguments.
5:48
These will appear over here as variables that we can either work with through Python expressions
5:54
or turn into strings. So I'll put a little div and then we'll say your number of the day is and in Jinja we do a double curly brace that will
6:05
tell you were about to take a variable, it was passed over and turn it into a string.
6:09
What we're gonna do, we're gonna work with number that auto complete all that is cool. So let's rerun it again, go back over here, refresh Your day,
6:21
your number of the day is 100 incredible. You got 93 52 69 59 awesome. So is it impressive? No, is it a good start. Absolutely.
6:33
The thing I want to have you take away here is this deep integration notice we
6:38
have automatic, we have auto complete for things like four and then I guess maybe more easy. See as if we haven't and an end for we have all these
6:49
sort of auto complete type things for the Jinja language which that doesn't make sense what
6:55
I wrote but shows you some Auto complete as well as the fact that this data there is being passed over. Notice our variables is num,
7:02
but the keyword argument is number and that's what it auto completed when I typed in
7:07
Super, super cool. So the integration and just getting us off on the right foot pretty awesome.