Episode: 2141
Title: HPR2141: Make Web Python with Flask
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr2141/hpr2141.mp3
Transcribed: 2025-10-18 14:51:08

---

This in HPR episode 2,141 entitled, Make Web Python with Flak.
It is hosted by Klaatu and in about 37 minutes long.
The summary is, Klaatu talks about Flak, a Python-based web-micro framework.
This episode of HPR is brought to you by an honesthost.com.
Get 15% discount on all shared hosting with the offer code HPR15, that's HPR15.
Get your web hosting that's honest and fair at An honesthost.com.
Flask, what is it? Why do you want to use it?
Well, I don't know that you do, but Flask is a Python web framework.
So, I guess from what I understand, you could think Ruby on Rails, but for Python.
You're probably thinking, but I thought that was Django, Django.
Yes, it is. So, Flask is kind of like Django, Django, light or an alternative to that, I guess.
I don't really know if it's all that much lighter, I don't know what the use cases are.
You can tell I don't know a whole lot about Flask so far.
But actually, I've been using it at work and originally when I was tasked with this particular thing that I had to do, I thought,
okay, so I need to do some kind of dynamic website thing, not too terrible at Python.
So, I will try to do this in Python plus some of the APIs that I had to tap into or Python.
So, it was quite obvious that it was going to need to be Python.
And I had heard of Django before, and I tried to kind of learn it, but all the tutorials on the Django site were,
at least at the time, they were very specific. They were like, you know, the how-tos, the tutorials.
It was always centered around like making a blog or something like that.
And I just kept thinking, well, I don't really want to do that.
So, I kind of never kind of captured my interest.
But so, Django was the first thing that I thought of.
And then the more I thought about it, the more I remembered that at Ohio Linux Fest, years and years ago now,
I had heard verbal from Chicago.
Do a talk about web2py, is what it was called.
And I thought, okay, well, web2py was a web, like a Python web framework.
So, maybe I could just use that.
So, I looked into that and sure enough, it appeared to do similar things as Django would.
It's, you know, kind of like this portal so that you can write Python code.
And then open a web browser and see the, I, I have to say the output of the Python code,
because that's not really what you're seeing, but you're seeing the results of pages generated by Python.
Put it that way.
So, so far we've got two different ones Django and web2py.
I did a little bit more research just to see if there were other options out there.
Like what, what I was dealing with.
And then I turned up this other thing called bottle, B-O-T-T-L-E.
That was a web Python framework.
And then finally I found Flask.
And then actually I did, to be honest, I did not find Flask.
So, I emailed the IT, the IT department at my job.
And I was like, yeah, I need this Python thing installed, this Python web framework.
And I like listed all three of them.
And they were like, well, what's wrong with Flask?
So, that's how I found Flask by not finding it.
And then being told that it was the obvious choice.
But it turns out that, I mean, I, I'm imagining that they all basically do the same thing,
just a matter of probably slight differences and kind of the way they approach it.
My point really is that I'm not, I'm not talking about Flask in this episode
because I have compared it with lots of other solutions and have found to be the better, you know, the best possible thing.
I'm talking about it because it was the one that kind of dropped in my lap.
You can find more about Flask at flask.poku.org.
That's flask.
F-L-A-S-K dot Poku P-O-C-O-O dot org.
It is a very simple page you will find.
It's, it's not super fancy.
It's very sort of minimal.
It's got some code snippets on there and a brief overview of what it does and how it does it.
And it's all pretty easy to ostensibly get started with.
Certainly like on their front page they have a very sort of simple hello world code snippet
and then how to install Flask, how to start up a local web server.
You know, one of those little Python web servers to just see, to see the output of your test application.
And it's basically the equivalent of getting to the Apache it works page.
It's kind of like, yes, I can see that it does in fact function.
But it doesn't go super in the detail and neither will I because I only have so much time.
But I will go a little bit farther than their demo app.
But the first thing that we need to do to kind of get our feet wet here is to install Flask.
There are lots of different ways to install Flask.
There is the normal, quote unquote, normal Linux distribution way of installing Flask,
which would be, you know, yum install Flask or DNF, I guess, install or apt install, whatever, whatever you use.
So you could just install Flask from there.
It might be called Python-flask or it might be called pyflask or Flask-python.
You know, do a search first for Flask and dig around and see what you've got.
That's one way to do it.
The other way would be to install it sort of directly through Python.
So specifically PIP, if you have not used PIP, it's kind of the, the officially sanctioned package management system of Python.
You can get PIP by going to pip.pypa.io.
So that's PIP.pypa.io.
It tells you on the page slash e in slash stable slash installing.
So if you go to the install installation link, it tells you, like, do I need to install PIP?
And it says that it's already probably installed if you're using Python versions blah, blah, blah or blah, blah, blah.
But you should upgrade it says.
So it gives you this file get pip get dash pip.py.
You can download that and then run Python dot slash get pip.py.
And it will upgrade or install PIP as needed.
If you are on a system that you are not the administrator of, pip can act locally.
And that's kind of a cool trick.
So if you do a pip install,
a flask with a capital F dash dash user,
or if you do a Python dot slash get pip.py dash dash user, then it will install pip locally.
And then you can use pip locally and you can do a pip install flask dash dash user.
So the dash dash user command whenever you're using pip will install everything local to your to your user folder.
It's really quite handy.
Everything goes into till the slash dot local slash bin slash whatever or slash live whatever.
But it's in the till the slash dot local folder.
Now, if you're going to do it that way, then you need to kind of manage your Python environment yourself in theory.
You should be doing that any way with set set on no that's sorry.
C shell virtual on for Python.
So you will want to do that if you're if you're working locally.
But if you're if you're not working locally, which I am, which is why I'm mentioning it.
But if if you're not, then you can just install via your Linux package manager and just kind of start using it.
It doesn't, you know, everything will just kind of fall into place.
Flask will know where all of its own files are located and it's not really a big deal.
Okay. So once you've got pip installed, as I said, it's the flask installation is pretty simple.
And they give it to you right there on the front page of their own site.
It's just pip install flask or again, pip install flask dash dash user for a local install of it.
And once you're done, I mean, that's that's installed.
All of its own components should bring in all of its own dependencies and stuff.
And then you should be able to run a quick hello world application.
And I guess we might as well start there.
So if you go to the flask.poku.org site, there's like I say a little snippet about seven lines, not including white space of code.
And it boils down to this from flask import flask app equals flask parentheses underscore underscore name underscore underscore parentheses.
And then the at symbol app. route or route. However, you say that in your local area.
parentheses quote slash close quotes close parentheses.
And then de f space hello parentheses parentheses colon and then return in indent return space hello world.
And then if underscore underscore name underscore underscore equals equals quote underscore underscore main underscore underscore quote colon app.
Run parentheses parentheses. So taking that apart a little bit, you've got your from flask import flask. Obviously, that's just you're importing flask the flask module.
And then you're you're declaring that you're you're using the flask class as as as your app.
So it's a pp equals flask and then the name.
So that's that's just kind of declaring this entity called app as a flask entity.
And then you give the app a default route, which is in this case, just slash. So that's going to serve as your web route folder.
So that's in other words, we're almost cheruting. We're not, but we're almost cheruting flask.
So when we start our flask server, it's going to it's going to consider the the base of itself, the root of itself.
As as call, it's going to call that slash and that's going to be the start of the server.
And that by default is the current the current directory.
So then we create a function called hello and we just return a string hello world.
Of course, we do the classic if underscore underscore name equals underscore underscore main, which does then we app.run parentheses parentheses.
And that's that's your that's your hello world file and or yeah, your file.
And so then if you do a Python space dot slash hello.py or whatever you just called that file.
And then it will tell you that flask is running on HTTP colon slash slash one two seven dot zero dot zero dot one colon five thousand.
So if you open a web browser and go to local host colon five thousand.
You should and then return you should see the string hello world or whatever you typed into your little hello world app.
You were following along it was the string hello world.
And that's it. That's that's up and running with flask in, you know, the 12 minutes minus the preamble.
So to get a little bit a little bit fancier and like I say, I'm not going to go too far into it because obviously once you start getting into it, it gets really, really complex.
But what we can do instead of just, you know, scraping the surface is we can we can make a couple of cool little tricks here just to kind of give you the idea.
An idea of what of what flask is capable of.
So the first thing that we'll do is we'll import a bunch more modules than the what we've got.
So the the from flask import flask is a good start.
But typically the modules you're going to need are going to also include session.
So it would be from from flask import flask flask comma comma session comma render underscore template.
That's a good one comma URL underscore four. That's URL underscore FOR comma request comma redirect. That's a good one. And comma J sonify J so in I FY.
We're not going to use all of those right now, but those are kind of like that's my default set.
I rarely do anything without that set. I'm not saying that's necessarily canonical by any means.
I don't know what other people do, but that's that's what I tend to use.
And then I we're going to go ahead and import sis and we're going to import OS. So that's sys and OS import sis import OS.
And let's see what else will need.
I think that's all we will need for now. Now actually that's not true. Let's do a from OS dot path import expand user.
That will be handy as well. I mean, technically, we have that because we imported OS.
Well, anyway, we can debate the most efficient way to do that later.
Let's do that for now. Okay, so the thing that we'll do is we'll say app equals flask colon.
I mean parentheses underscore underscore name underscore underscore.
But then instead of just leaving it there, we're going to define a custom folder for it for our templates for the pages that we return.
You don't necessarily have to do this because you can inherit templates.
And flask is certainly if you've installed it the default Linux way flask knows where all its templates are located.
And you can just create your own template files inheriting all the all the all the default templates and expanding them and kind of compounding upon them.
But we're going to go ahead and just we're going to ignore all the default templates and we'll just kind of create our own pages from scratch.
So we'll do a comma space template underscore folder equals.
And then this is why we imported all the OS stuff.
We want to do an OS dot path dot join parentheses expand user parentheses quote tilde close quote close parentheses comma.
And then folder by folder the path to your current flask directory like whatever directory you're working in.
And so for me, it's comma quote pie close quote comma quote flasky close quote quote comma quote template close quote parentheses parentheses.
So I've got this stuff in my home directory in a sub directory called pie.
And then in that directory, there's something called flasky.
And then there's a template folder inside of that. So that's that's my path to my custom template folder.
And that's so I'm launching. I'm going to we're in a launch flask.
We're going to use flask in our application.
And we're going to redirect it.
We're going to override its default template location to our own template folder. Hopefully that makes that makes sense.
I don't think it's I don't think it's too too complicated really.
Okay.
So what we'll do, I guess, is we'll we'll keep this sort of default app route, which is app dot route or the at symbol and then app dot route parentheses quote slash close quote close parentheses.
And we will do a D E F index parentheses parentheses colon.
And then the next line, we're going to define a variable and we're going to call the variable arg ARG.
We're going to set arg to equal cis dot arg V square bracket one close square bracket.
So if you are at all familiar with Python, if you've heard new world orders Python series, then you know that this grabs whatever argument, the first argument that was passed to
to this file and and puts it into the variable arg.
So it's basically a really quick and dirty arg parse.
So if we launch this file, which is called hello dot pi, if we launch it as Python dot slash hello dot pi,
then food is the first argument.
And so when we're in the file, when we're in this little running application, if we say, hey, Python, I want you to grab whatever was in cis dot arg V in the first slot, then it will be food.
Okay, so then that's all we'll do. We'll just we're just creating arg.
And then what we want to do is when when people go to this location, which by default is just the top, you know, the root level of our of our little server here, we want to return instead of returning just a string like we did in the hello world application, we are going to return a template.
So we'll say return space render underscore template.
And remember, we've redefined where the template folder is.
So it's not going to be looking for a template from the flask distribution, it's going to look for a template that we define.
So we'll do a render underscore template parentheses, quote index dot HTML, quote, comma, and then arg equals arg close parentheses.
Now, that's important. We need to tell it arg equals arg because I mean, we could say penguin equals arg.
It doesn't really matter what we call it. I'm just calling it arg again so that everything's consistent.
But what we're doing essentially is we're passing that variable in this case, arg, we're passing that variable through this file here that's going to be running into the template.
So that we can then use our variable in our little template.
And then we can close this whole file with the same, the same general incantation if underscore underscore name underscore underscore equals equals quote underscore underscore man underscore underscore underscore quote colon next line app.
Run parentheses parentheses.
Okay, so if you want to, and this will fail, but if you want to, we can launch that right now.
We can do Python dot slash hello dot pie.
And in your web server, you can do a refresh or whatever you need to do to get back to local host colon 5000.
And it's going to tell you that it has no idea where well, actually, it might, it'll probably first tell you that there's an index error because I forgot to pass an argument to it.
So that that might happen. But either way, so eventually you're going to get to a point where it's telling you that the template cannot be found because we haven't created index dot index dot HTML yet.
So you can cancel that control C to stop flask from running.
And then we need to do.
You need to go into emax.
I mean, you can go into a different editor. If you must, I don't know why you would.
If you want to learn emax, I've done a series on that for you here on hacker public radio, well worth knowing.
But I digress. So we're going to go into our templates folder and open up index dot HTML, which doesn't exist yet.
And so the first thing that we can do, I guess, is just make a really simple template. And this is going to be, again, like if you've installed it via your Linux package manager, then everything kind of knows where everything else is.
And technically you don't have to make your own template. You could just ride with whatever flask provides.
But I'm assuming that in that in real life realistically, you're not really going to be using templates all that much.
You're going to be making your own. I mean, why why wouldn't you really? So I'm not.
I guess I'm not 100% clear on what template the use of templates are.
My self, but I guess it's good to learn like inheritance and stuff. But anyway, we're just going to make our own. So we'll just do a simple HTML tag HTML.
And then head. And then title. Those are all required.
And then we'll put in some title. We can just say, you know, flask app.
Close title, close head, and then open the body. And then let's do an H1 hello world.
Close the H1. And then we could even do like paragraph. That's a p tag.
And we could say this is a web.
Pi page, I don't know, whatever you want to say. I'm not really feeling that creative about this right now.
But you close the p tag and you close the body and then you close HTML.
And if we save that control X control S and then close control X control C.
Now if we start our little Python app.
And we still need to give it an argument. So the string to start this thing is Python space dot slash hello dot pi.
And then I'm just going to put in through.
We won't see anything about food yet because we haven't used that argument or that variable.
But there you go. So if we go to our web, our web browser and refresh, we should see the index page that we just created.
So that's good. But it's still a little bit more. So since we did define in our in our script.
We told let's look at that again. We told.
Flask that that that when we return the index dot HTML page to the user.
We told flask to to be aware of the arg variable.
So again, the path of the arg variable, we when we type in Python dot slash hello dot pi.
And then we do space food.
Then food is this the the argument that we're passing into the hello dot pi file.
That's just how commands work. We all know that right. We do an LS space slash us r slash share.
Then us r slash share is the argument for the LS. Yeah.
So food is the argument to hello dot pi.
And so when we've started up hello pi dot pi.
And we've imported the sys module sys.
We're giving Python access to whatever we typed after hello dot pi.
And in this case, all we do with it is we say.
Take that value sys dot arg v square bracket one square bracket.
And dump it into this variable called a r g arg or we could call it anything.
Again, we don't have to call it art.
We call it penguin. We could call it through. We could call it bar.
We could call it bas. It doesn't matter.
But I'm calling it arg because that makes sense to me. It's an argument.
So arg equals sys dot arg v square bracket one square bracket.
Now when we say when we're telling flask to return a render underscore template.
Which is of course located in the template folder that we told flask to use.
When you at the start of our little hello dot pi file.
Then return the file index dot html comma arg equals arg.
And there we're just saying also.
As you're returning this index dot html file from your templates folder.
I also want you to create a new variable called arg.
And in this new variable, I want you to put the contents of arg.
So again, it doesn't have to be arg. It could be anything.
It could be bar equals arg. It could be bas equals arg.
It could be anything.
But we're creating that variable on the fly that kind of goes along with our index dot html.
And that's how that's how it's aware of that thing.
Okay. So let's go back into our template.
So e max space template slash index dot html.
And in the title block, instead of just saying flask app, let's use a variable.
The way that you call a variable from within a template is the double curly bracket.
So I'm going to delete flask app.
And I'm going to type in curly bracket curly bracket space arg.
And it needs to be the exact same capitalization that you used in your in your hello dot pi.
And all caps ARG all caps, but you if you do whatever you did.
So curly curly space arg space curly curly.
And then I'm going to go ahead and put in my body.
I'm going to put an h1 curly curly arg curly curly just so I really, really see this thing.
And that'll be enough. Okay. So that's how you get a template.
You get a variable from from your template file.
Which is a powerful, powerful thing.
Okay. So now let's start the hello dot pi.
And this time I'm going to use h a x x 0 are.
So Python space dot slash hello dot pi space h a x x 0 are.
It's running. So now if I go back over to my web server and refresh.
It says hacksore.
This is a pie page.
So there you go. That's some getting variables out of the Python script.
The reason that that's so powerful is because now in this Python script, this hello dot pi script,
you can generate all kinds of things like you could.
You can dynamically pass data into it.
You can talk to databases and grab variables out of the database and then present it to your user.
You can create forms.
Have your user submit data into your your Python file and then process that data and return it to the user.
You know, in some other format.
It's a really, really powerful thing.
I mean, this is that's where it becomes truly a Python application.
Like the hello world stuff where you're just kind of telling Python to create HTML.
Routing.
And maybe some HTML code to show to the user.
It's it's kind of.
It's kind of just a really round about over complex way to generate a stupid unstyled.
Web page.
But once you start actually using the Python stuff underneath to generate new and interesting data that the user.
You know, can interact with or whatever.
Then suddenly you're you're programming.
Like this entire new program and everything that the user sees is just the front end.
It's just kind of like the interface to your Python program.
So I mean, the sky's the limit from there.
It's probably that's probably enough for you to sort of get started with really.
I don't I don't really know what else I could go through without starting to get into.
Into different ways of of you know, inventing data and grabbing data and stuff like that.
But I feel like that would be biting off a lot more than probably one episode could chew.
So the next step probably from here would be to go to flask.poku.org and have a look at their snippets page.
Their snippets page has.
I wouldn't say hundreds, but maybe 80 or 60 or 100 different little snippets of sort of things that you can do with with flask.
And they're all essentially public domain files.
That's what they say is they say consider it public domain.
And so there's a bunch of different things that you can do.
Some of them are maybe more complex, you know, depending on where you are.
In your in on your experience level, but, but it's it's not a bad idea to at least have a look at it.
And then, yeah, and play around with the different routes as well.
That's not a bad idea. In the flask documentation, they go over that a little bit.
And essentially they're their decorators in your Python file.
And you can do like an at app.route parentheses quote slash would be, you know, close, quote, close parentheses.
That would be just the again, the root level of your server.
But then if you you could also do like an at app route parentheses quote slash log in, you know, close, quote, close parentheses.
And then if anyone went to your website slash log in, then they would be, you know, you would return a different template with with some other different back end.
So yeah, there, you can you can really structure your website very manually and just kind of direct people all over the place. And it doesn't really.
It's a little bit confusing if you're used to hard coding everything and kind of working very much within a literal web server.
With Python with with with flask, you can you can kind of construct a virtual web server almost, you know, it'll it'll look to the user like, oh, I'm going to a directory called log in or I'm going to a directory called data or or pages, whatever.
But it's not actually a directory. It's just it's a route in flask that returns a different set of data. It's pretty crazy. You can really, you can really mess with your users head.
If your users trying to reverse engineer how your web servers happening, which hopefully they're not.
Yeah, so that's that's flask now obviously in real life, you wouldn't want to run flask from your home directory.
And you wouldn't want to use the built-in web server that flask provides for testing for your production, obviously.
So that again is kind of a different topic. It's not that complex, but since there are so many different ways to do it, I don't feel like it's worth covering, but but essentially, I mean, first of all, you would want to probably again install flask sort of the right way, you know, the on the system level.
And then you need, you need some kind of interface for your web server to be able to interface with your with your with the fact that flask exists and should be run.
So for Apache, you would need, you know, the Python modules and for Ingenx, you need Python. I don't know what they're called in Engine X. Are they still called modules? I don't know. But yeah, you usually need to make your web server aware.
Well, you always need to make your web server aware that you want Python to be, you know, like CGI kind of executable things that that it can serve to your public.
So there'll be a little bit of configuration there, but again, it's it's all it's not we're not inventing anything here. This is all pretty standard stuff. So it's it's all documented quite well on the flask website.
It's a lot of fun. It's very cool. It's like the next level of web development. It's it's it's it's actually quite satisfying to I've talked to several people now who
who I mean, I'm probably biased because I'm using it at work. So I kind of get I get confirmation from people, you know, around here who are using flask and it's kind of like, oh, yes, yes, flask is good.
But I've talked to a couple of people like an IRC who who really enjoy flask as well. So yeah, I feel like I feel like it's kind of a cool little technology. And it is simple. I don't know, you know, I don't have enough experience with Django or or anything like that to to truly compare it, but I will say that
that the time it took to kind of get up and running with flask relatively it was really pretty quick. So and I imagine once you learn flask learning other frameworks would probably be pretty easy as well.
Like I could probably if I needed to or wanted to, I could probably crack open Django at this point and figure it out in an afternoon instead of the three years that it took me to to finally come to flask and discover it and and actually use it.
And as always, having a project in mind is very helpful as well. So, you know, try try to start out simple, start out with some point and and you'll probably you'll probably have great success. So yeah, let me know if you do anything cool with it. Thank you for listening and I will talk to you next time.
You've been listening to hecka public radio at hecka public radio dot org. We are a community podcast network that releases shows every weekday Monday through Friday.
Today's show like all our shows was contributed by an HBR listener like yourself. If you ever thought of recording a podcast and click on our contributing to find out how easy it really is.
Hecka public radio was founded by the digital dog pound and the infonomicom computer club and it's part of the binary revolution at binwreff.com. If you have comments on today's show, please email the host directly, leave a comment on the website or record a follow-up episode yourself.
Unless otherwise stated, today's show is released on the creative comments, attribution, share a light, free dot org license.
