Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Your first Flask site
Lecture: Introduction to creating a Flask website
0:00 I don't know how you feel
0:01 but I think it is high time that we write some code.
0:03 I'm itching to work on this PyPI project
0:05 and get a super cool web application build in Flask
0:08 and I'm sure that you are as well.
0:10 Before we get to write in our PyPI app
0:12 we're going to build just a really simple one off project
0:16 and we're going to do this in two ways
0:17 we're going to use the CLI, Command Line Interface
0:19 just terminal so on and we're also going to use PyCharm.
0:22 The terminal part is a little bit more complicated
0:25 only a little, has a few more steps.
0:27 We're going to do that first so that you can appreciate
0:30 all the stuff that PyCharm is doing for us
0:31 and I actually personally prefer to just do things
0:34 on the command line these days
0:35 but I do remember when I started
0:37 I preferred having the help that PyCharm provided.
0:39 Okay, so, how do we get started?
0:41 Well we're going to create and activate a virtual environment.
0:45 It's really important that our development environment
0:48 and our production and even test environment
0:50 stage environment, all the different servers
0:52 and places that we run this web app
0:54 is as close to the same as possible
0:56 and in Python, a really good way to do that
0:58 is to create a virtual environment so you
1:00 can precisely control what packages
1:02 and what versions of those packages are installed.
1:05 So we always start with this
1:06 create a virtual environment.
1:07 Want to create the folder structure.
1:09 Now, a lot of tutorials with Flask say
1:12 Oh Flask is so easy, all these other ones are complicated.
1:15 What you do is you create an app.py
1:17 you type these few lines in there
1:18 and boom, you've got a Flask website.
1:20 No, you don't. You've got a silly little tutorial.
1:23 A real Flask website is like other real websites.
1:26 You have hundreds of files, mini static files
1:29 all sorts of stuff going on
1:30 and you need the proper structure to maintain that.
1:33 So there's a couple of cool design patterns
1:35 we're going to bring into place
1:36 and that's going to drive our folder structure
1:38 which I'll show you in just a little bit.
1:40 If you want to see what the Flask folks recommend
1:42 you can follow that link at the bottom. Mine is different.
1:45 I like mine better, you can decide which one you like best
1:48 and just follow that, okay?
1:50 We spoke about the virtual environment and the requirements
1:54 the best way in Python to do that
1:55 is to have some kind of requirements file that we can run.
1:58 pip install -r requirements.txt is very very common
2:01 actually like to have two requirements files.
2:04 We'll talk about what those are and how they fit together
2:07 but you could be using something like Pipenv
2:09 or Poetry that actually uses the pip lock file.
2:11 It doesn't really matter but something that
2:13 encodes endev version control, these are the packages
2:16 and the versions of packages that we depend upon
2:19 that lets us bootstrap our virtual environment.
2:22 Once we have our little tiny bit of app.py written
2:25 that's going to create a route and a view in someone
2:27 we can serve it up and add features and then just iterate
2:30 and you'll see with Flask you don't even have
2:32 to restart the server necessarily.
2:35 It'll detect changes to the python files
2:36 or the template files and automatically restart the process.
2:40 So that's really cool, you can just keep working in
2:43 really nice fluid style.
2:44 Sometimes this falls apart if our app, you know, has like
2:47 bad syntax it might crash and not refresh correctly
2:50 so then you got to restart it.
2:52 But generally we can just keep working on our site
2:53 adding features, fixing bugs.
2:55 It's a nice fluid style.