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