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()
The onclick="open('run_graph')"
will call a JavaScript function passing run_graph
string 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")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.