# Manage.py as the task runner

Source: https://djangotv.com/videos/django-day-copenhagen/2024/justus-hamalainen-managepy-as-the-task-runner-django-day-copenhagen-2024/
Date: 2024-10-13
Speakers: Speaker 1, Justus Hämäläinen, Speaker 3

## Summary

Justus Hämäläinen argues that Django’s `manage.py` can serve as a cross-platform task runner for project setup, testing, deployment, and other development workflows, avoiding fragile shell scripts and lengthy setup instructions. Custom management commands are straightforward to write in Python, can accept command-line options, call external programs, use Django settings, and extend existing commands; he demonstrates running Tailwind alongside `runserver` and describes uses such as preparing development data, sending Slack messages, and renewing certificates. He considers this a good fit when a team is committed to Django, but less compelling when it is not.

## Transcript

### [00:00] Introduction and Task-Runner Problems

**Speaker 1:** [00:05] Hello and welcome back live on stage. We have Eustus. And you're gonna tell us something about Task Runners. And I think we should start by giving you a big round of applause. The stage is yours

**Justus Hämäläinen:** [00:26] Hey, thank you very much. And hello for you all. So did you like the Yes. Great. Nice thing to be in front of you all. And as I said, I'm talking about running tasks in a development projekt. But first as we start, I'm Justus Hammerleinen and I work as a It's hard to say because sort of like one problem with my sort of way that I do my work is

[01:12] I don't like to choose only a one job. I like to work in various kind of situations, in different kinds of projektin. With sort of like business side, with the tech side, sort of talking about what we should do next. Moment talking about should we run things with pass scripts or like how we should manage sort of like details. Currently I'm working on a small team and we work with a few projektia. One is Vegemi. Vegemi is a food education software that pitää familia jail eat more fruit. veggies. It tries to make it more fun. It tries to make people to understand better what are the benefits of eating

[02:01] So the fruits and veggies. Then the second thing is I'm part of is Franklin that does electrical construction and home like building automation. You can already see the synergies between those two projects. And then the third thing that my team is working on is we work with a Pooh real estate agency that sort of helps people to find homes. And one thing that combines all of those projects is that they use Django and that's why sort of like I'm here. talking about something. And I'm talking about managed. py. And basically what it does, it already sort of like

[02:47] does a whole maintenance tasks that you have in your dango projects and we already saw a lot of sort of commands that you use when you are interacting with bodelos and schemas and creating sort of migrations for your database. And for a brief background, sort of like how I ended up being interested in Manage Spikes Mikä andttää why it's suin holista -projekte. Mutta alkaa sortta, how kodebas easilä yhdessä ja easilä. use and how because one of those things that I have noticed in many projects that you have a readme

[03:35] that is pretty long that sort of like has all of the steps that you need to be able to run the process that you are. How many you have a sort of readme in your project that you are working on now that sort of tells how to set up the system. And how many README 's are up to date? You are like how many of you are 100% certain that if I would take that README, I get the project running? Yeah, yeah. And this was one of those things that sort of like I noticed that they are not up to date. And then the other problem that I don't like to read. Like I'm I'm I'm sort of hired to write code, not to read paragraphs. So sort of like,

[04:20] how could you make things easier? And I came up with sort of like test free commands that if you have it makes a lot easier to sort of start interacting with a new project. Först är start skript. Basic om du har ett skript som vill sätta upp eller at tell att du har installat sam dependens som är näkött för projektet vill fa och tell that to. So you have one sort of like paste and it documents what you need to do to be able to run your project. Then test script and it basically runs all of those things that can fail. So you there's one command that you can run after your changes and no does the sort of continuous integration fail. And then the final one is deploy.

### [04:29] Why Use manage.py

**Justus Hämäläinen:** [05:07] And the reason is that there's often things that sort of like are hard or not totally working or needs changes. Related to deployment process and really often nobody sort of like wants to work on that part of the application stack and I have maybe sort of like I have a this kind of like traumas of being that person that ends up working on the deployment process and with all of the easy to use cloud tools and CI tools. and sort of like being a little bit off of afraid of them. Well, that's a sort of like thing that let's say ten years I've sort of like wanted to get these scripts like working But the real problem is that when you are doing a sort of script that you want to work straight

[05:56] out of the sort of box, you'll end up using the lowest common denominator. And basically it means the pawn cell. And it I don't mean pash, I mean the sort of like pawn shell. And for example I personally use a corn cell in my development. So you end up in a situation that you don't have a language that is really nice jay to use Then you end up in a situation that you have some tools that you can use, but then for example if people are using Linux, there might be a curl or VJ installed by default. And then well you need a git. And did you all know that sort of like even if you have a program that is named the same but

