I have a flask app that takes some text as an input, runs a python script and spits out an output on the same html page, except it goes to a new page. I don't see why it would go to a new page.
This is my app.py file:
#!/usr/bin/env python3
from flask import *
from flask import render_template
from myclass import myfunction
app = Flask(__name__)
@app.route('/')
def homepage():
return render_template('index.html')
@app.route('/', methods= ["POST"])
def background_process():
if request.method == 'POST':
try:
story = request.form.get('story')
if story:
result = myfunction(story)
return render_template('index.html', **jsonify(result))
else:
return jsonify(result='Input needed')
except Exception as e:
return (str(e))
if __name__ == "__main__":
app.debug=True
app.run()
And this is my index.html file:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="../static/main.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type=text/javascript>
$(function() {
$('a#process_input').bind('click', function() {
$.getJSON('/background_process', {
story: $('textarea[name="story"]').val(),
}, function(data) {
$('#result').text(data.result);
});
return false;
});
});
</script>
</head>
<body>
<div class='container'>
<form>
<textarea id="text_input" rows="20" cols="80" name=story></textarea>
<br>
<a href=# id=process_input><button class='btn btn-default'>Submit</button></a>
</form>
<br>
<p><h2 align='center'>Result:</h2><h2 id=result align='center'></h2></p>
</div>
</body>
</html>
When input is given, it shows the result in a new page on json format. I want to show it on the same page. What's going wrong here? Thanks!