0

My question about implementing a script into a view in a django web app.

I have a .py script that I want to run inside this django web app using the user-supplied data, the configfile, the modelfile and the choice as command line arguments. The output is then a list with different scores which will then be displayed inside the html (not the part I need help with yet).

So I have 2 forms, one that uploads a ML model and another one (the one I need help with) that uses user-supplied data to run a uploaded model:

 class Meta: model = MlModel fields = [ 'modelname', 'configfile', 'modelfile', 'choice', ] class TestForm(forms.Form): testdata = forms.FileField() model = forms.ModelChoiceField(queryset=MlModel.objects.all()) 

My views are as follow, where the test view is where the testing happens:

 if request.method == 'POST': form = UploadForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponseRedirect('/mltesting/') else: form = UploadForm() context = {'form':form} return render(request,'mltesting/upload.html', context=context) def test_model(request): submitbutton = request.POST.get("submit") testdata = '' modfile = '' confile = '' choice = '' form = TestForm(request.POST, request.FILES) if form.is_valid(): testdata = request.FILES['testdata'] model = form.cleaned_data.get('model') model = MlModel.objects.get(modelname=model) modfile = model.modelfile.path confile = model.configfile.path choice = model.choice else: form = TestForm() context = {'form': form, 'testdata': testdata, 'submitbutton': submitbutton, 'modelfile':modfile, 'configfile': confile,'choice': choice} return render(request,'mltesting/test.html', context=context) 

And this is the HTML template where the output of the testing is supposed to be:

 {% block content %} <p>Compound Protein Interaction Tool</p> <form action="" method="POST" enctype="multipart/form-data"> {% csrf_token %} {{ form.as_table }} <input type="Submit" name="submit" value="Submit"/> </form> {% if submitbutton == "Submit" %} {% endif %} {% endblock %} 

I am fairly new to Django in general so I apologise for the (probable) bad formulation of my question, but all help is greatly appreciated.

1 Answer 1

0

You can follow the answer from this solution

Make a folder in the same directory, add the py file there and then import the module you want in the view.py.

call the function as you would call any custom module.

** EDIT ** Call the script as stated in this answer

def test_model(request): if form.is_valid(): testdata = request.FILES['testdata'] model = form.cleaned_data.get('model') model = MlModel.objects.get(modelname=model) modfile = model.modelfile.path confile = model.configfile.path choice = model.choice else: form = TestForm() ######################################### ###########Here you run your script ###########Any variable from the script will be loaded here exec(open("./path/toYour/script.py").read(), globals()) #Add the information you want from the script to the context context = {'someVariableFromTheScript':someVariableFromTheScript, 'form': form, 'testdata': testdata, 'submitbutton': submitbutton, 'modelfile':modfile, 'configfile': confile,'choice': choice} return render(request,'mltesting/test.html', context=context) 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the help, however the script I want to run is a script that takes command line argument which I would run as follows: python3 script.py --modelfile ... --configfile ... --choice ... So it's not a module I want to import.
Then check this answer, I think this is what you want. stackoverflow.com/questions/1027714/…
and how do you implement this feature in the django context? Where is the script that I want to run saved? Do I save this in the static folder (where my css files are) or is this somewhere else?
Follow this answer: stackoverflow.com/a/31566843/8900853 If you run your "script.py", python will load the globals (the variables used in the your script) to the current function beeing run, like the "test_model" you have there. There you can access it and added to the context and return it to the HTML and render it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.