[06:42] the GNU Linux people didn't want to use the real Unix flags? So you have this kind of like a Big problem with sort of like getting everything work. And then you end up installing kustom software and you basically are sort of writing a README to sort of get the start script running that sort of was point. to make the README less important. And even if you get this working, then there's somebody that comes to your project and starts using Windows and then sort of like everything breaks. Yeah. So it's sort of like But the thing is, I would like to have a widely supported

[07:29] Ken and R purpose language that would have a large user base and it would be already available that you wouldn't have to install anything And this is me understanding that we are working in a dungo project, so we have a Python already installed. And well this is I'll show you a picture of our next development meeting where I sort of told my idea what we want to do. So it's me tied other developers että hei, i it's a really good idea that we'll start using Managed. py for everything. So rather than calling Managed Py from other

[08:15] programs, we'll call other programs for. manage. py. And why to choose manage. py? Why not to use some other task runner or some other sort of thing? The main thing is that when we are working in a dungo project, we already are using it. We'll have to run migrations or like Micrate. Sorry, run server or test. All of those are the things that we are already running. So why wouldn't we sort of like go all the way there? And next let's check like how the managed pie works. So Do you hey how many of you know how many ways you

### [09:01] Django Management Command Discovery

**Justus Hämäläinen:** [09:01] you can call dango management commands? There's somebody sort of like has three fingers up. That's good. So we have dango admin. We have the managed. py and we have the python module django. And the thing is, all of them are the same thing, essentially. So sort of like all of them work the same way. You call it, you give them a command name and then it sort of finds that command jaunks it. But how the commands like where they are, how to sort of like we tells that what to run when you call. Mikrata oli runserver. Väjälen kommand on stillittanut the samma vai. Ja this on sort of great way

[09:47] with Dangho. There is no sort of special kaka within the dangho environment. You have basically app name, then you have management and command directories and within those directories you have a command name. py Which sort of like means that for start project you have a core management commands start. py for runserver core management commands runserver. For collect static contribute static files management commands collect static. py you'll start to see a pattern here So that's the way that Jangen

[10:33] knows the applications. But well, then there's a question that how it knows where to look thätään The core is easy because they are always available. But how all of the other stuff is found? And how many sort of like though the great thing for me with Dango is that basically everything plus comes back to the installed apps. Like almost everything, sort of like the great thing with Django is that it works the same way whatever you are doing. And this is a good example about So same way kuin static filet. Based on the order

### [11:18] Creating Custom Commands

**Justus Hämäläinen:** [11:18] you have in your installed apps, it suits which static files based on the path it will load. You can then override existing static files, you can override existing templates, you can override existing commands, you can create new commands. And let's check now like what is the minimum like thing that you need to do to be able to have a new command in Dango So to create a new command, we'll first need to create an app. And let's call it hello C. It would be hello command, but I don't have enough horizontal space with the font size I'm using, so we'll have to use short names for the

[12:06] Then in the settings files we'll add the hello command app and this is something that if you have a problem basically always for me it's the thing that take the time to put things in the installed apps. And if we check the hello C -direktori, we have management, commands jamand. py Then of course normal app files like apps py models. py. Then within the hello command. py We'll import from the Dung Core Management base command. Then we create a class that is named command. It's important it's named command. Command. Dango finds the right thing to

[12:56] run. Help, it's used to sort of like tell the user what the command should do, then we'll just override handle method. And that's all. Like those basically like four lines of code, and then we can do anything that we want within a Python. So it's pretty easy from like from my point of view. of view. And then w after you have done of all of this you can run the command Python managed by hello command and it will answer. Simple. Easy. And it's not all. The best thing is that sort of with as with Dungo, somebody's already sort of worked on this, had a real

[13:42] like problems and needed to extend things. So it's really too easy to add for example flags or sort of options to your command. So you'll just override add arguments method where you get self and parser. The parser is instance of arc parser that is a sort of like Python module for parsing command-line arguments and with that you can just add for example parser add argument a name option and then action store tells that will want to keep the value that you are providing and help just this thing that you want to show the user

[14:28] When they ask for the help. And now what you get, you still have handle and in the keywords you will get the variable or the option that you have said in the name that you said on the other argument. And then you can do what you want with those argument. And again when you run the command and you pass that argument it will sort of like work. So it's really easy to add those things to those to start creating command line tools with managed. py and then the extending part so of like you can create new commands that is great but you can also

