Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: Your first Pyramid site
Lecture: Demo: CLI starter site
0:00 Alright, we're going to begin creating our project
0:02 by simply opening up our terminal or command prompt
0:05 if you're on Windows.
0:07 Want to going to go to the right location.
0:08 So over here in our demos, this is the Git Repository
0:13 that you all have access to
0:14 created a thing called Chapter Four: First Site.
0:18 For each chapter I'm going to have a starter and a final bit.
0:22 So, starters whatever we happen to start with.
0:24 Later on, we're going to start with our app making major steps.
0:28 Like one point, it'll have a design added to it.
0:31 At one point, it'll have SQLAlchemy data access added to it.
0:35 We want to start from that point on the next chapter
0:37 so we'll kind of move along
0:38 so you always can kind of catch up
0:40 or start from the same place that I'm starting from
0:42 at any given chapter.
0:44 So over here we're going to create our site in this final
0:47 and the starter one, there's nothing to do yet.
0:49 We didn't start from anything.
0:50 This is from scratch
0:51 so we'll just have a blank folder there.
0:53 So let's change directories over here.
0:58 In this, you'll see I have oh-my-zsh.
1:00 It's detected this is a Git repository.
1:03 It doesn't really matter if your shell does that or not.
1:05 So what we want to do, is we want to make sure
1:07 that we have Cookiecutter installed
1:09 so that we can run the various templates.
1:12 So we'll say pip, maybe pip3, just to be safe.
1:16 Install, it will ask for an upgrade just in case
1:20 and I'll say --user
1:22 cause we find it just installs in my user profile.
1:25 I'm not a machine, which means we don't require sudo.
1:28 Then finally, cookiecutter.
1:31 I should already have it.
1:32 Looks like everything's up to date.
1:33 Yours might have installed it.
1:35 You can always just type cookiecutter
1:38 and see what you get, right?
1:42 Something like that. It shows you where it comes from.
1:44 Okay, excellent, we already have this installed
1:46 and ready to go.
1:47 So step one, make sure we have Cookiecutter installed.
1:50 We have that.
1:51 Step two, will be to use one of the Cookiecutter templates.
1:55 Now if we're over on the Cookiecutter site
1:57 on read the docs, you'll se that there's this thing called
2:00 pantry full of cookies; cookie cutters.
2:03 And there's tons of different things.
2:05 So heres a flask thing, heres a bottle
2:07 heres a pyvanguard; whatever the heck that is.
2:09 If you look for a pyramid
2:11 there's a whole section on these templates.
2:13 So here are a bunch that come from the pyramid team
2:16 and this one down here I actually added myself
2:20 for the Python for Entrepreneurs course.
2:22 The one we're going to use is this pyramid cookiecutter
2:24 which is just a Git repository here.
2:26 So we can just take the root of the Git repository
2:30 and we'll just come down here and say cookiecutter
2:33 and give it the full URL to the repo.
2:36 Not .git, not that, just the page basically.
2:41 It says we've already gotten this before
2:42 can we get a refresh version, yes.
2:44 It's going to ask some questions here.
2:47 It's going to say, "What is the name of your site?"
2:49 We're going to call this PyPI
2:51 cause that's going to be the name of our site.
2:53 Let's call this Python Package Index.
2:56 Alright, that's going to be our demo
2:58 throughout the whole class here.
3:00 It's going to say the name that we're going to use
3:03 in the Python Package in the folder structure stuff
3:06 "Is Python Package Index okay?"
3:08 Let's go with PyPI; something a lot shorter to type.
3:10 That says, "Great, we're almost ready.
3:12 "What template language do you want to use?"
3:15 Default to Jinja2, use to default Chameleon.
3:19 We're going to use Chameleon. You can make another choice if you want.
3:21 I'll tell you why later
3:22 why I think Chameleon's the right choice
3:23 but you can pick any one of these three.
3:26 Hit okay, and it's gone and created
3:28 a various project structure over here
3:30 so we could type tree and see what we got.
3:34 It's created this folder
3:36 which because you can see it's got all this kind of stuff.
3:39 It's has a setup.py.
3:40 This is actually a Python package
3:42 and here's the implementation of it.
3:44 It has the same name always.
3:47 Then there's a __init__.py for the package.
3:49 Then here's our pews, here's our templates
3:52 here's our static files, and so on.
3:54 We can go up here and see it says
3:55 "All right you can go in here
3:56 "and what we want you to do is go into that folder
3:58 "and create a virtual environment."
4:00 So, we'll do that.
4:02 They want to create the virtual environment.
4:07 You can name it a lot of things.
4:09 .env, or venv, is one of the best choices
4:13 if you're planning on using PyCharm
4:15 cause it will automatically detect those.
4:17 I'll make it more explicit and say venv.
4:21 Now the next thing to do, and so we ask "which Python"
4:25 and Windows asks "where Python"
4:28 it'll say, "It's the system one."
4:29 We want to do is use this one, so we need to activate it
4:36 so we're going to run that.
4:37 Notice my prompt changes here
4:38 and if I ask the same question
4:39 I'm getting the one that I just created.
4:42 So that's good.
4:43 On Windows you don't say source
4:45 and this is scripts; venv\scripts\activate.bat.
4:51 Okay, so now we have our virtual environment activated.
4:55 It's created in the directory
4:58 that is the top level of the package; good.
5:01 Now all we have to do is to basically register our package
5:05 and we can run it, register our website.
5:07 We do that by saying python setup.py develop.
5:11 Now normally you would run an install command here.
5:14 What we want to do is tell Python
5:15 to leave these files in place and just let us edit it here
5:18 so we don't have to ever rerun this command again.
5:22 It'll just reference this local working location
5:25 rather than copying it over to what's called site packages.
5:28 So we do this, it's going to install all the dependencies
5:31 and get everything setup so we can run it.
5:36 All right, great, it's installed everything.
5:38 Now we can ask what is installed by saying "pip list"
5:42 and we've got a bunch of things.
5:43 We've got pyramid
5:45 and the underlying template language installed.
5:48 We even have this full local location installed
5:52 with the package PyPI which is our web application itself.
5:55 One thing that's kind of annoying
5:56 about virtual environments in Python
5:59 is they always install out of date versions
6:03 of both setuptools and pip.
6:06 So, go ahead and update that real quick too.
6:09 There's only 11 versions out of date.
6:12 What's wrong with that?
6:13 Okay, so we should be able to run our web app
6:16 and go interact with it now.
6:18 Let's see "pserv" and we see if that exists.
6:22 Looks like it does.
6:23 That got installed when Pyramid got installed, okay?
6:26 And that Pyramid got installed when we ran the setup.
6:29 Then what we're going to give it is.
6:30 Let's just look around really quick.
6:31 We're going to give it either the development.ini
6:34 or the production.ini.
6:36 Here we want to give it the development one
6:38 so pserv development.ini.
6:41 There's a small chance this will fail.
6:43 There's this weird behavior sometimes
6:45 that the package in the virtual environment
6:47 doesn't get registered exactly right
6:49 unless you activate, deactivate, and then reactivate it.
6:53 Fingers crossed this works, let's see.
6:56 Perfect, okay if it doesn't
6:58 just deactivate the virtual environment
7:00 reactivate it, and then you'll be totally good.
7:02 All right, so now if we go throw that into our web browser
7:04 we should be golden.
7:06 Let's see what we get.
7:07 Ta-dah, the Pyramid Starter Project is up and running
7:11 and this is the Python Package Index Pyramid Application
7:15 generated by Cookiecutter; hooray.
7:17 So, we now have this basic structure over here.
7:22 If we look at it, whew
7:24 a lot more stuff going on now that we have
7:25 that virtual environment stuff registered.
7:28 Up here's what matters.
7:29 This is all the package installed locally
7:32 and then the virtual environment.
7:34 This is what you really care about, right?
7:36 We've got our package here.
7:37 We've registered it and now it's running.
7:39 We can go edit our template
7:41 to change this look and feel of our CSS.
7:44 We can go change our view to have different behaviors
7:47 or to have additional URLs and so on.
7:50 All right, so we're up and running with Pyramid
7:52 on the command line.