I've been banging my head against the wall to figure this one out. I am writing an app which, ideally, when an onclick happens in HTML runs a python script from FLASK. The problem is nothing I do seems to work. I don't get errors anymore but the program doesn't run.
I've tried accessing the python script through AJAX, I tried calling the python script through jscript in the HTML, I created a separate file for the python script instead of running it through Flask....
</style>
<h1>Book Roulette</h1>
<h2>Discover Something New</h2>
<h3>Click the image to download a surprise book</h3>
<a href="/static/gut.py"> <button class="button" style="background: url(/static/images/stack.jpg)" onclick="button();" action="/button"></a>
</button>
<script>
$(function() {
$('button').click(function() {
$.ajax({
url: 'static/gut.py',
data: $('form').serialize(),
type: 'POST',
success: function(response) {
console.log(response);
},
error: function(error) {
console.log(error);
}
});
});
});
</script>
</body>
</html>
Here is my Flask/Python Document
from flask import Flask, render_template
from gutenberg.acquire import load_etext
from gutenberg.cleanup import strip_headers
import random
app = Flask(__name__, static_url_path='/static')
@app.route('/books')
def books():
return render_template("main.html")
@app.route('/button', methods=["GET", "POST"])
def button():
f = open("GutProject.txt", "w")
for x in range(1):
y = (random.randint(0, 59000))
text = strip_headers(load_etext(y)).strip()
f.write(text)
if __name__ == "__main__":
app.run(debug=True)
What I want to do is have the python code run when I click my HTML button generating a new file on the user's computer. So far I've reached the point where when I click on the button nothing happens.