0

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

5
  • 1
    A bit outside the scope of the question, but statements like "don't ask me why I'm too lazy to explain" don't display an attitude too welcoming to potential answerers of this question. If it's significantly less effort or more efficient to use a tool like Node or React, why not entertain answers using them? Commented Apr 6, 2020 at 21:32
  • i don't want to use them because this question is for a friend, and he is a beginner, and it's only for a school project, then i think he don't want to start learning other things that he needs Commented Apr 6, 2020 at 22:43
  • it seems like your friend should use web framework (I think flask is good for a school project, or if he has more time django is good too) also, you can actually run Python integrated with javascript with the use of github.com/iodide-project/pyodide Commented Apr 7, 2020 at 7:02
  • never done that by myself though Commented Apr 7, 2020 at 7:02
  • yeah i'm using flask :) Commented Apr 7, 2020 at 9:31

3 Answers 3

2

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()}}
Sign up to request clarification or add additional context in comments.

Comments

1

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

{{your_data_variable}}

4 Comments

it works, but the returned data is some html how can i insert the data in a div on the page?
can you paste it in the comments? not sure why you would be getting back html, and in case you are it should fit into the div without any issues
the response is like render_template('index.html', response = '<p class='class-example'>Text here</p>"). Then, how can i turn this text response to an HTML object ? I tried a kinda weird way with jQuery (paste.myst.rs/n38)
i think the double quotes in the jquery, the "{{response}}" are converting it into text.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.