Run Python File first, then 'runserver'

Hi there, I am an absolute beginner with Django and need help. When I start the App I want to run a python file first which imports some csv files, makes some calculations etc which then lead to the final pandas dataframes. Those are the foundation for the data that will be displayed in the App.

How can I achieve that this file is run exactly once (at the beginning) and after that the individual sites in the App can access the dataframes.

Some additional information: I use plotly dash for the dashboards inside the Django App which incorporate the dataframes and display various figures.

I hope this description is understandable for you. If not I am very happy to answer any questions for clarification.

Any help is really appreciated. Thank you in advance.

Hm… My first thougt on this would be: Django runs on a database. So whatever you do with your pandas needs to go into the database. Next: there is no “starting the app” in that sense. When your django project is running, then all the apps are running.
What you could do is have a starting view for your app. So when the user opens the view, in the background the pandas import is called. However that would mean that anytime a user visits the view, that process is triggered.

Maybe this is of help to you:

1 Like

One possible way to run something before the web application starts is to put some code before the application instance is created in

Unfortunately, I’m not sure this is a great idea. Because of the way many web application servers work (by creating separate worker processes that handle individual requests), you might have a hard time running your setup code “exactly once.”

You might also be able to get away with using AppConfig.ready ( and some caching.

I’ve not personally tried to do what you’re attempting though so these are possible ideas only. I can’t guarantee that they will work the way you want.

1 Like

Adding to both of the previous answers, keep in mind that in a production environment, the Django app is always running. You’re not starting it up in the morning and shutting it down at night, or restarting it any time you want to modify data being displayed.

So having said that, depending upon how you want to feed your data into the system, you’ve got a couple of choices.

  1. You can build a form in Django, allowing the CSV files to be uploaded. The upload of the CSV then runs the process to perform whatever prep-work is required.
  2. You can create a custom management command that you can run on the server manually when necessary, perhaps supplying the CSV file as a parameter to the command.
1 Like

Thank you all for the fast and detailed comments. One thing that is probably important to know is that presently I am using just the Plotly Dash Apps for myself as analytical applications. I am trying to use Django to create multi-page applications to handle the increased complexity. So this runs only on my machine.

Yep, that does put a different perspective on things.

I’d probably go with the custom management command then. Have it do whatever pre-processing needs to be done, and then as its last step, run the runserver command.

1 Like