0

I want to run my python file when a html button is pressed. I have my python file working when i press run in PyCharm (it just creates a graph). My python file is stored as main.py. I am having trouble linking the html button to run the main.py file when the button is pressed.

At the moment I think i have the button pointing to a function in my views.py which in turn runs my main.py file. (or so i think. it is not working)

INDEX.HTML

<input type="button" value="Run graph" onclick="open('run_graph')">

URLS.PY

from django.urls import path
from . import views

urlpatterns = [
path('', views.index, name="index"),
path('run_graph', views.run_graph),
]

VIEWS.PY

def run_graph(main):

return main

MAIN.PY

import matplotlib.pyplot as plt
import numpy as np
col_count = 3
bar_width = .1

korea_scores = (554, 536, 538)
canada_scores = (518, 523, 525)
china_scores = (613, 570, 580)
france_scores = (495, 505, 499)

index = np.arange(col_count)

k1 = plt.bar(index, korea_scores, bar_width, alpha=.4, label="Korea")
c1 = plt.bar(index + bar_width, canada_scores, bar_width, alpha=.4, label="Canada")
ch1 = plt.bar(index + 0.2, china_scores, bar_width, alpha=.4, label="China")
f1 = plt.bar(index + 0.3, france_scores, bar_width, alpha=.4, label="france")

plt.ylabel("mean score in PISA 2012")
plt.xlabel("Subjects")
plt.title("France")

plt.xticks(index + .3 / 2, ('Maths', "Reading", "Science"))
plt.legend()
plt.grid(True)
plt.show()
kitchen800
  • 197
  • 1
  • 12
  • 36
  • The `onclick="run_graph()"` will call a JavaScript function not Python's function you implemented. Also, you need to implement a call from your JS function that triggers an URL endpoint that will execute your python function. – Chiheb Nexus Jun 20 '20 at 22:50
  • Does this answer your question? [How can I execute a python script from an html button?](https://stackoverflow.com/questions/48552343/how-can-i-execute-a-python-script-from-an-html-button) – Jacques Jun 20 '20 at 22:51
  • @ChihebNexus A yes sorry that should be `` i wasn't meant to use any js. even pointingto my views.py it doesn't work – kitchen800 Jun 20 '20 at 23:08

1 Answers1

0

The onclick="open('run_graph')" will call a JavaScript function passing run_graphstring as a variable to the function not Python function.There are many ways to implement this , the method which i am telling is one of those.

This is your Html Tag that u need to change

<input type="button" id='script' name="scriptbutton" value=" Run Script " onclick="gotoPython()">


<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

<script>
        function gotoPython(){
            $.ajax({
              url: "/python_file",
             context: document.body
            }).done(function() {
             alert('finished python script');;
            });
        }
    </script>

And in your URLS.py

urlpatterns = [
path('', views.index, name="index"),
path('python_file', views.python_file),
]

And in your Views function copy the code in it and return a Http response

import matplotlib.pyplot as plt
import numpy as np
def python_file(request):
   col_count = 3
   bar_width = .1
   korea_scores = (554, 536, 538)
   canada_scores = (518, 523, 525)
   china_scores = (613, 570, 580)
   france_scores = (495, 505, 499)
   index = np.arange(col_count)
   k1 = plt.bar(index, korea_scores, bar_width, alpha=.4, 
   label="Korea")
   c1 = plt.bar(index + bar_width, canada_scores, bar_width, alpha=.4, 
   label="Canada")
   ch1 = plt.bar(index + 0.2, china_scores, bar_width, alpha=.4, 
   label="China")
   f1 = plt.bar(index + 0.3, france_scores, bar_width, alpha=.4, 
   label="france")
   plt.ylabel("mean score in PISA 2012")
   plt.xlabel("Subjects")
   plt.title("France")
   plt.xticks(index + .3 / 2, ('Maths', "Reading", "Science"))
   plt.legend()
   plt.grid(True)
   plt.show()
   return Httpresponse("Script Runned")

Uday Kiran
  • 229
  • 2
  • 13