Building data-driven web apps with Flask and SQLAlchemy Transcripts
Chapter: Jinja2 templates
Lecture: Create the PyPI site
0:00 Before we can jump into our template example
0:03 we have to do one thing really quick.
0:05 We don't have a PyPI demo site yet
0:07 so we're going to actually create that now
0:10 and then we can add templates and Dynamic HTML to it.
0:13 So kind of a review from when we talked
0:15 about creating our first site
0:16 but we haven't actually done this formally for this project.
0:19 So let's just take two minutes and do that.
0:21 So over here in the Jinja template we're going to create
0:23 a final thing, we have started from scratch
0:26 so this one has no starter. But when we go over here
0:31 and we're going to first create our virtual environment.
0:33 See the command that's actually running at the top
0:35 but just Python 3 create a virtual environment and activate it.
0:39 And then we have this here, just, remember what we did
0:44 we wanted to have the overall part that manages this thing.
0:47 So we might have like a requirements.txt
0:51 also had a dev for one of those.
0:53 We might have setup and readme's
0:55 and all those kind of things.
0:56 And then we're going to have a pypi_org
1:00 or then this is going to be our actual web content.
1:03 And here we'll have things like the static folder.
1:05 The views. Templates, most importantly for this one.
1:11 We had other stuff, but we're not going to add that yet.
1:13 And let's go ahead and add the app.py here as well.
1:17 So if we go back we can now open this in PyCharm.
1:21 And the quickest and easiest way to do this on mac OS
1:24 is to go to the directory that contains
1:26 the virtual environment
1:27 and dragging and dropping that onto PyCharm.
1:30 On Windows and on Linux you have to go up here
1:33 and say file, open, directory.
1:34 But hey we get a little shortcut, that's cool.
1:36 So here we can see we've got our templates folder
1:38 that's already kind of detected.
1:40 We've got our app
1:41 let's go ahead and add our requirement of Flask.
1:44 Make sure we've detected the virtual environment
1:48 so let that go as I've actually get to it.
1:50 Here you can see, and we can say which Python.
1:53 On Windows say where, and you see that's actually
1:55 the one were looking for.
1:56 So we can install Flask.
1:59 IP list is empty, install Flask.
2:08 If we look, all the Flask stuff is here.
2:09 Okay great.
2:10 And that's because PyCharm automatically detected
2:12 the requirements file and so on.
2:14 So we'll import Flask, this is super quick
2:16 because you've done this before.
2:18 We'll say app = flask.Flask(__name__)
2:24 like so. And then we'll say @app.route /index
2:31 return hello world.
2:34 And here we can just say app.run
2:35 but lets be little bit better about it, we'll say
2:38 do the main example.
2:39 So if __name__ == '__main__' and we'll say app.run.
2:43 We're going to add a whole bunch more good stuff
2:45 to the startup here.
2:47 Little cleanup, let's just run this
2:49 and make sure that everything runs.
2:51 Also, let's mark that as a sources route
2:53 so these are all relative to each other.
2:57 I pressed a bug, didn't mean to but whatever
2:59 it also works so it says we're running production
3:01 because we're not setting a couple of settings
3:03 which we can do. Let go and run it.
3:06 Hello world. Looking good huh? All right.
3:11 So we can mould our app and say, debug=true for now.
3:14 Of course you want to put that in a configuration.
3:17 We rerun it.
3:19 You'll see now, the debugger's on which is nice
3:22 'cause if we make a change, it automatically detects
3:25 and reloads this, so.
3:27 All right so we're in good shape, we're not quite done
3:29 but we're making progress
3:31 for getting our app up and running.
3:34 Now, we're going to actually start working with data
3:36 and using these templates to generate Dynamic HTML
3:39 using Jinja 2 and our models and all that good stuff.