### [15:14] Extending Existing Commands

**Justus Hämäläinen:** [15:14] change break and sort of totally modify what the sort of existing command Like there was an idea here that you can add a sort of validator that your pizzas don't have pineapple. But sort of you could create a make migrations tool that sort of just removes that information there. It doesn't sort of like take doesn't allow pineapples to be removed. Yeah. But let's see how to integrate a tailwind to the run server process. So again we'll create a new app, detail, and again, short name because we have only so much horizontal space.

[16:01] We add the detail to the settings app and then we'll create the command Nyt näytän detail server. Ja koska meillä on detailillä ensin, niin dango näyttää runserverän kommand, itse kats ourän, not korva. When we are extending we need to do things a little bit differently. So we'll import from Dango Core Management Commands run server. So the command that we want to extent and we need to import because dango works in a way that it loads the file and it runs the class named command And if we want to have a command in our file, we need to

[16:47] have our class named command. So we need to rename it. because then you know what the command is that you are extending. You create class command and here you do override run argument Do people notice what we are doing differently compared on the last example? Does anybody want to yell? Yeah. And we are overriding run, not handle. But this is sort of like I didn't point that out to you so much on the

[17:32] Nyt on, että why overridin run here, not handle? If we vote on the core server. py file jackän, kuinka Its handelkomi says restarting app, kunsaut filet. Ja sitten saa self-run, joka sähta. Starting servoilla itsellä ja sitten cleanupia. So because the sort of command class is just a normal Python object or normal Python class, you sort of like can Look at it and you can extend different parts of it so that you can sort of like piggyback on the things that it's already doing. So

[18:18] Basically, we'll extend the run if we want to extend the run server command. And again, we can do anything we want before we call the super run. So we can sort of like well, we have unlimited possibilities We can sort of like you can you can break things really horrible horribly or sort of you can do nice sort of like really hard to catch bugs if you sort of like likes try to do two complex things. But now we have a power of doing anything. So let's continue of integrating Tailwind. Again we have a the directory, we have a management command

### [19:04] Running Tailwind with runserver

**Justus Hämäläinen:** [19:04] Run servers. Tässä source that is our input. CSS. If we want to add Tailwind-related direktivoissä. Tässä static detail. This is our output. We output the Tailwind CSS, normal files, then of course package JSON, Tailwindissä NPM, sort of package. So you need to install the dependencies and then you need to add Tailwind config. And with Tailwind Config one of those things that we Because the directory that we are running, it's the detailed directory. We go one directory back and then just search all of the templates statik files

[19:50] and we generate the CSS that we wanna. for all of our things. And now we'll do the so this is the class command class and what we need to do to be able to run this kind of like a background running service. So we'll need to care get a process ID so that it 's not c garbage collected before our code exits. So in the run we first want to have the command directory. That means the command directory is the detail command management commands directory. And from that we'll want to go to the app direct directory itself so that we'll run things in right places.

[20:35] And then whatever sort of process we want to call we'll use subprocess So we open a pipe. And here we can example call the tailwind. To sort of watch and build our sort of CSS this at the same time we are running the run server. And here is the sort of we want to set the current working directory to the app directory so that it finds all of the files shell so it sort of like works and everything Exists what many sort of like terminal applications accept expect.

[21:20] Standard in would be Like prosesspipe. Then there's self-standard out jaf-standard error. So we'll just direct everything that happens to our application. will know what is happening there. And then we'll call super command self-run. And this way we can call here an esimerkiksi Tailwind, but we could call any application we want. So this is sort of like now we are using the Python managed by a shell. or any application or any program that we can think of. And I'll show an example of calling async

### [22:06] Async Tasks and Python’s Flexibility

**Justus Hämäläinen:** [22:06] libraries because after that you have the information of doing basically anything in managed by side. So if we would have async example command, we would have some async sort of like API calls or something that we want to do. We'll wait all of those calls to be ready. Then we have the normal command and then we'll just in the handle use the asynchron to run those things. So we'll move to Async side if we need to and back to the sort of like sync side after it's done. And it's okay because all of the management commands are something that we run that start and end. They are sort of like synchronous by nature. And the great thing is

[22:51] when you start doing that is that it's just Python. So you sort of like anything that you can think of you can now do as part of your sort of like process. You can use any package. So if you want, you can take pandas, you can start creating sort of like processes that take your data, generate some kind of like information about it. You can call any program, you can call kit, you can call anything that you want and manage those things through the Python. hand side. It handles a lot of those things that are sort of like cross operating system, cross-platform things. And makes everything really easy in that way. You have access to the settings pie, so you don't

