how to send django website user input data to external python script

Viewed 252

I am trying to create a webpage where users input two data and then I receive that data and pass that to an external python script I cannot enter the python script the Django since its very big


<form action='generate' method='post' >
  {% csrf_token %}
  enter the current status:<input type="text" name="status"></br>
  enter the water_level:<input type="text" name="level"></br>
  
  <input type="submit" value="submit">

</form>
'''
I am receiving the value entered by the user into views. generate function as follows
'''
def generate(request):
    a=int(request.POST["status"])
    b=int(request.POST["level"])
  out=run(sys.executable,['C:\Users\siddhant\Desktop\internship\indicator\work\templates\water.py',"a","b"],shell=False,stdout=PIPE)
print(out)

I want to run the water.py file here only bypassing the input a and b

from os import read
from random import randint
import sys
from tkinter.constants import X


from datetime import*
import pandas as pd
import numpy as np
from pandas.core.base import DataError

from time import time

start_time = datetime.now()
# do your work here




x=sys.argv[1]
current_level=sys.argv[2]

I want to pass the received input a and b to x and current_level respectively

1 Answers

Have a look at the subprocess module. You can use the values from your view in the subproces and read the output.

for example:

subprocess.run(["ls", "-l"])

in your case it should look something like:

subprocess.run(["python", "/path/to/water.py", status, level])
Related