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()