Reputation: 29
For a school project I would like to be able to call some functions in a file "app.py" from "index.html".
In fact, the py file generates some things and then I have to insert the generated things into "index.html". I know how to insert the response from the py script but I don't know how to call it.
Here is app.py (that's only to illustrate)
def main():
return "insert this into the html page!"
<script>
function insert()
{
?
}
</script>
<input type="button" value="click to insert" onclick="insert()">
I tried with Flask but when I call the script, well, it works, but then it switches to another page...
So, I would like to do what i'm trying to do WITHOUT Node.js or React or things (don't ask me why I'm too lazy to explain)
And yes, I need to use a py script
Thanks for your help
Sorry for my bad english if you i'm clear just tell me i'll try to explain you
Upvotes: 0
Views: 2686
Reputation: 21
When you render your html template you need to pass you function uncalled in the render_template function then call it on the html page like:
def main():
return "Do your thing"
return render_template('myfile.html',main=main)
In the html file
{{main()}}
Upvotes: 2
Reputation: 577
well you should ideally use node or ajax but if you really want to come back to the same page you could simply render the same page again.
return render_template('same_page.html', your_data_variable=your_data)
and on your html page
Upvotes: 1
Reputation: 42
You should use a framework like Django or Flask, you can't use python like a js script. Python in web development is used most in the backend.
I suggest you to learn django: django.com
Upvotes: 0