'Lunching python script from Django View
I wrote an essential python script (data import) which executes a series of commands and, once it is executed, write in the console a message.
I would like to implement this script in Django in order to run the script directly in a "frame console" without needing to prepare several JS/AJAX functions to write in the page the operation has been executed.
Is this possible in a "simple" way without using sockets or any other tricky method?
Thank you
EDIT: as requested I'll try to explain what my script does. When lunched, it performs a mysql database dump: first it checks whether the connection to the database exists (1), than performes a series of for and iterations the dump of the database (as I can't get access to mysqldump directly).
So, once lunched, it appears such as:
| - - - - - - - - -
| - Try connect to X database...
| - Database connected
| - - - - - - - - -
| - Dumping tables...
| - - x_1 table completed
| - - x_2 table completed
| - - x_3 table completed
| - - - - - - - - -
| - Database backup completed
| - - - - - - - - -
I didn't specified the View but I meant a simple Template View with some HTML and a DIV.
Solution 1:[1]
You don't give enough detail to know why it isn't straightforward as a Django view, or what kind of view. But let's say, it processes a csv file. You might inplement a FormView with a FileField amongst other data. form_valid() would triger upoload of the csv file and then processing thereof. Results could be accumulated in a Python string (maybe use Python StringIO to replace writing to the console) and then display the results in a template which gets rendered as the view results.
This is only appropriate if running the script completes reasonably fast. If it takes many CPU-seconds you will want to run it asynchronously. Tools to do this exist. Celery is perhaps the best-known.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nigel222 |