[23:38] need to manage environment variables. Y voidaan käyttää joita settingskilaan ja lisää tämä filma. Ja nyt on kommit Git ja miten python, not se, että se on samen skrivat. Ja näyttää nähdä, että se on environment. that needs to be set. And now sort of like after giving all of this power to you, like what you should do with it And it's I have a few ideas. So for example, NPN install. Tailwind is one of those tools that I like to use, but one of those things that it's a sort of node program so you sort of like have to handle different things

### [24:25] Development Workflow Integrations

**Justus Hämäläinen:** [24:25] but you can create a script that sort of handles all of that setting of setting up of the like installing all the dependencies, running it, being part of the run server command, being part of the collect static command, all of that you can do. yourself you could have a more complex sort of like build process for your front end and it also would be something that when you start run server it would automatically run. And it helps again people that are not so maybe sort of like in the front end side that you can set up things without having to move between two different world. You could have dev fixtures so that you have a command that would go take an SSH connection your to your dev servers

[25:10] would pull the media files from there, would pull the data that you have you have running there. And now you have the sort of like environment set up with data that is sort of like sane or at least more sane that you can sort of start learning about the application managing secret so you could use any but one example one password software development git you can use anything that you sort of like need, but you can start integrating more stuff within your application, not so that it's sort of like integrated, for example, in the CI side. You don't need to use GitHub Action, you could use Python Slack as DKI. you can

[25:55] generate a sort of send message command that sends a slack message if you using chat ops and now you have something that is in The Python side it's easier to debug because you can run it on your own machine. You don't need to sort of like go back and forth with the GitHub actions, sending a comment that I changed something that will it work. As it's dango, you have batteries included and it means that sort of you can get easily colored text. There are roles that you can sort of like use to set different colors and then there's teams so sort of like you don't have to say että always green with black background it handles okay and then the no-kolor situation

[26:41] Because there are situations where you don't have colors. Automatic bash komplet and of course call command. So that when you do something as a Command you can easily use it also in the dango code. So if you have some processing that you want to trigger within the sort of managing request you can easily sort of do that and I'll tell you one case where we use it it's part of how we handle certificates so we'll have a we have a GitHub action that is basically just a time trigger and it calls managed. py

[27:28] Manage certificates. It pulls certificates from one password. If they are still valid, it doesn't do anything. But if there's less than one week validating them, it generates new certificates with DNS challenge and saves them to one password. And now our deploy process can read them from the one password and set up on the servers working okay. And this was important for us because we On the Franklin sanoissa deploymin asiassa lokal netvoikissä, johä on acessa ja kannattaa HTTP challenge. Mutta we still want to know acessa our sort of like internal services through an HTTPS

[28:14] without managing the whole sort of like self-signed certificate process But that's that's all. Thank you

### [28:36] Questions

**Speaker 1:** [28:36] Thank you, Eustace. Do we have a question or two or three we can certainly manage a question yes question there in the back

**Speaker 3:** [28:53] Hello, thank you for your interesting talk. You've certainly made some very compelling arguments, I think, for using managed PY. However, do you think there are any cases where it's not advisable to use it?

**Justus Hämäläinen:** [29:14] I do I have like I can come up with sort of like arguments against it and I have heard a lot of arguments against it against it. But I'll have to say that right now, and this might be a sort of like that changes after some time. But right now I feel that that's one of the best if like again we are working so that we'll commit fully on dango so we'll you use sort of not the best parts but the whole dungo thing so I think for our cases it's been a really That will do everything within that. Koskuita, secret management and environment variable management, that easier to understand and sort of like map. how things are used. So

[29:59] not really sort of like if you are not sort of committing totally to dango then it sort of like might not be that Good idea. But those cases where you are you are going to commit, it's really good idea. At least now, I feel like right now is so I'll after two years I'll come to here with a talk that you don't want to do everything in Money Spy

**Speaker 1:** [30:27] Great. More questions? I'd like to ask maybe about what kind of effect it has had on your README 's. Compared to some before situation.

**Justus Hämäläinen:** [30:45] Well, it hasn't really affected readmes of projects that I I'm running. But it has changed so changed things so that people that are not me can also sort of contribute. So they are now sort of like at least codes that documents what needs to be done to get things running. So it's

**Speaker 1:** [31:09] all right. So we are going to switch directly to the to the next talk. So hang around. But let's give Eustus a big round of applause. Thank you so much for the amazing talk. Thank you
