Latest / Talk Python To Me / #379: 17 Libraries You Should Be Using in Django
Transcript
- 0:00Do you write web apps in Django?
- 0:01The framework has come a long way lately, with versions 3 and 4 adopting many of the modern Python capabilities.
- 0:08Async, for example.
- 0:09But there are so many other libraries and apps that you can use to do more with less code and plug in new functionality.
- 0:16I'm happy to have Christopher Trudeau here on Talk Python to take us through his 17 favorite libraries you should be using in Django.
- 0:24This is Talk Python To Me, episode 379, recorded August 22nd, 2022.
- 0:30Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:46This is your host, Michael Kennedy.
- 0:48Follow me on Twitter where I'm @mkennedy and keep up with the show and listen to past episodes at talkpython.fm.
- 0:54And follow the show on Twitter via at Talk Python.
- 0:57We've started streaming most of our episodes live on YouTube.
- 1:01Subscribe to our YouTube channel over at talkpython.fm/youtube to get notified about upcoming shows and be part of that episode.
- 1:09This episode of Talk Python To Me is brought to you by the IRL podcast, an original podcast from Mozilla.
- 1:15This season, they're looking at AI in real life.
- 1:18Listen to an episode at talkpython.fm/IRL.
- 1:22And it's brought to you by Microsoft for Startups Founders Hub.
- 1:25Get early stage support for your company and build that startup you've been dreaming about.
- 1:29Visit talkpython.fm/foundershub to apply for free.
- 1:34Transcripts for this and all of our episodes are brought to you by Assembly AI.
- 1:38Do you need a great automatic speech to text API?
- 1:40Get human level accuracy in just a few lines of code.
- 1:43Visit talkpython.fm/assemblyai.
- 1:47Hey all, a quick announcement before we jump into the interview.
- 1:50Some of the ideas we discuss in this episode come from a brand new Django course that Chris authored over at Talk Python Training.
- 1:57If you enjoy this episode and want to put these ideas into practice or you want to just get started with Django,
- 2:02be sure to check out his course at talkpython.fm/Django.
- 2:06Now let's dive in.
- 2:07Chris, welcome to Talk Python To Me.
- 2:11Thanks for having me.
- 2:12Yeah, it's great to have you here on the show.
- 2:13You know, we've worked on some projects together and talked otherwise, but your first time here on Talk Python.
- 2:19Monumental.
- 2:19Indeed, indeed.
- 2:21So I'm super excited to talk about Django.
- 2:23I don't do that much Django, which is why you're here because you do a ton of stuff with Django.
- 2:28So really excited to have you share your experience.
- 2:31And we're going to focus on a bunch of cool little tools and apps that you can plug into Django and make it better,
- 2:36make it do more and do a lot of the Django things, you know,
- 2:39pull a few pieces together and make your app better without a lot of coding, right?
- 2:42That's the idea.
- 2:43Indeed.
- 2:43Before we get to all that, though, let's just start with your background.
- 2:46How do you get into programming in Python?
- 2:48I'm an old man, not punch card old, but single digit megahertz old.
- 2:53My dad worked for IBM.
- 2:55So my first computer was an IBM PC Junior.
- 2:58256 kilobytes of memory with the expansion card.
- 3:02No hard drive, five and a quarter floppy and 4.77 megahertz.
- 3:08And that second seven is really, really important.
- 3:10It made all the difference.
- 3:11Did it have a turbo button?
- 3:12It did not.
- 3:14IBM did not do that little bit of marketing stuff.
- 3:18But I'm pretty sure my USB charger has more processing power now.
- 3:22But all my friends had Commodores.
- 3:24And so I couldn't trade pirated games with them because that's what everyone did.
- 3:28And the box came with this kid friendly teach yourself basic book.
- 3:33And so that was kind of my intro.
- 3:35There was I had one game and nothing else to do with the computer.
- 3:38And the first program I remember writing was a Batman logo, not copying it from a book, like on my own kind of thing.
- 3:46And so like the basic library had like draw arc and fill in yellow and it kind of clicked for me.
- 3:51And 30 years later, I was running a software department at a company that got acquired by a large firm who shall remain nameless.
- 4:00They just own all of your favorite movie franchises and have a rodent fronting it.
- 4:04I think you know who I'm talking about.
- 4:05And as part of that acquisition, the department got shut down, but we were asked to stick around to help with the transition.
- 4:10We had about two weeks worth of work to do and we had six months to do it in.
- 4:14So I had a fair amount of spare time on my hands and I used that to pick up Python.
- 4:18And the first script I wrote in Python was that we had a photo sharing site and the due diligence of the transfer included looking for questionable content in that photo sharing site.
- 4:31And I had to, for the police investigation, go off and grab a whole bunch of stuff out of S3 and download it that belonged to this one individual.
- 4:39So the script was kind of icky, but I was very, very impressed on how little code I had to write in comparison to previous languages.
- 4:46Part of that's Python itself and part of it was just, you know, pip install, pip install, pip install, and it solved all these problems.
- 4:52And I've never looked back.
- 4:55It's been my primary language for almost 15 years now.
- 4:58What a good language.
- 4:58What a good choice.
- 4:59Who knew 15 years ago or 10 years ago or whenever we all got into it that it would be so incredibly popular these days, right?
- 5:07Yeah, it's grown a lot.
- 5:08It's, yeah, it's really taken off.
- 5:10Yeah, I just saw an article.
- 5:11What was it called?
- 5:13The Unstoppable Programming Language or something like that from InfoWorld.
- 5:19And boy, it, I'll even pull it up.
- 5:21What an interesting situation where, you know, the subtitle of the article is Unstoppable Python.
- 5:28Once again, ranked number one in the TOV index.
- 5:31I think it's the breadth of the libraries, right?
- 5:33Like most of the languages, there's, you know, JavaScript is often at the top of that list, but it's only really for one purpose.
- 5:39It's for building web applications.
- 5:41And I know you can do other things in it and not trying to upset the internet.
- 5:44But with the data science behind.
- 5:47You're going to get us email, Chris.
- 5:48Oh, yeah, I'm sure.
- 5:49Well, you'll get them email.
- 5:50I'll be long gone.
- 5:51So that's fine.
- 5:52With the data science side, with the ML side, with the, you can do things on the web side, right?
- 5:58Like there's being number two in all of these areas very easily makes you want number one overall, right?
- 6:04So it's, I think it's the breadth more than anything.
- 6:07Yeah.
- 6:07You talked about how the company around was acquired and you had all this sort of time to kind of think about changes.
- 6:14I find times like that are really interesting ways to maybe refocus your career or to think about where you've been and where you might be going.
- 6:23Maybe whether you should turn left or right or just keep going the way you are.
- 6:27You know, I've had a few like that.
- 6:28And it's, those are some good opportunities to take if you can get them.
- 6:31Yeah, it gives you a chance to experiment if nothing else.
- 6:34You know, there was no pressure to pick up a language.
- 6:36I was a manager at the time.
- 6:37So there was no pressure at all, period.
- 6:39But yeah, it gives you the breathing room to go and play with a few things and get comfortable with it.
- 6:45And the next job I took was helping a small startup.
- 6:48And a manager comes to me and says, oh, pick the language, pick the stack.
- 6:53And I was like, okay, we'll do Python.
- 6:55We'll do Django.
- 6:55Then it sort of went from there.
- 6:57So good choice.
- 6:58How about now?
- 6:58What are you doing these days?
- 6:59My marketing people tells me, tell me I'm supposed to call myself a fractional CTO.
- 7:03So I help organizations with technical decisions and software processes.
- 7:08So that's architecture, some coding.
- 7:10Often it means nowadays the agile process stuff.
- 7:13So it's a bit of a mixed bag.
- 7:14I tend to specialize in trying to get small teams more efficient in some tech way, either the process or the learning itself.
- 7:22And because of the agile thing, I've done a fair amount of teaching.
- 7:25Because oftentimes when you're bootstrapping new teams, you got to start out with Scrum 101 and all that kind of good stuff.
- 7:30And that kind of led into the screencasting and online teaching world.
- 7:34So now I've done over 30 courses in Python and I'm still clicking along.
- 7:38So it's become this sort of side thing that I have that has blown up into its own thing.
- 7:45I can relate.
- 7:46I can definitely.
- 7:47It's awesome.
- 7:49Well, cool.
- 7:50So part of that actually is you and I just recently worked on a course for Talk Python training, Django, getting started.
- 7:57Which there's been, you know, it's a six hour course.
- 8:00People can check it out.
- 8:01There's a bunch of good stuff.
- 8:02We're not going to really talk about that.
- 8:04But it kind of got me thinking like, hey, wouldn't it be great to just cover all the cool little things that you touch on here and then just, you know, in your other training.
- 8:14I guess people probably could reach out to you if they've got a team and they want to maybe help them shift gears, help them change technologies or change the way they're working.
- 8:22Sure.
- 8:23Always nice to have the pipeline full.
- 8:25So for sure.
- 8:26Indeed.
- 8:26Yeah.
- 8:27So, you know, you always, you might be brought in to do one thing like, hey, help us do Django or help us change the way we're working with sort of deploying our apps or whatever.
- 8:36But then there's like, oh, but did you know about this tool?
- 8:38And have you tried this?
- 8:39And wouldn't it be easier if you use that?
- 8:40And so how about we make that the next hour?
- 8:42Yeah.
- 8:43You know, and this kind of ties back to that.
- 8:46You know, we were sort of talking about Python and the breadth of that, right?
- 8:48One of the reasons it's so easy to get a lot of things going is because there are so many libraries that have been there and done that.
- 8:56And I often, we're going to talk about later, you know, one of the ones that's sort of a catch all.
- 9:01And every time I'm in there, I'm like, oh, oh, wait, I need to remember that's in here because I keep writing that code.
- 9:06Right.
- 9:06And so knowing that things are out there and finding this stuff can make a big difference in how little code you have to write.
- 9:13And it's always great when someone else has tested it, because then not only are you not having to write it yourself, but it's probably more stable than the stuff that you're writing yourself.
- 9:21Or at least I'll speak for myself.
- 9:22It's more stable than my code.
- 9:23For sure.
- 9:24Well, I think, you know, people who are getting into programming think about what is an expert.
- 9:28An expert is somebody who could write these crazy algorithms or implement this wild file processing stuff from scratch.
- 9:36And that somewhat is true.
- 9:38But more often, the expert is the person that knows they can pip install this other thing and not have to write that.
- 9:43You know, they're like, oh, these libraries exist.
- 9:45I know they'll work in this situation.
- 9:47I don't need to do this.
- 9:48Right.
- 9:48So, yeah.
- 9:50It's both a power, but it's also really like knowing Python.
- 9:52I think knowing Python is partly knowing the libraries outside just the language.
- 9:57I would say more than partly like picking up a new language is if you've done a few before and it's in the same vein, although, you know, all the grandchildren of C based languages.
- 10:07It's like, OK, so how do they what's variable assignment look like?
- 10:10What does four loops look like?
- 10:12Once you've done that, you know, a few days later, you're ready to go.
- 10:15Knowing the depth of what's out there so that you don't have to write it, that takes forever.
- 10:20Right.
- 10:20So, you know, there's language expertise within months.
- 10:23There's library expertise, which is years and is constantly changing.
- 10:28It's nonstop.
- 10:29It's like you can't learn them as quickly as they come out, really, especially if you're in JavaScript.
- 10:33That's another story.
- 10:34Yeah, I saw a funny T-shirt that said I learned Python.
- 10:37It was a great weekend.
- 10:38And that's in some sense true.
- 10:40Right.
- 10:41But on the other, you'll compare and contrast that to like, I feel like I've been doing this forever.
- 10:45And I'm still learning every single day.
- 10:47How do you square those two things?
- 10:49Right.
- 10:49It's a lot largely what you just said.
- 10:51Yeah.
- 10:51All right.
- 10:52So we're going to talk about a bunch of different topics.
- 10:55We'll see what the final number is.
- 10:5617 ish different libraries, some of them directly pluggable into Django for specific things,
- 11:03and some of them just really helpful for Django.
- 11:05So you want to kick us off with the first one here?
- 11:07So there's sort of two groups in the first group of things that we're going to talk about are libraries that any project of a reasonable size should consider using.
- 11:15So these are like you said, these aren't the Django specific stuff.
- 11:17And then we'll get through a few of those and then we'll get into the actual this you should use this for Django.
- 11:22So this first one that you've got up on the screen there, this is coverage.
- 11:26This measures how much of your code gets run when you run your code.
- 11:31So it's typically used for your user tests.
- 11:34And what you're trying to figure out is how many how much of your code has been exercised when you run your tests.
- 11:40So it has sub commands, one of which is run, and you basically hand it any script and it will give you a output listing of what it saw as it went along.
- 11:51That output listing is like a file by file listing and shows you like a percentage mark that says, oh, you know, 30% or 50% or whatever of this file got executed in your run.
- 12:00And then there's a couple other commands that you can run that will combine all this information into a report.
- 12:06And the report is spit out in HTML and that gives you a fully annotated list of your code.
- 12:12So you can go in and see, oh, there's the red line.
- 12:14That's a line that didn't get triggered.
- 12:16So often I find when I'm writing tests, it's usually fairly easy to get the, you know, the easy path through, right?
- 12:23You're not thinking about the errors or whatever.
- 12:25You just sort of, oh, I exercised it.
- 12:26And then you go, oh, I've only got 75%.
- 12:28Well, finding if you want that, you want to have something higher than that.
- 12:32You go in, you look at the report and you go, oh, that red line is that error condition.
- 12:36Okay, I need to add a test to make sure that that error condition is handled properly.
- 12:39So I often take this one and then write a little bash script that does this, runs my tests and then runs a linter afterwards.
- 12:47So it's my little check-in hook, right?
- 12:49So just before I do a push, it'll go off and run all my tests and give me coverage.
- 12:53And if I don't like the numbers, then I go off and fix it some more.
- 12:56I think when you're learning about testing, it's important to test the happy path, but that's the one that often kind of obvious, right?
- 13:04You're going to know right away if that thing is broken.
- 13:06But it's those edge cases.
- 13:07What if I put a string into this input where, you know, a number like the word seven versus the number seven?
- 13:14Is that a 500 server error or is that a, sorry, invalid data, ideally even caught on the client side with HTML5 validation or something?
- 13:23Those kinds of things are really hard to think about.
- 13:25And coverage really points out, you know what?
- 13:28All the parts that are read that are uncovered, those are the error handling or the guarding clauses or the other cases where you're not normally going to run into them.
- 13:37So you really need to have a test to check them.
- 13:39Yeah, that's right.
- 13:40I'm also independent of coverage.
- 13:42I'm also a big fan of when bugs show up.
- 13:44I'm not a pure TDD guy until bugs show up.
- 13:47So when the bug shows up, I go and write the test that triggers the bug and then I fix it because then I'm adding to my regression test as well.
- 13:55Right.
- 13:55So that's also to your point, it's those weird conditions that you didn't even think of.
- 13:59And coverage, because it is only line based, you can get into trouble where, you know, the first part of an if clause passes and the second part doesn't.
- 14:08And so it's green.
- 14:09It looks like it's happy, but that doesn't mean you don't have a bug.
- 14:12Right.
- 14:12So there's always this mix and go.
- 14:14But coverage can help you find this stuff first time through if you're looking.
- 14:18I'm definitely not a purist, TDD purist myself either.
- 14:21I find there's tests add a lot of value, but there's also a practical, I just got to get some stuff done and I don't need to like retest everything.
- 14:28It'll only so many hours in the day.
- 14:30Yes, exactly.
- 14:31But I do like this idea of like once you found a bug, if you can reproduce it with a test, you kind of pin it down so you don't play whack-a-mole and have it ever come back.
- 14:39Exactly.
- 14:40Yeah.
- 14:40Kind of related to this whole part of the conversation, Anthony in the audience asks, he's interested to know how best to test Django web apps with pytest.
- 14:49Feels that there ought to be ready-made recipes for this, given how opinionated Django is.
- 14:54You got any thoughts on that?
- 14:55I don't actually use pytest myself, so I'm only vaguely familiar with it.
- 14:59There are some recipes out there.
- 15:00I know there are Django apps that you can install that will help you and pre-canned with the recipes.
- 15:06Django is fairly robust itself.
- 15:10It builds a whole bunch of things on top of the unit test framework that is very Django specific.
- 15:15And it allows you to shortcut the web as well.
- 15:19So you can access the views directly, which means you don't have to worry about like a lot of JavaScript and things like that.
- 15:25And it also has a live server mode, which I was playing with this afternoon, actually, which runs a server so that you can run things like Selenium on top of it.
- 15:33So there's a fairly rich set of mechanisms in there for Django to begin with.
- 15:38From what I understand of the pytest library, it really just sort of makes some of those hooks easier and exposes them rather than introducing a lot of extra functionality.
- 15:47For sure.
- 15:47Wouldn't you know that there's an awesome Django list?
- 15:50And it happens to be done by Will Vincent.
- 15:52In here, there's a whole section on testing.
- 15:55And you've got some pytest Django.
- 15:59You've got Factory Boy for test fixture replacements.
- 16:01A feature flipper Django waffle.
- 16:04That's kind of an interesting name.
- 16:06Model Bakery for creating, I'm guessing, the ORM models in interesting ways.
- 16:12And yeah, people can check this out.
- 16:13Another related question, Nick asks, I like to hear opinions on mocking the ORM database when testing versus actually using the database.
- 16:20I don't know how you feel about this, Chris.
- 16:22I don't tend to mock unless I absolutely have to.
- 16:25I usually mock for things like, oh, we have to do something with dates.
- 16:29And dates and times always cause problems because, you know, you're running the test now.
- 16:34For the ORM, I tend to just use tests straight into the ORM.
- 16:38Django comes with a lot of mechanisms like fixtures that allow you to push things into the database.
- 16:43And the test suite automatically will install those fixtures and wraps each test in transactions.
- 16:49So it does it and then undoes what you did.
- 16:52So you're always in a known good state at the beginning of each one of your functions.
- 16:56So I haven't really come across a case where I really need to mock out the ORM.
- 17:00I would guess there's probably a argument about speed there.
- 17:05But I tend to get around that with things like tagging my tests.
- 17:09So I will tag much that are like the smoke test, the quick one pass through.
- 17:13And if I need like a really fast test to run on, say, check in, then use a smoke tag and then let the regression run overnight if I have to or whatever.
- 17:22So I haven't come across the need for mocking it out yet myself.
- 17:25Yeah.
- 17:26And you can use SQLite with an in-memory database model as well.
- 17:30And just as start of the whole test run, you could fill that in-memory version up with known good test data.
- 17:36The transactions will kind of roll it back each time, right?
- 17:38And so just let it rip on SQLite in memory and then disappear into the ether of RAM.
- 17:44Yeah.
- 17:44It tends to be, unless you're doing huge amounts of work, it tends to be pretty peppy.
- 17:49Sure.
- 17:50Yeah.
- 17:50And you can choose how much data you put in it, right?
- 17:52So you can chill out on that.
- 17:56This episode of Talk Python To Me is brought to you by the IRL podcast, an original podcast from Mozilla.
- 18:02If you're like me, you care about the ideas behind technology, not just the tech itself.
- 18:07We know that tech has an enormous influence on society.
- 18:11Many of these effects are hugely beneficial.
- 18:14Just think about how much information we carry with us every day through our cell phones.
- 18:19Other tech influences can be more negative.
- 18:22I really appreciate that Mozilla is always on the lookout for and working to mitigate negative influences of tech for all of us.
- 18:30If those kinds of ideas resonate with you, you should definitely check out the IRL podcast.
- 18:35It's hosted by Bridget Todd, and this season of IRL looks at AI in real life.
- 18:40Who can AI help?
- 18:41Who can it harm?
- 18:42The show features fascinating conversations with people who are working to build a more trustworthy AI.
- 18:49For example, there's an episode on how the world is mapped with AI, but it's the data that's missing from those maps that tells as much of the story as the data that's there.
- 18:59Another episode is about gig workers who depend on apps for their livelihood.
- 19:03It looks at how they're pushing back against algorithms that control how much they get paid,
- 19:08and how they're seeking new ways to gain power over data and create better working conditions for all of them.
- 19:14And for you political junkies, there's even an episode about the role that AI plays when it comes to the spread of disinformation around elections.
- 19:21Obviously, a huge concern for democracies around the world.
- 19:25I just listened to The Tech That We Won't Build, which explores when developers and data scientists should consider saying no to projects that can be harmful to society,
- 19:34even though we do have the tech to build them.
- 19:37The next thing kind of related to that is PyFlakes.
- 20:05I mentioned at the end of that that I often use a linter.
- 20:07This is the one that I tend to go to.
- 20:09PyFlakes is code-focused rather than style-focused.
- 20:13So when I first started linting Python, a lot of the code I was maintaining was not PEP 8 compliant,
- 20:19and we'd run the linters and they were just constantly screaming about everything.
- 20:23PyFlakes was like a nice little happy medium for us.
- 20:27It runs using the abstract syntax trees, which means it doesn't actually load the modules, which is good because that means you're not having to do the side effects of your actual code.
- 20:35And I usually use it for things like finding unused imports and weird variables and things like that.
- 20:41The other place is I find if I've made a lot of changes across a whole bunch of files, running PyFlakes before I run my tests, it's sort of a bit of an idiot check, right?
- 20:50So the things that don't compile or whatever, it'll go through and run everything once for you.
- 20:53And it seems I find it faster to do that than, say, run all my tests first.
- 20:58Sure.
- 20:58This is kind of like a compiler, you know, and compiled languages, C++, C#, Swift, whatever.
- 21:05You press compile and there's a how does it all hang together sort of check that the system has to go through as a static language.
- 21:11And we don't have that.
- 21:12Even if you put type hints, that's a hint, not a requirement for accuracy.
- 21:16And so not saying that this does, you know, type validation like mypy, but it makes sure that your code at least somewhat hangs together, right?
- 21:24It's a nice, nice quick check.
- 21:25Yeah, it's a sanity checking.
- 21:27Yeah, absolutely.
- 21:28And there's a version of this, if you want to dig down into it, that is called Flake 8 that combines PyFlakes with PEP 8.
- 21:35So if you want to get all anal retentive about the code cleaning and all the rest of that, you can dig into that.
- 21:41And essentially, it's just called the same kind of commands after it.
- 21:44Also has a nice little, one of the things I always look for in a linter is the pragma that says ignore this line so that you can turn off the noise.
- 21:52Because every once in a while, there's going to be something that you want it to ignore.
- 21:56And I find as soon as you've got warnings popping up in your code, it starts to become broken window theory, right?
- 22:02Like, oh, there's a warning there.
- 22:03Oh, now there's two warnings there.
- 22:05Now there's three warnings there.
- 22:06And the next thing you know, you've got 100 warnings and nobody's doing anything about them.
- 22:09So I like to try.
- 22:10That's exactly the analogy I was thinking as well.
- 22:13And that's the way it is.
- 22:14Because especially in a team situation, you know, there's some people who are really bought into like CI and that kind of stuff.
- 22:22There's other people who are not really that interested and worried whether the test still passed or worried about whether the linters pass.
- 22:29And they're just going to go along.
- 22:30But certainly if that kind of stuff starts showing up, they're like, well, if it's already got warnings, I don't care about one more, you know?
- 22:37Yeah.
- 22:37So the zero warnings policy is pretty good.
- 22:39And this kind of thing helps you find that and check it easily.
- 22:42Right. So for people who are listening, you just put hash for comment, Blake 8 colon no QA on a line and then it'll skip that line.
- 22:50You know, there's times when you get advice that in general is good advice, like try accept past or something like that.
- 22:58You should never do that.
- 22:59Except for sometimes in little weird situations, you're like, no, I really just need this to not care if there's an error and just keep going.
- 23:06And we're just, you know, stuff and you'll you'll be stuck with this permanent warning.
- 23:10This file has an error.
- 23:11You're catching too broad of an exception.
- 23:13Like in general, yes.
- 23:14But right now I don't care.
- 23:16I accept that.
- 23:17Let's just keep going together.
- 23:18Yeah.
- 23:18And this kind of stuff really is great for making those warnings go away.
- 23:22And you can tie it into your PR process as well.
- 23:24So if I'm doing a poll review for something you've done where you've put this, we can have a policy that says there better be a comment above it that explains why you're allowed to ignore it in this session.
- 23:34So it becomes sort of two kinds of flag.
- 23:37It's a flag for pie flakes.
- 23:38And it's also a flag flag for your code reviewer to go, oh, we should explain why, you know, is this a valid case?
- 23:44Oh, yes, we do.
- 23:45This is this is the generic.
- 23:46So let's put a comment that says, yes, we meant to do this rather than I was just too lazy to fix it.
- 23:51Yeah, that's a really good point.
- 23:52Actually, I like that a lot.
- 23:53All right.
- 23:53On to another Sphinx, the Python documentation generator.
- 23:58Yeah.
- 23:58So this is a sort of bit of a left turn here.
- 24:01I will admit I am obsessive compulsive when it comes to comments and documentation.
- 24:06I did co-op in university, which is what I think they call paid internships in the US.
- 24:10And my third co-op term was with the same department as my second.
- 24:15And the first day back into the office, I said hello to everyone.
- 24:18And then my boss says, yeah, there's a bug in the code you wrote last term.
- 24:21Go fix it.
- 24:22And if my name had not been at the top of the commit, I would have sworn this was written by somebody else.
- 24:27The code was just hideous.
- 24:28And this is where I learned the whole comments are for other people is only half right.
- 24:32Sometimes you're that other person like eight months later.
- 24:35Just shifted in time.
- 24:37Yeah.
- 24:37Yeah.
- 24:37So I've become very, very, very retentive about it.
- 24:41And so what Sphinx is, is a mechanism for taking your PyDoc comments and turning it into documentation.
- 24:49And so if you were writing your comments already, it gives you a couple little formatting tags that you should take advantage of.
- 24:55But once you've done that, it's a lot of effort.
- 25:03So I've got a lot of effort to take advantage of the code.
- 25:10And so I've got a lot of effort to take advantage of the code.
- 25:12And so I've got a lot of effort to take advantage of the code.
- 25:13And so I've got a lot of effort to take advantage of the code.
- 25:15And so I've got a lot of effort to take advantage of the code.
- 25:17But one of the things I find I use a lot is you can highlight references to other parts of your code.
- 25:25So if I've got a class that I'm using here, an object that is some sort of class, if I note it properly in the comment, when the documentation is created, I can click a link and it'll take me through to it.
- 25:37So you can always find the cross references.
- 25:39Yeah, that's fantastic.
- 25:40Generates all sorts of different kinds of output.
- 25:43I personally only ever use the HTML, but it does EPUB, ManPages, LaTeX, if you want to spit out some PDF and some other things as well.
- 25:50Comes with a sort of a quick start command that builds all your cookie cutter files for what you need.
- 25:56It also allows you to intermix in general RST files.
- 26:01So if you want to write, say, a how-to guide separate from your code, but then suck in some documentation from your code, it allows you to mix and match that.
- 26:09So as an example.
- 26:10Tutorial or installing or something like that.
- 26:13Yeah, so I often will write, although Markdown is the most common format for the readme file on GitHub, I use readme.rst.
- 26:20And the reason I do that is because I usually use that same readme file as the intro file in my Sphinx doc.
- 26:28So I essentially just say, oh, go grab that readme.rst, pull it in here as the title page, add a little table of contents flag on the bottom of it.
- 26:37And the next thing you know, you've got some documentation.
- 26:39Yeah, that's really fantastic.
- 26:41It's great when you can get your work to multiply automatically.
- 26:45And obviously, you know, you've got to have been in the habit of doing this kind of stuff anyways.
- 26:50But if you were, adding a little bit of rigor to how you're writing the comments helps a lot.
- 26:55And I find particularly with things like APIs, right?
- 26:59Because you should be writing that doc string anyways that says this is what the call is.
- 27:02These are the parameters and what they mean.
- 27:04And it turns all that into a PDF document that you can, you know, hand off to managers or whatever, put up on your web page or however you like to do that.
- 27:12Yeah.
- 27:12For those who are not a fan of restructured text, there's the MyST markdown version or plugin.
- 27:20So you can do markdown as well these days.
- 27:23I have to check that out.
- 27:23I haven't played with that one yet.
- 27:24Yeah, yeah.
- 27:25It's pretty neat.
- 27:25You can even do inline restructured text within your markdown.
- 27:29So you're like, markdown, markdown.
- 27:30Oh, I need something a little bit more complicated.
- 27:32There's a way.
- 27:33I don't remember what the syntax is, but there's a way to, you know, inline a little restructured text.
- 27:38Excellent.
- 27:38And then fall back to markdown.
- 27:39Yeah.
- 27:40A little bit of an escape hatch.
- 27:41And then embed your HTML inside of your restructured text inside of your markdown.
- 27:46It's turtles all the way down.
- 27:47Excellent.
- 27:48Absolutely.
- 27:48The other thing, there are companion plugins for this as well.
- 27:52So one of the other libraries that I use is something called a Sphinx RTD theme, which is a read the docs style theme and plugs right in.
- 28:00And essentially, it allows you to generate your HTML looking like the read the docs site.
- 28:06And if you combine that with a git hook into read the docs, your read the docs will get automatically updated every single time you do your push up to git.
- 28:13So nice little combination there.
- 28:15Yeah, that's very cool.
- 28:16I do find it sort of meta that the Sphinx read the docs theme is hosted on read the docs.
- 28:21Yes.
- 28:22So fun.
- 28:25All right.
- 28:26PUDB.
- 28:28Is that the right?
- 28:29I have no idea how you're supposed to pronounce it.
- 28:31I always say PUDB, but that's fine.
- 28:33PUDB.
- 28:34Well, I do feel like a lot of these projects like this one, if you have too much of an acronym or you have too clever of a name, you really should put a, and here's the MP3 of how you say it.
- 28:43Hey, there you go.
- 28:44Yes.
- 28:44The little, I loved that answer with Linux.
- 28:47It was, hi, my name is Linus and this is how you spell Linux.
- 28:50And I couldn't understand his accent.
- 28:51It didn't clear anything up.
- 28:52It was fantastic.
- 28:53That's funny.
- 28:54I think I've mentioned up front that I'm old.
- 28:57So a lot of my early career was spent on Unix systems, SunSpark systems, that kind of stuff.
- 29:02And at the time there was this whole war between VI and Emacs.
- 29:06The war is over.
- 29:07I think VS Code won.
- 29:09But I picked VI because it was the only thing guaranteed to be installed on any of the systems I was telnetting into.
- 29:14And so I'm still very old school using Vim to this day.
- 29:18Yeah.
- 29:19I mean, you just said the word telnet.
- 29:20Come on.
- 29:20I'm keeping it.
- 29:21It was telnetting.
- 29:23It was pre-SSH.
- 29:24I know.
- 29:24Yeah.
- 29:24Now, hold on now.
- 29:25Did you do like Archie and Gopher before telnet?
- 29:28A little bit of Gopher.
- 29:29I'd never, I was never on an Archie system.
- 29:32I did a little bit of Gopher.
- 29:33Yeah.
- 29:33So.
- 29:34It could even go more old school than that, but yeah.
- 29:36Okay.
- 29:36Yeah.
- 29:36Great.
- 29:36So maybe these days you're SSH'd in, but same basic idea, right?
- 29:40Same basic idea.
- 29:40So why am I babbling about this?
- 29:43Well, what this means is because I use an editor rather than an IDE.
- 29:46And of course there's another place where you'll get letters because you can do everything in Vim
- 29:49script.
- 29:50So it's an IDE as well, but I tend to just use it as a letter editor.
- 29:53What that means is I tend to use a standalone debugger.
- 29:57So PUDB is one of those.
- 29:59It's a standalone debugger.
- 30:00I'm not sure why, but I've never been a big fan of PDB.
- 30:04The one that comes with Python.
- 30:06Part of that is because it's based on GDB, which I never liked either.
- 30:09I just need to see more screen, more code on my screen at a time.
- 30:14And so this is a TUI program, which is text user interface.
- 30:18And it's for, and if you want to, again, shout out to the old folks in the audience, it's
- 30:23got a very Borland turbo feel to it.
- 30:25So you can see the screen and the text that you're working on.
- 30:28And then there's a couple little side windows with your stack and your variables and set
- 30:32breakpoints, all the things you expect a debugger to do.
- 30:35And if you're stopped at a breakpoint, you can open up a REPL and it gives you full context
- 30:39of where you are, just like you would with inside of PDB.
- 30:41So I find oftentimes I'm doing more debugging in the REPL in context than I am actually in
- 30:46the debugger.
- 30:47Now, why this is a big deal for me is let alone the local.
- 30:52If I were using PyCharm or whatever, I can work locally.
- 30:54This will work anywhere where I can SSH into.
- 30:58So because it runs over the terminal, I can get into a machine.
- 31:02I can use this and have it work.
- 31:03And it means I'm using consistent environment across all the places I go.
- 31:07So I don't try to sell this to other people.
- 31:09When I manage teams, I don't say you will use this, but it tends to be my go-to.
- 31:14And I like to tell people that it's out there because you never know when you have to do that
- 31:19remote stuff.
- 31:20Yeah.
- 31:20A lot of times, you know, like it works on my machine and you get the certification even
- 31:25there.
- 31:25It works on my machine.
- 31:26And then I go to the server and it doesn't work.
- 31:29And I can't really figure out why, you know, especially when you're setting up a new environment,
- 31:34you're like, ah, trying to run the web app now and uWSGI over here and it just won't
- 31:39start or something like that.
- 31:40Right.
- 31:40So the ability to get a little more insight over SSH is pretty cool.
- 31:44Yep.
- 31:44Good.
- 31:45All right.
- 31:45Is this, I think this may be our last sort of, nope, second to last general, general library
- 31:52is talks.
- 31:52Yep.
- 31:52So this is a wrapper for all your tests.
- 31:56So it essentially builds virtual environments based on a quick little configuration file.
- 32:01So give you an example.
- 32:03Let's say I've got a line that says pi brace three, seven, three, eight, three, nine, three,
- 32:0810 dash Django 400.
- 32:11It knows how to split that apart and we'll do all the combinations of running Python three,
- 32:16seven with 400, three, eight with 400 and et cetera, et cetera, et cetera.
- 32:19And it creates a little virtual environment for each one of those combinations and then runs
- 32:24all of your unit tests inside of that virtual environment.
- 32:27So if you're doing library support across multiple Python versions or across multiple, say,
- 32:32Django versions, then you can mix and match these with just a few lines inside of a configuration
- 32:37file and it will go off and run all of this.
- 32:39And those virtual environments are created locally and cached.
- 32:43So although the first time it runs, it's dreadfully slow because it has to pip install all of your
- 32:48requirements in each one of those environments.
- 32:50Once that's set up, it just leaves them there, which means, you know, first time hurts.
- 32:55And after that, it's not too bad.
- 32:56It's one of those trade-offs between I want an absolutely clean from scratch reproducible thing.
- 33:02Yes.
- 33:03Also, when I get my job done.
- 33:04So if you're going to test across five different versions of Python, maybe don't try to pip install
- 33:09something like uWSGI or other things that have to compile for a great long while.
- 33:15Just, just leave it there.
- 33:17Because it does compile and leave it, you know, let it do it.
- 33:19Go get dinner.
- 33:20Come back and just never delete that virtual environment ever, ever again.
- 33:24Yeah.
- 33:25I suspect it has multiple levels of caching as well.
- 33:27Like if I pip install something and it's exactly the same version, it'll still be cached
- 33:31even on my machine, even if I recreate the virtual environment.
- 33:34So probably.
- 33:34There's levels of it.
- 33:35It essentially just creates a, I'm trying to remember.
- 33:38I think it's dot.
- 33:39It might even be dot VM.
- 33:40It might be dot talks.
- 33:42I can't remember.
- 33:42But it essentially just creates a directory and creates a directory inside of that for each
- 33:46one of your combinations.
- 33:47And then pip installs into that virtual environment.
- 33:50So when it runs the next time, it'll do upgrades and things like that.
- 33:52And if you need to, you can just go in and RMRF the directory and blow it away if you're
- 33:57running into trouble and need to start from scratch.
- 33:58It sounds interesting for a web app.
- 34:01It sounds super useful for a library package.
- 34:04That's where I tend to use it.
- 34:05So we'll talk later about one of my little bits of self-promotion here.
- 34:10But I've got a couple of Django libraries that are out there.
- 34:12And, you know, I support 3.6 through 4.0, I haven't got around to 4.1 yet, and three
- 34:18or four versions of Django in it.
- 34:19And every once in a while, you'll run into something that just doesn't work in some version
- 34:24that you think is generic.
- 34:25And it's become a lot easier since I've dropped support for 2.7 because there was a lot more
- 34:30crap having to deal with that.
- 34:31But that's how you catch those things.
- 34:32Yeah, absolutely.
- 34:33The last general one here is Pillow.
- 34:36This is semi-general.
- 34:38In case you're sleepy, you're programming too long and you just need to take like a little
- 34:41bit of a nap or what's going on here?
- 34:42That's what it's for.
- 34:43That's right.
- 34:44Pillow is for sleeping.
- 34:45It's also for image manipulation.
- 34:48So I don't do a lot of graphics stuff.
- 34:52My Batman logo, notwithstanding, was very early on in my programming career.
- 34:56But in fact, Django has a semi-dependency on this.
- 35:01You don't need it out of the gate.
- 35:03But if you're using certain features in Django, you do have to have it installed.
- 35:07There's a field in Django, which is the image file field.
- 35:09And it uses the Pillow library to make sure that the files that are associated with it
- 35:14actually are images.
- 35:15So this is a really deep, really cool image processing library.
- 35:19And I tend to just skim the surface of it.
- 35:22I use it for image and I use it for a couple libraries as well that deal with image thumbnails.
- 35:26But otherwise, I don't really touch it.
- 35:28But it tends to be one of those things that if I go through all of the virtual environments
- 35:33set up on my system, I'm finding, oh, it's installed in a lot of them.
- 35:36So it's one of those that's kind of common that I, because I don't do a lot of image stuff,
- 35:40it's not something that I'm playing with a lot.
- 35:43But it's very, very powerful.
- 35:44So if you're into this kind of stuff, this is a great library to play with.
- 35:47And if you're not, you may want just the surface level pieces for some web things like thumbnails.
- 35:54If you're running a website that accepts, say, image upload, for example.
- 35:58Yes.
- 35:59You obviously need this for the Django ORM field.
- 36:02That's the image type.
- 36:03But you could also say, if it's too large, let's just resize it.
- 36:06Or you can resize it to the same size all the time.
- 36:09You know, it drives me crazy when I go to sites where like, this image is 1500 by something
- 36:14and we only support 1280.
- 36:15It's like, just resize it then.
- 36:17Yes.
- 36:17Yeah.
- 36:19I've also used it in like in some command line scripts, which will like create like two
- 36:25or three versions of an image that is in the static files.
- 36:28Right.
- 36:28So it'll automatically go through.
- 36:30And like you said, okay, we've got our master that's, you know, 1500 by 1500.
- 36:33And it'll go create some extra ones or whatever.
- 36:36And you run a command and it'll run through all your static files and check that there's two
- 36:39or three size versions or that kind of thing.
- 36:41So again, if you're playing with the web, size is important.
- 36:44Right.
- 36:45So yeah, for example, if you want to have icons for your web app that can be saved to
- 36:50iOS home screens, you know, they want all these different sizes from like 512 down to 64.
- 36:54And if you're doing retina or high resolution, you can have different ones that get swapped
- 36:59in and out.
- 37:00And like, it'd be really nice to just automate all that.
- 37:02Right.
- 37:03That's right.
- 37:03Yeah.
- 37:03This portion of Talk Python To Me is brought to you by Microsoft for Startups Founders Hub.
- 37:10Starting a business is hard.
- 37:12By some estimates, over 90% of startups will go out of business in just their first year.
- 37:17With that in mind, Microsoft for Startups set out to understand what startups need to be successful
- 37:23and to create a digital platform to help them overcome those challenges.
- 37:26Microsoft for Startups Founders Hub was born.
- 37:29Founders Hub provides all founders at any stage with free resources to solve their startup challenges.
- 37:36The platform provides technology benefits, access to expert guidance and skilled resources,
- 37:41mentorship and networking connections, and much more.
- 37:44Unlike others in the industry, Microsoft for Startups Founders Hub doesn't require startups to be
- 37:50investor backed or third party validated to participate.
- 37:53Founders Hub is truly open to all.
- 37:57So what do you get if you join them?
- 37:58You speed up your development with free access to GitHub and Microsoft Cloud computing resources
- 38:03and the ability to unlock more credits over time.
- 38:06To help your startup innovate, Founders Hub is partnering with innovative companies like OpenAI,
- 38:11a global leader in AI research and development, to provide exclusive benefits and discounts.
- 38:17Through Microsoft for Startups Founders Hub, becoming a founder is no longer about who you know.
- 38:21You'll have access to their mentorship network, giving you a pool of hundreds of mentors across a range of disciplines
- 38:27and areas like idea validation, fundraising, management and coaching, sales and marketing,
- 38:33as well as specific technical stress points.
- 38:35You'll be able to book a one-on-one meeting with the mentors, many of whom are former founders themselves.
- 38:40Make your idea a reality today with the critical support you'll get from Founders Hub.
- 38:46To join the program, just visit talkpython.fm/founders hub, all one word, no links in your show notes.
- 38:51Thank you to Microsoft for supporting the show.
- 38:54This one, I'm going to vote this next one here as probably the best icon of all the things we're going to talk about.
- 39:05It's the Django Ninja.
- 39:06Yes, they've done a good job with it.
- 39:09There's a special flavor of marketing to open source projects, isn't there?
- 39:14Yes, there is.
- 39:15Yes, I think it's called none.
- 39:16We could talk for hours on why it's called the GIMP.
- 39:22Anyways, I find that because I'm a Django guy, I don't have a really strong need for single page applications.
- 39:30A lot of the work I find I end up doing, Django templates is good enough for many of the pages in the application.
- 39:37But then there's going to be some pages where you want to do fancier stuff.
- 39:40So I often use view.js so that I can do best of both worlds there.
- 39:45So I, you know, this is kind of a multi page application.
- 39:48So those pages that require SPA kind of feel to it, they have view on it.
- 39:53And those that don't, I just use generic 1.0 web style stuff.
- 39:58So in the 2.0 world, you need REST APIs and enter Django Ninja.
- 40:05This is what it does.
- 40:06So it sits on top of a Django view and essentially RESTifies the view.
- 40:11And I, when I say on top, I mean that literally you actually use decorators.
- 40:15So you write a view, decorate it with an HTTP method like get or put, and then that view outputs JSON instead of your usual HTML.
- 40:24The library is built on top of Pydantic.
- 40:27So it relies heavily on the types to help it define the JSON that comes out.
- 40:31You write a little schema file that says the names and types of the fields for the JSON output.
- 40:36And it's got a schema that is based on top of Django models.
- 40:43So this works a lot like a model form if you've ever used one of those.
- 40:47So let's say I've got, you know, a Django model called a customer.
- 40:50I can create a schema called customer out that inherits from Django, from Ninja's model schema.
- 40:56I create a subclass called config, which is kind of like the meta subclass in Django.
- 41:00And it sets the model attribute to point to the customer.
- 41:04And if I want to use all the fields in the model, that's it.
- 41:07It's basically just the three lines.
- 41:08And you can customize that.
- 41:11So like model form, you can say, don't use all the fields, only use these fields.
- 41:15Or you can add fields if there's things like, say you want a field called name that combines first name and last name.
- 41:21You can build something that computes that so that it doesn't actually use the model directly.
- 41:25So oftentimes you end up having to write a couple of these because you often need the inbound and the outbound to have different fields in it.
- 41:33So a common practice is to have your schema for customer be called, say, customer out or customer in.
- 41:40There's nothing that enforces that, but that's how they do it in their documentation.
- 41:43And I've kind of just sort of picked up the habit as you go along.
- 41:46Yeah, it's so tempting sometimes to think, oh, we can just exchange the same model inbound and outbound in so many ways that just doesn't pan out.
- 41:53Even if you're using something like Beanie or SQL model or something where like literally the Pydantic model goes in the database as well.
- 41:59Often that's not even the same thing because, for example, your inbound create user, you want a password.
- 42:04Your response, you'd never want to send the user the password directly back.
- 42:09You shouldn't even know what it is.
- 42:10The ID field is the most common thing, right?
- 42:13Like every model's got one and there's hacks you can do to say, oh, if it's zero, it's a create, you know, and that's a common way of doing things.
- 42:19But for Django Ninja, what I often just do is just leave it out.
- 42:23And because there are objects like anything else, you can inherit it, right?
- 42:26So you can start with the in and then add the ID to it.
- 42:29So there's ways around it.
- 42:30It's what it comes down to.
- 42:31The Django Ninja can be plugged into an existing Django app.
- 42:35It's not like a separate thing that I run that is also correct like Django.
- 42:38It's actually just stuff that sits on top.
- 42:41It's got its own routes.
- 42:42So essentially you're building Django views, but you're decorating them with the Ninja.
- 42:47And when you decorate it, it automatically collects all of those underneath one path in the URLs file.
- 42:54So you set up a router.
- 42:56Usually you set it to say something like slash API.
- 42:58And so back to that customer example I was using, if I added a view for create customer, I set it to the customer path.
- 43:05And because it's decorated, it automatically ends up under slash API slash customers.
- 43:09So it takes care of a lot of that for you.
- 43:12It looks like it also has async support as well, which is pretty neat.
- 43:16It started from scratch with the asyncio.
- 43:18I'm not 100% sure of this, but I think there's a couple former DRF guys on here.
- 43:23And I think it was a rewrite from scratch with async from the beginning and Pydantic.
- 43:28So I think it's sort of the next generation.
- 43:30The key writers weren't in the DRF.
- 43:33They obviously took a lot of learning from what was there and what worked and what didn't work.
- 43:37It's pretty familiar to FastAPI type of programming.
- 43:41And it has a lot of uses of type hints, right?
- 43:44For example, you have an API function that takes a delay.
- 43:47It says it's an integer.
- 43:48I suppose it probably actually parses that and validates it and so on.
- 43:51Yes.
- 43:52And it's particularly useful as well because by the time you're getting things down, you
- 43:56know, your web string, everything is string, right?
- 43:59Like when you post it, it's all coming up in the string.
- 44:01So somebody somewhere has to go, oh, that's supposed to be an int and try to convert it.
- 44:05So it takes care of a lot of that kind of stuff.
- 44:07And in fact, one of the reasons there's so little coding in this, particularly with the
- 44:12schema definitions is because you're just, if you've already got that model file, it knows
- 44:16the Django field is a text field or is a Boolean or is an integer.
- 44:20And it goes, oh, okay, I know what to do with that, right?
- 44:22So it essentially is building on top of that stuff that's already there for your existing
- 44:26code.
- 44:27That's really cool.
- 44:27And you basically so much of the validation has already happened.
- 44:31By the time it gets to you, you're much closer to the right answer.
- 44:34You don't have to check so many things.
- 44:35Much less code you have to write.
- 44:36Yeah, for sure.
- 44:37And if that's not good enough on top of all of this is it comes with a web-based debugging
- 44:42view.
- 44:42So it lists all of your registered calls.
- 44:44You can go and click on things.
- 44:46Authentication is required for the call.
- 44:48And it's got a whole bunch of stuff in the back end for that as well.
- 44:50You can click through and say, okay, I push this button and paste in my key or hit the
- 44:56login button in my Django and then use it that way.
- 44:58So it allows you to sort of see all of this and it includes in it the output in curl.
- 45:03So as you're using the debugger, you can copy and paste the line into your terminal and then
- 45:08run it in curl.
- 45:09It's a quick little tool for helping you sort of figure it all out.
- 45:12Pretty powerful.
- 45:14It even has an interesting sync to async adapter decorator to allow you to work with the ORM
- 45:20until it's kind of migrated along.
- 45:22Shouldn't be as necessary now that 4.1's out because that was one of the major changes in
- 45:26Django 4.1 is they added async to the ORM.
- 45:28So yeah, that's really a big deal.
- 45:30Yeah.
- 45:30I actually remember one of the comments I saw on Twitter after 4.1 came out was, oh, the
- 45:34Django guys, the ninja guys are going to love this.
- 45:36I'm like, yeah.
- 45:38Sounds like it.
- 45:39All right.
- 45:40Julio asks, is this as complete as Django REST framework?
- 45:43And Nick Harvey says it looks lighter than Django REST framework.
- 45:46Maybe give your thoughts and then we can maybe pull up the next topic.
- 45:50Why don't we punt seeing as the next one is the Django REST framework and we can talk about
- 45:56it as we go along.
- 45:56The short answer is no, it's definitely not as complete.
- 45:59So the Django REST framework or DRF to its friends is Ninja's granddaddy.
- 46:04And it for a long time was the go-to REST library in Django.
- 46:08Like Ninja, it breaks things down into serializers.
- 46:11That's the equivalent of a schema and then views and routes.
- 46:14And like Ninja, it supports decorators, but it also supports classes.
- 46:18But because it's older, it doesn't support the type mechanism.
- 46:21So you have to do a little more boilerplate code to get this going.
- 46:24You have to do a few more things with the serializers.
- 46:27It tends to, I was going to say it tends to favor a class-based approach.
- 46:32I'm not sure if that's true.
- 46:33When I write with the DRF, I tend to use the class-based approach.
- 46:36There are other mechanisms in there, but it seems to be the one that felt natural for
- 46:40me there.
- 46:40It has a concept called a view set class.
- 46:43And this is really where it shines because you define an object, which is an endpoint.
- 46:48And then you can say inside of that endpoint, I want to do list and create and retrieve and
- 46:53update and destroy.
- 46:54And you tie that into your query set.
- 46:56And it does all of those things for your object.
- 46:59So back to my customer example, I could build out that customer endpoint.
- 47:03And then I can say, I want to use list, create and retrieve.
- 47:06And it'll allow me to create a customer or list all the customers or get a specific customer.
- 47:11And it does it based on mix-ins.
- 47:13So you can mix and match what, which of those choices you want.
- 47:17So it's sort of this composing kind of mechanism.
- 47:20I question or sort of an observation here from, to be fair, I haven't done much with
- 47:24DRF.
- 47:25But it feels to me like a lot of what you're getting with DRF is like, I have a database
- 47:31table.
- 47:31Let me do the rest things to it.
- 47:34Like I want to be able to do a get against and get all of them or get against an ID and
- 47:38get individual ones.
- 47:39I want to be able to do a post and maybe create one where it's a lot.
- 47:43It seems like it's very structured around CRUD, but over API.
- 47:46Is that accurate or no?
- 47:48There are ways around it.
- 47:49So the concept of a serializer does not have to be tied to a model, but this is a tool for
- 47:55Django.
- 47:55So it's not really a surprise that it's kind of tied to the models.
- 47:59And that's for the most part, even with Ninja, I find 80% of what you're doing over the rest
- 48:04API is mapping to your database models in the first place.
- 48:08particular if you're doing it for something like React, because the things in your database
- 48:12are usually what you're showing up on the screen.
- 48:15So it usually tends to map fairly easily.
- 48:17And there's enough depth there that if you need to introduce new fields or do something
- 48:22custom, it doesn't handcuff you.
- 48:24It does allow you to do a lot of this stuff easier, though.
- 48:28So like if you are sticking with things that map nicely to your database, then, you know,
- 48:32it requires far less code is what it comes down to.
- 48:35I guess to get back to that sort of question, it is just definitely lighter weight.
- 48:40It definitely I find requires less code than the DRF because it takes advantage of things
- 48:45like the typing that which DRF is older and it didn't exist.
- 48:48And it was compatible with Python 2 and some of the stuff where those type pieces weren't
- 48:52there.
- 48:53DRF has a much richer output listing.
- 48:56So if I were going to need something that was not JSON based, for example, over REST, I might
- 49:02go back to the DRF.
- 49:03So it's got mechanisms for either through it or through plugins, YAML, XML, MessagePack, Excel,
- 49:10even LaTeX, right?
- 49:12So there's libraries that go with these libraries that make this that sort of connect these things
- 49:16together.
- 49:16So, yeah, there's there's a lot of depth here.
- 49:18The only place that the DRF makes me really, really nervous and is the permission mechanism.
- 49:25I have shot myself in the foot a few too many times doing it.
- 49:29It's fine by default, but then as soon as you start mucking with it a little bit, it seems
- 49:34to open up a whole bunch of things.
- 49:36You have to make sure once you start playing in that space that you do it correctly and test
- 49:40properly.
- 49:41Otherwise, you could have gaping holes.
- 49:43Ninja seemed to be a little more locked down by default.
- 49:46So I kind of liked that.
- 49:47But that could just because I'm missing toes from playing with the DRF over the years.
- 49:51You know what the guns look like?
- 49:54Yeah.
- 49:54Tishar asked, I think this is probably in reference to the Django Ninja.
- 49:58It looks similar to FastAPI and how would you compare them?
- 50:00It's a lot of sort of similar kind of concepts.
- 50:03And in fact, you know, either between FastAPI, the typing mechanisms, is FastAPI directly based
- 50:10on Pydantic or just deeply influenced by it?
- 50:13I think just I feel like Pydantic became very popular because of FastAPI, but they're separate
- 50:18projects.
- 50:18Okay.
- 50:19Separate maintainers.
- 50:20Ninja is built on top of Pydantic.
- 50:21So, you know, they're all being inspired by the same thing and trying to solve very similar
- 50:25problems.
- 50:26So it's not surprising me that people are seeing.
- 50:28Yeah, exactly.
- 50:29And at the bottom of the Django Ninja site, it says, this project was heavily inspired by
- 50:33FastAPI.
- 50:34Yeah.
- 50:34My feeling is like, if you're already doing Django and you want to add APIs and you like
- 50:39FastAPI, this might be a really good option because it brings so much of a similar model,
- 50:44but you don't have to now maintain two apps that like run separately or version separately,
- 50:48all that.
- 50:49Yeah.
- 50:49I also find it's got a little bit of a flask flavoring as well, right?
- 50:54As to how some of those pieces.
- 50:55And again, that's the same thing, FastAPI, using the decorators, using the types, right?
- 50:59So it's a similar kind of influences if nothing else.
- 51:03Yeah.
- 51:03Very neat.
- 51:03I think.
- 51:04All right.
- 51:05Django Grappelli.
- 51:06What's this one for?
- 51:08All right.
- 51:08So a little bit of a tangent here for a second.
- 51:10Django is named after a jazz guitarist named Django Reinhardt.
- 51:15And he was often a duo working with somebody named Stefan Grappelli.
- 51:19So if you're looking for a name for a library for Django, Grappelli is a good fit.
- 51:24So now great.
- 51:26We've got a name.
- 51:27What does it do?
- 51:29This is actually a reskinning of the Django admin.
- 51:32So the Grappelli folks have taken advantage of the fact that the Django admin is built on
- 51:37top of the Django templating mechanism, and they've rewritten all those templates using
- 51:42a new style.
- 51:43The style still feels a little dated, but not quite as dated as the Django admin.
- 51:48So it's a step in the right direction.
- 51:49It does logical things like a lot of the top right-hand corner, you know, reset password
- 51:55view page.
- 51:56A lot of that stuff gets moved into like little dropdowns and that kind of thing.
- 51:59The coloring is a little more consistent.
- 52:01It uses a footer.
- 52:03So like the actions are always in the same place.
- 52:06It just generally feels cleaner to me.
- 52:08I often use the Django admin for like my backend support people.
- 52:13Don't tend to use it customer facing.
- 52:15So, you know, folks who are trainable and I find that when I've used it with them, Grappelli
- 52:21feels better for them.
- 52:22They just, it feels a little more intuitive.
- 52:24So it's just a, it really is just a skin, but it's a nice skin and it's a really easy install.
- 52:30You have very little you have to do.
- 52:32You get a couple, a couple of changes to your settings.py file and it's all done for you.
- 52:36So.
- 52:36Yeah.
- 52:36And also it looks like, you know, you hear skin, you think, oh, it's pretty, but it also
- 52:41seems like it adds more functionality.
- 52:43Like it adds autocomplete for foreign keys and relationships.
- 52:46It adds like a tiny MCE, which is rich text editor.
- 52:50It adds reordering through drag and drop.
- 52:53I mean, those are a little bit more than just pretty, right?
- 52:56It's modernization of the interface is really what it comes down to.
- 52:58Yeah.
- 52:59Yeah.
- 52:59Very cool.
- 52:59So another random Django bit of history to go along with the musicians was created in Lawrence,
- 53:05Kansas, right?
- 53:06Yes.
- 53:06By the newspaper.
- 53:07That's where I went to college.
- 53:09So.
- 53:09Ah, okay.
- 53:10Right, right down the street from the home of Django around the same timeframe.
- 53:14There you go.
- 53:14Right.
- 53:15All right.
- 53:15So what's up next?
- 53:16Import export.
- 53:17So it's like a distributor.
- 53:19It's going to make some money off selling rugs or what's the deal?
- 53:22Rugs.
- 53:22Yes.
- 53:22Sure.
- 53:23Rugs.
- 53:23All right.
- 53:26Unlike Grappelli, where you have no idea by the name, this one's a little better.
- 53:31You might have an idea what it does, right?
- 53:34Less creative, more straight to the point.
- 53:36Like this is a utilitarian name.
- 53:37I can import or I can export stuff from Django.
- 53:40Okay.
- 53:40That's exactly it.
- 53:41How does it work though?
- 53:41What?
- 53:42Like, I mean, not just a database dump, probably something more interesting than that.
- 53:46Yeah.
- 53:46So it's built on top of Tablib, which is a Python library for tabular data formats.
- 53:51And essentially it maps things out of your ORM to whatever Tablib can handle.
- 53:57So that's Excel, JSON, YAML, Pandas, HTML, JIRA, TSV, ODS, CSV, DBF.
- 54:04And I even know what some of those acronyms mean.
- 54:06It's essentially your go-to place for spitting stuff out.
- 54:10So like Ninja and like the DRF, it has this, what it calls resources.
- 54:15This is like our schema.
- 54:16And it essentially does that mapping thing again.
- 54:19So you can say, I want to take this model and I want to map it to this mechanism.
- 54:24And then it'll, you just run it and say, export this to JSON and you'll get it in JSON.
- 54:28Export this to YAML and it'll export it to YAML.
- 54:31There are mechanisms inside of the resources, similar to what I was talking about in Ninja
- 54:35and the DRF to do compositing.
- 54:37So if I want a name field, which is based on first name and last name, it'll combine all
- 54:42those things and give that to you together.
- 54:43One of the things that I like most, I haven't used this library a lot, but when I have used
- 54:47it, it's because of this feature, which is it integrates nicely with the Django admin.
- 54:52So you essentially can build one of these resources.
- 54:55Let's say back to my customer example, let's say you want to be able to import customers.
- 55:00You can essentially define the resource and then you add a mix in to your customer admin
- 55:06object and it automatically gives you an import button on the client screen.
- 55:10And when you hit that import, it'll upload, you give it the CSV file.
- 55:13It gives you a little screen that says, oh, this is the data that's coming in.
- 55:17And you can say, yes, I like it.
- 55:19And it'll put it into the database.
- 55:21And it's smart enough to know the difference between creates and updates.
- 55:24So depending on whether or not your ID field is blank, this is back to that zero thing we
- 55:29were talking about before.
- 55:30It will actually update things that are there.
- 55:32So if you've got a situation where you've got a client phone book or something that's maintained
- 55:38through some other system and you need to keep them in sync, this is a quick and easy way
- 55:43to do that.
- 55:44throw it together with a management command and stick it in a cron job and you can constantly
- 55:50push these things through.
- 55:51It might sound really simple, but the ability to say, hey, business person or manager or salesperson,
- 55:56you can just take your CSV and upload it.
- 55:59You don't have to bother the dev team to add your data.
- 56:03Like that's pretty fantastic, actually.
- 56:05One of my clients is a very lightweight CMS.
- 56:08And of course, when they sign up a new customer, they don't want to have to input their customer's
- 56:14data by hand.
- 56:14Right.
- 56:15So you can say, oh, go.
- 56:16You have it in an Excel file somewhere.
- 56:18Great.
- 56:18Save as put it in this format.
- 56:20Suck it all the data in.
- 56:21So it can make a big difference.
- 56:23Yeah.
- 56:23And the preview, make sure this looks good.
- 56:25Like that's also an important part.
- 56:26That's a lifesaver, right?
- 56:28You know exactly what you're getting in there.
- 56:29You can check it rather than it going, you know, running havoc all over your database.
- 56:34Which it probably would if people just randomly start uploading stuff.
- 56:38Exactly.
- 56:39And that might make your app crash, but you could use the Django debug toolbar to figure out
- 56:42what's going on.
- 56:43Look at you, king of the segues.
- 56:44Yes.
- 56:45Oh, yeah.
- 56:46So the next one here is, as you said, this is the Django debug toolbar.
- 56:50So this is a plugin for Django that overlays over your views and gives you all sorts of
- 56:55information what's going on in your page.
- 56:57The installation is a little more involved than, hey, just add this to settings.py because
- 57:02you need some middleware and some static files and things.
- 57:04But once you've, the instructions are good though.
- 57:06So as long as you follow the instructions closely, you'll be okay.
- 57:09And once you got that going, it pops up in the top right-hand corner.
- 57:12I think that's actually configurable.
- 57:14And it has a list of panels.
- 57:17It comes with, I think it's 12 of them.
- 57:20And then there's a whole bunch of plugins that you can do as well.
- 57:22So for example, there's history information.
- 57:25A useful one I find I use a fair amount is the time panel.
- 57:28So it shows you the breakdown of where, you know, where your call has been spent.
- 57:33The one you've got up on the screen there right now is the one that saves my bacon all
- 57:37the time, which is the SQL panel.
- 57:39So it shows you exactly what queries were run, how long each of them took.
- 57:42I understand enough database stuff to just get me in trouble, right?
- 57:46The ORM provides this nice abstraction that I find really helpful, but I'm aware that I'm,
- 57:50you know, sometimes I'm doing things that aren't efficient in SQL.
- 57:53Or you don't know you're doing something inefficient, right?
- 57:55You might not be aware you forgot a join.
- 57:58Exactly.
- 57:58And now you have this N plus one problem where you're like, I thought I did one query.
- 58:01Why are there 50 queries, 51 queries on this page?
- 58:03Exactly.
- 58:03You know?
- 58:04So when you've got that sluggishness, you go to the time panel, you see that you're
- 58:08spending all your time in the SQL space.
- 58:10Then you go to the SQL panel and you go, oh, okay.
- 58:12So this is where it is.
- 58:13I'm beating my database to death.
- 58:15Why?
- 58:15And then you can figure out, like you said, oh, I need to add an index or, you know, I
- 58:19need to change the join or whatever.
- 58:20Right.
- 58:21So in the Django debug toolbar, it has a time section that says CPU and total.
- 58:25And if the CPU time is the majority, then it's your Python code.
- 58:29If it's not, then it's like, what are you waiting on?
- 58:32Are you waiting on a database or an API?
- 58:34Or go look somewhere else because it's not your code exactly.
- 58:37It's an external resource that's holding it up, right?
- 58:40Yeah.
- 58:40So it helps you sort of pull these things through it.
- 58:43And it's information that's available in any profiler as well, but it's there in combined
- 58:47with other things.
- 58:48So you don't have to go do it separately.
- 58:49So there's other ones there.
- 58:51Things like caching, headers, signal registering, logging, the list goes on.
- 58:56And then it's a published mechanism for how you build your own panels.
- 59:00So even on the main site, there's another 19 panels with instructions on how to write your
- 59:05own.
- 59:06And if you go digging, there are people that aren't in the official list as well.
- 59:09So there are dozens and dozens of them out there.
- 59:12For sure.
- 59:12Probably in the awesome Django.
- 59:14I bet.
- 59:15You know, I bet there's like, there's a whole thing on the toolbar.
- 59:19Yeah.
- 59:20It was right there at the top where you're testing.
- 59:21That's right.
- 59:22Yeah.
- 59:22Yeah.
- 59:22I thought maybe there'd be a list of plugins as well.
- 59:25Oh, a subset.
- 59:25Yeah.
- 59:26Yeah.
- 59:26Yeah.
- 59:27Yeah.
- 59:27Like a subsection.
- 59:28But yeah, these things are really nice and I find them to be quite useful on the, you
- 59:32know, they've got different, I got the whole list here.
- 59:34Like profiling.
- 59:35That's kind of cool.
- 59:36I guess one also, one really quick thing you might want to touch on for everybody is it might
- 59:42not be best to put this in production.
- 59:44Yes.
- 59:45I think there's a rule against turning this off in production.
- 59:48If I remember correctly, it will not run if debug is set to true.
- 59:51It's to false.
- 59:52Excuse me.
- 59:52So by out of the box.
- 59:54So if you're, if you're following best practices of setting debug to false in your production environments,
- 59:59it won't be there.
- 1:00:00It doesn't hurt to belt and suspenders that though.
- 1:00:02I usually put this, if I'm using it, I usually put it inside of my, you know, requirements
- 1:00:07dash dev.
- 1:00:08And I have a little hack I use to build up my installed apps that I add to it from local
- 1:00:14things, which there's a podcast in and of itself there as to how you configure in different
- 1:00:18environments.
- 1:00:19But I often only add this to that piece or hackishly commented in and out as I need to.
- 1:00:24It's a little bit of a crude lever, but if it's not in the requirements.txt or pyproject.toml
- 1:00:30or whatever, you're not going to accidentally run the toolbar if it's not installed.
- 1:00:33So don't install it in production.
- 1:00:35Yep.
- 1:00:35Exactly.
- 1:00:36Effective, right?
- 1:00:36Yep.
- 1:00:37How about some local flavor?
- 1:00:38Yep.
- 1:00:39Local flavor.
- 1:00:40So this package eons ago used to actually be inside of Django and it just got kept getting
- 1:00:46bigger and bigger.
- 1:00:47And essentially this is the localization information for different countries.
- 1:00:52So for example, the CA folder is for Canada, which is where I'm from.
- 1:00:56It includes a list of all the provinces.
- 1:00:58That's what we call states.
- 1:01:00It's got a widget for postal codes, which is what we call zip codes and social insurance
- 1:01:05numbers, which is what we call social insurance numbers.
- 1:01:07Although we use a different acronym for whatever reason.
- 1:01:10So essentially, if you're using things like shipping addresses, you want local flavor.
- 1:01:15It's mostly just giant dictionaries and collections of tuples, but it's useful data that often you
- 1:01:22would end up writing yourself.
- 1:01:23And it's always nice when somebody else is maintaining it.
- 1:01:26And for any fellow non-Americans listening, it is spelt without the U.
- 1:01:30I'm not sure if it was intentional when they picked the name that the name itself needs
- 1:01:34internationalization or whether that was just an amusing little accident, but that's what it is.
- 1:01:40So even just having the country codes is great.
- 1:01:42Is this something that maybe makes sense to use outside of Django or is it?
- 1:01:45I'm not sure how easy.
- 1:01:47I've never done it.
- 1:01:49It might work.
- 1:01:50It really is just a long dictionary and collections of tuples, but some of it's kind of Django
- 1:01:55Django-esque.
- 1:01:56For example, the list of provinces is set up so that you can use it easily inside of
- 1:02:01a dropdown in a choices field.
- 1:02:03Got it.
- 1:02:04You might be able to take advantage of it without that, but it's formatted in a fashion that makes
- 1:02:08sense in the Django world.
- 1:02:09Good to know.
- 1:02:10All right.
- 1:02:10We already talked about the import and the export buttons, but even more buttons on the admin
- 1:02:15if we want them.
- 1:02:16Even more buttons.
- 1:02:16Yes.
- 1:02:17You can always do with more buttons.
- 1:02:18So I've only used this one a couple times, but it saved me a whole bunch of work.
- 1:02:23So I thought it deserved a mention.
- 1:02:25The Django admin area pretty much is mapped to your model objects.
- 1:02:30It really is just sitting on top of your ORM.
- 1:02:32And every once in a while, you're going to need something that you want your support staff
- 1:02:37to be able to do that isn't really directly tied to the ORM.
- 1:02:41And in fact, I've seen libraries out there that hack things, creating fake objects inside
- 1:02:46of the database so that they will show up in the ORM properly.
- 1:02:49And instead of doing that, you use admin extra buttons.
- 1:02:52So this is really just a way of inserting views into your admin.
- 1:02:57And it's got a quick little mechanism for forms so that if you hit a button, it'll pop up the
- 1:03:03form for you.
- 1:03:04You can fill in the form and then it'll give you the data from the form as a callback.
- 1:03:08And then you can do whatever you want with it.
- 1:03:10So where I've used it recently was I had a complex count creation mechanism that has to create
- 1:03:16like five or six different objects that have to be tied together.
- 1:03:19So instead of having my support staff going in and using the admin to create each one of
- 1:03:23those separately, I gave them a little button.
- 1:03:25They fill in the form and it creates all the objects and ties them together nicely.
- 1:03:29And actually just a couple of days ago, I added one, which was sometimes I'll have in
- 1:03:34production code, like a couple of accounts that are like sample accounts that you want
- 1:03:38to reset the data to a known state so that you can hand it off to demo somebody or quick
- 1:03:42if you're trying to sell a client or something.
- 1:03:44So this doesn't even require the form.
- 1:03:46There's a mechanism in here called confirm action, which essentially is it gives you the
- 1:03:50button.
- 1:03:50It pops up a little thing that says, are you sure you want to do this?
- 1:03:53And if you say yes, it goes off and calls your little five lines of code.
- 1:03:56So it's nice and clean.
- 1:03:59The form rendering is ugly is the word, but that kind of fits with the traditional,
- 1:04:05traditional.
- 1:04:05It's traditional.
- 1:04:06It fits very nicely with what's inside of the Django admin.
- 1:04:09It doesn't look out of place, but there's nothing sexy with it.
- 1:04:12I have not used it in combination with Grappelli yet.
- 1:04:15So it'd be interesting to see what happens in that case.
- 1:04:18There's basically very little styling on the form, but it has saved me a whole bunch of
- 1:04:23code.
- 1:04:23And oftentimes I find when you're writing things for your support staff, they're spending 90%
- 1:04:28of the time inside of the admin anyways.
- 1:04:30So pulling them out of it for one special page is a bit of a pain.
- 1:04:35And you usually have to write a whole bunch of extra stuff on top of it.
- 1:04:38And so this, because it inserts it right inside of the admin means you don't have to get them
- 1:04:41into a different place.
- 1:04:42And they're in an interface that they know how to use and makes things easier.
- 1:04:46So very much like that import export.
- 1:04:48If import export didn't exist and give you that button up top, this would be a way of building
- 1:04:52it.
- 1:04:53Really nice.
- 1:04:53And it's very decorator heavy.
- 1:04:55It is.
- 1:04:56Yes.
- 1:04:56Yeah.
- 1:04:57Everything's a button or a view.
- 1:04:59And you've got up on the screen there, right?
- 1:05:01That's the confirm that I was talking about at the top.
- 1:05:03You call it decorate the view.
- 1:05:06And that's because in this case, you're inserting it inside of the actual Django admin model pieces,
- 1:05:11right?
- 1:05:11So you have to essentially, the decorators are doing the registration to say, okay, this is
- 1:05:17extra code for the admin.
- 1:05:18It's how they're hooking it all.
- 1:05:20Yeah, it looks very, very useful.
- 1:05:21All right.
- 1:05:22We've got a couple more.
- 1:05:23Yep.
- 1:05:23This one, I think I know the guy who created this one.
- 1:05:25Yes.
- 1:05:25So this is a little bit of blatant self-promotion.
- 1:05:28This is Django All, A-W-L, like the leather working tool.
- 1:05:32And yeah, this is one of mine.
- 1:05:33If I had been aware of the Django extensions library at the time that I wrote this, I probably
- 1:05:38should have just contributed to the Django extensions library.
- 1:05:40It would have been the smarter thing.
- 1:05:41But at the time, I wasn't familiar with it.
- 1:05:44And since then, I've been maintaining this beast.
- 1:05:46It's a general collection of a whole bunch of crap, like extensions that I just found
- 1:05:52I was writing over and over again every single time I went to a client.
- 1:05:54So one of the times I was doing that, the client, I went to the client and said, let's open source
- 1:05:59this stuff.
- 1:06:00And they went, okay, great.
- 1:06:00And since then, I've been doing this.
- 1:06:02You sell widgets.
- 1:06:03You don't sell Django admin extensions.
- 1:06:05Like, let's make the world a better place and contribute back ideas.
- 1:06:08Exactly.
- 1:06:09This will hurt you.
- 1:06:10Big list of random crap, right?
- 1:06:12So a bunch of mix-ins, CSS color utilities, some context processor pieces.
- 1:06:18Custom admin commands.
- 1:06:19Yep.
- 1:06:20There's decorators in here that make sure that you're doing a post with JSON content.
- 1:06:25So that if somebody's calling your view in the illegal way, it screams ranked models.
- 1:06:31So one, two, three, four, five, and gives you a little thing inside of the admin to move
- 1:06:35them up and down.
- 1:06:36Some testing tools.
- 1:06:38So there's a custom test runner in here, which adds a whole bunch of features for doing
- 1:06:42things like getting out information out of the Django admin and checking that the fields
- 1:06:47and the admin are correct.
- 1:06:48So really just sort of a little catch-all that's out there.
- 1:06:51But yeah, it's here and I happily take PRs.
- 1:06:55So go find it, break it.
- 1:06:58If you find it useful, great.
- 1:06:59If you don't, send me a note and tell me why not and I'll fix it.
- 1:07:02Yeah.
- 1:07:02Lovely.
- 1:07:03It looks really useful.
- 1:07:03I'll stop with the shameless promotion after this one.
- 1:07:07Hey, I'll keep going.
- 1:07:08These are good.
- 1:07:08This is a Django airplane.
- 1:07:10So I used to, I used to have to do a lot of traveling when I was consulting and being on
- 1:07:17a plane is a great place to get some uninterrupted four or five hours of coding in unless you're
- 1:07:21coding on the web.
- 1:07:22And particularly in the olden days when you didn't get wifi on the planes and even now it's
- 1:07:26spotty and expensive.
- 1:07:27You can't really count on it.
- 1:07:28Yeah.
- 1:07:28Yeah.
- 1:07:28So what I found was, you know, if I'm using something like bootstrap from a CDN, I would
- 1:07:34get in and I'd be trying to run my Django site and be like, oh crap, nothing's looking right
- 1:07:38because the CDN is there.
- 1:07:40So this is essentially a little tag that you use anywhere where you would use an HREF for,
- 1:07:45you know, links or style sheets or whatever.
- 1:07:47And then there is a setting that defines the mode of the system.
- 1:07:53And if you're in build mode, anytime these tags find it, they'll go off, they'll grab the
- 1:07:58content and they'll save it to the disc and just runs the page normally.
- 1:08:01And then when you're in cache mode, instead of trying to get to the internet, it pulls it
- 1:08:05off of the disc.
- 1:08:06So it's nothing terribly complicated, but it solved a particular problem that I was having
- 1:08:12a lot.
- 1:08:12And now that I'm not really traveling, I'm not touching it as much, but if folks out there
- 1:08:18find it useful.
- 1:08:19People travel less.
- 1:08:20I don't know.
- 1:08:20It's kind of weird.
- 1:08:20Yeah.
- 1:08:21There's been a few changes.
- 1:08:22Life has changed.
- 1:08:23There have been changes.
- 1:08:25Although I did travel recently in a pandemonium.
- 1:08:27So if you want madness, you can go back and try to fly.
- 1:08:30Yeah.
- 1:08:30So this is really nice.
- 1:08:31So basically you say airplane, instead of a static ref to a CDN, you say airplane and then
- 1:08:36the reference to the CDN and either it just passes through or it can download and then
- 1:08:40pass through, or you can say I'm in this development mode.
- 1:08:43Just, I don't have, I'm either at like a coffee shop or I'm traveling or whatever.
- 1:08:47And just let me, I'm, you know, another good use case, not just airplane, even though that's
- 1:08:51the name is if you're at a hotel with notoriously bad internet and you're traveling for work and
- 1:08:57you got to get a presentation or new version ready for the morning, like that can be a super
- 1:09:01frustrating experience.
- 1:09:02It essentially moves anything that's normally on your internet down to your hard drive.
- 1:09:05If you've tagged it properly and there's a couple of management commands as well.
- 1:09:09So like, it'll tell you what's in your cache and you know, how to see it and how to wipe
- 1:09:12it out if you need to.
- 1:09:13So there's a couple little utilities, but like I said, it's one of those, one of those libraries
- 1:09:19that is simple, doesn't do a lot, but solves a particular problem.
- 1:09:23So if you're having that problem, go for it.
- 1:09:25Yeah.
- 1:09:25Fantastic.
- 1:09:25Your example is for a CSS.
- 1:09:27Can I like leverage this to like JSON API somehow like offline, some API calls or anything,
- 1:09:34or is it, is there not really a way to fit that together?
- 1:09:36I don't think it would work.
- 1:09:39If it's a straight URL that was always coming back with the same value and it was hard coded,
- 1:09:44it probably shouldn't care.
- 1:09:46But yeah, it would depend on how you were using it.
- 1:09:49Yeah, sure.
- 1:09:49Last one.
- 1:09:50The thing you always put last security, right?
- 1:09:52Just kidding.
- 1:09:53Okay.
- 1:09:54We're skipping ahead.
- 1:09:55Got it.
- 1:09:55No problem.
- 1:09:55Oh, are we skipping ahead?
- 1:09:56I had a Django extensions next.
- 1:09:58So.
- 1:09:58Oh yeah.
- 1:09:59I think I just didn't open that one.
- 1:10:00That's fine.
- 1:10:01I kind of touched on this one when I was talking about Django All.
- 1:10:04This is a collection of things that probably should just be built into Django is really what
- 1:10:10it comes down to.
- 1:10:11There's a whole bunch of different areas.
- 1:10:13Again, it's this huge hodgepodge.
- 1:10:15So there's extra tools for the admin, more management commands, more signals, template filters, extra model fields, job management.
- 1:10:24It's a very lightweight job management.
- 1:10:26But if you're not at the level where you want to use Celery or something like that, this might be good enough for you.
- 1:10:30Some model mix-ins, permission mix-ins, extra validators, some utility methods.
- 1:10:35The one I use here all the time, particularly when I was learning Django Ninja and I was trying to understand how the router worked, is a management command called show URLs.
- 1:10:44It'll show you all the URLs you've registered, what their keywords are, and how to call them.
- 1:10:50So it's a nice little introspective kind of thing to work through backwards.
- 1:10:54It's got another management command called run script.
- 1:10:58And this one's so useful that I had the exact same thing in Django All.
- 1:11:01Again, as I said, I should have just looked at this library first.
- 1:11:04And the idea of run script is it gives you a Django context for running a Python script.
- 1:11:10So this is kind of like the dash dot slash management shell command, where it gives you inside of a Django context, but it does it for the script that you're running.
- 1:11:18Oh, that's cool.
- 1:11:19So things like the database are initialized and stuff?
- 1:11:22Exactly.
- 1:11:22Yeah.
- 1:11:22Okay.
- 1:11:23I find I generally use it if I'm debugging, right?
- 1:11:26So if I've got three or four things I want to do in the database and I need to run it three or four times until I figured out what the problem is, rather than constantly doing that in the REPL over and over again,
- 1:11:34you stick it in a file, run the script, look at your debug or use it in the debugger.
- 1:11:39Of course, is the other thing there, because you can step through that a lot easier than you can say inside of a inside of a REPL.
- 1:11:45So a lot of good stuff here.
- 1:11:46And it's one of those libraries that I find every time I look at it.
- 1:11:49I'm like, oh, I have to remember that's there.
- 1:11:50And then I forget.
- 1:11:51But you always have to sort of go back and dig through it.
- 1:11:54So it's a good little toolbox.
- 1:11:57Diff looks cool for probably pre-post migration.
- 1:12:00There's three or four SQL commands in there that'll tell you how your migrations are, how they've changed, how you're managing them, what it's doing to them.
- 1:12:08Django has since added a couple things that are similar to it inside of the core library.
- 1:12:13But these are definitely more powerful.
- 1:12:15So, yeah, if you're trying to figure out what's going on with your models, there's some interesting stuff in there as well.
- 1:12:19Yeah, fantastic.
- 1:12:20All right.
- 1:12:21Maybe that brings us to Django All-Off.
- 1:12:23This is it.
- 1:12:24This is the last one.
- 1:12:25So this is Django All-Off.
- 1:12:28This is your go-to place for authentication.
- 1:12:30It has a series of workflows in it that are your typical user management things like sign up, email addresses, email verification, password resets.
- 1:12:40And so I don't usually use it for that.
- 1:12:43I use it for the thing that most people use it for, which is it's got this hook for your social media account authentication.
- 1:12:48So most social media sites use either OAuth or OAuth2.
- 1:12:53And so if you've ever been on one of those web pages that says, you know, you can sign in or create an account with your Facebook account or your Google account, this is a plugin that allows you to do that inside of your Django space.
- 1:13:03They have over a hundred different providers.
- 1:13:06I stopped counting.
- 1:13:07I just gave up after a while.
- 1:13:09Most of them are overlays on top of OAuth2, but there are also some that are custom as well.
- 1:13:16And what happens is when a user signs up using one of these, it still uses the standard Django user, but then it creates an associated object.
- 1:13:25And the advantage of this design is it means different users can use different things.
- 1:13:29I can sign in using Facebook.
- 1:13:30You can sign in using Git or Google or GitHub or Google.
- 1:13:34But we still end up in the same user style account inside of the database.
- 1:13:39But the other advantage of it is you can also associate multiple things.
- 1:13:42So say you were writing a site that wanted people to be able to log in through GitHub or GitLab, but also wanted to be able to tweet.
- 1:13:49Well, you could not necessarily allow logging in with Twitter, but you could allow them to authenticate through it so that you could tweet on their behalf.
- 1:13:56So it allows you to start connecting and play with all those things.
- 1:13:59The plugin, the workflow is plugin.
- 1:14:02Try that again.
- 1:14:03The workflow is pluggable as well.
- 1:14:05So there's hooks inside of the signup process.
- 1:14:08So let's say you've got some questions that you need to ask the user when they sign up, like extra information that Django doesn't normally have.
- 1:14:14It's got a flow for that that helps you with it.
- 1:14:17So really, really comprehensive library.
- 1:14:19The only thing I find that's challenging with this really has nothing to do with the library itself.
- 1:14:24It's just that third-party authentication tends to be messy.
- 1:14:27You are always dealing with tokens and provider sites and all the rest of it.
- 1:14:31And you're going to be...
- 1:14:33Have you registered your OAuth application at...
- 1:14:36Exactly.
- 1:14:36...AngelList?
- 1:14:37Did you do it right?
- 1:14:39Like, oh.
- 1:14:39You're always going to be in this space of, did I screw it up?
- 1:14:42Is it the token that's not right?
- 1:14:44Am I not hooking the library incorrectly?
- 1:14:46I always find it's a little finicky.
- 1:14:48Now, once you get it going, it's fine.
- 1:14:49But there, it does tend to be a little cumbersome getting there.
- 1:14:53Yeah, it's a little magical in, like, the traditional sense.
- 1:14:55Like, it can just work amazingly.
- 1:14:57But if it doesn't, you're like, I just don't know what the incantation I'm supposed to give it is.
- 1:15:01Because it's just not...
- 1:15:02It won't.
- 1:15:02It won't do the thing.
- 1:15:03You're debugging something that is somebody else's responsibility.
- 1:15:07So you're not getting a lot of information out of the host system, usually.
- 1:15:10Yeah, and often it's encrypted and signed and, like, stuff doesn't match.
- 1:15:15It's like, how are you supposed to know how it doesn't match?
- 1:15:17Anyway, but, I mean, that's the story of OAuth.
- 1:15:20OAuth 2, not this library's problem, right?
- 1:15:23The library makes it a lot easier.
- 1:15:25It's just, like you said, there's still a level that is painful.
- 1:15:29It swims in complex water.
- 1:15:31One thing that is kind of nice here is you can unroll your OAuth as well.
- 1:15:35You can say, I did originally sign up on GitHub, but I just want to make a password from my account now.
- 1:15:41I'll let you drop that out, which is, I think, kind of cool.
- 1:15:43I think it was a very smart architectural decision to continue to use the Django authentication mechanism underneath.
- 1:15:48It means, from a support standpoint, you don't have to worry about a lot of things.
- 1:15:53You can still see them inside of the admin.
- 1:15:55You're not having to worry those pieces.
- 1:15:56And like you said, if something goes wrong with somebody's Facebook account, you don't lose anything.
- 1:16:01You basically, you know, they can send you a support email and you can send them a password reset off of a primary account and rehook them up another way, which is a thing of beauty.
- 1:16:10Yeah, that's great.
- 1:16:10All right.
- 1:16:11Well, I would say we're a little over time, but it's been a super interesting list to talk through with.
- 1:16:15So maybe call it a wrap on however many topics we came up with.
- 1:16:19A whole bunch of cool ideas for doing more with Django and Python web apps in general.
- 1:16:24So thanks for that.
- 1:16:25Happy to do it.
- 1:16:26Yeah.
- 1:16:26Now, before we get out of here, I've got to answer the two questions.
- 1:16:29Well, I've answered one of them already.
- 1:16:31You're going to write a code.
- 1:16:33What editor do you use?
- 1:16:35Yeah.
- 1:16:35So I'm Vim.
- 1:16:36And for those who are taking the course, I used PyCharm there.
- 1:16:39So you don't have to understand antiquated.
- 1:16:40Speaking of magic, you don't have to follow along there.
- 1:16:43So I did use PyCharm there, but Vim is still my standard.
- 1:16:46And then notable PyPI package.
- 1:16:48It can be any one of these you want to give a shout out to.
- 1:16:50We've really kind of answered that question a bunch of times.
- 1:16:53But if you want to give a shout out to something else as well.
- 1:16:55I got something else for you, which is Ascii Maddox.
- 1:16:57So I do a lot of TUI work and I'm really keen on this library.
- 1:17:02They have a bunch of form widget stuff and also a whole bunch of cool, awesome 80s style
- 1:17:08animation bits and pieces.
- 1:17:09Core developer is a guy named Peter Britton.
- 1:17:12Very responsive.
- 1:17:13Always looking for MRs.
- 1:17:15So yay, Peter and Ascii Maddox.
- 1:17:17So if you're into this space, this is definitely something to check out.
- 1:17:20Wow.
- 1:17:21Amazing.
- 1:17:21Yeah, it looks pretty awesome.
- 1:17:22All right.
- 1:17:23Great example.
- 1:17:23All right.
- 1:17:24Final Call to Action, so many of the things that we've talked about here, if they're
- 1:17:28interesting, people want to learn how to actually use them.
- 1:17:30Grappelli, create your own admin command, a bunch of other stuff covered in your course.
- 1:17:35So check that out over at talkpython.fm.
- 1:17:38Just click on courses, find the Getting Started with Django course.
- 1:17:40Well done on that.
- 1:17:41Thank you so much for coming here and sharing all these.
- 1:17:45You know, maybe give a final call to action.
- 1:17:47People want to get started with these ideas and bring some of the material to their apps.
- 1:17:51What would you say?
- 1:17:52I think a lot of it depends.
- 1:17:53You know, anytime I'm trying to learn something new for me, it's always about the project,
- 1:17:56right?
- 1:17:57So if I, you know, if you're doing something that needs the REST API, then, you know, Ninja
- 1:18:01or DRF might be the place to go.
- 1:18:02If you're, you know, if you just want to play a little bit, then, or you're learning more
- 1:18:07about how Django works, the debug toolbar is a fantastic way of sort of learning some of
- 1:18:13the internals and taking your Django to the next level, because it's exposing a bunch of
- 1:18:16that stuff that you kind of think you know how it works.
- 1:18:19Yeah.
- 1:18:19It's a nice little place of doing a tutorial.
- 1:18:21So those might be good places to start.
- 1:18:23Yeah.
- 1:18:23That's cool.
- 1:18:24The framework tries to hide a lot of that stuff.
- 1:18:25The toolbar will like reveal it what's going on, right?
- 1:18:28Yeah.
- 1:18:29Well, as good framework should, but every once in a while, you want to see the guy behind
- 1:18:32the curtain.
- 1:18:33So that's right.
- 1:18:34All right, Chris.
- 1:18:35Well, thank you so much for being here.
- 1:18:36It's been a whole lot of fun.
- 1:18:37Enjoyed it.
- 1:18:38Yeah.
- 1:18:38Same.
- 1:18:39See ya.
- 1:18:39Cheers.
- 1:18:39Cheers.
- 1:18:39This has been another episode of Talk Python To Me.
- 1:18:44Thank you to our sponsors.
- 1:18:45Thank you to our sponsors.
- 1:18:45Be sure to check out what they're offering.
- 1:18:47It really helps support the show.
- 1:18:48You care about the ideas behind technology, not just the tech itself.
- 1:18:52And you know that tech has an enormous influence on society.
- 1:18:55So check out the IRL podcast.
- 1:18:57It's hosted by Bridget Todd.
- 1:18:59And this season of IRL looks at AI in real life.
- 1:19:02Listen to an episode at talkpython.fm/IRL.
- 1:19:05Starting a business is hard.
- 1:19:07Microsoft for startups, Founders Hub provides all founders at any stage with free resources
- 1:19:13and connections to solve startup challenges.
- 1:19:16Apply for free today at talkpython.fm/founders hub.
- 1:19:21Want to level up your Python?
- 1:19:22We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:19:27Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:19:32And best of all, there's not a subscription in sight.
- 1:19:34Check it out for yourself at training.talkpython.fm.
- 1:19:37Be sure to subscribe to the show.
- 1:19:39Open your favorite podcast app and search for Python.
- 1:19:42We should be right at the top.
- 1:19:43You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:19:48and the direct RSS feed at /rss on talkpython.fm.
- 1:19:53We're live streaming most of our recordings these days.
- 1:19:56If you want to be part of the show and have your comments featured on the air,
- 1:19:59be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:20:04This is your host, Michael Kennedy.
- 1:20:06Thanks so much for listening.
- 1:20:07I really appreciate it.
- 1:20:08Now get out there and write some Python code.
- 1:20:10I'll see you next time.