0

I have the following html code (index.html):

<!DOCTYPE html>
<html>
<head>
 <script>
 function addParagraphText()
 {
 var arg1=2
 var arg2=34
 var arg3="john"
 var myResult = runMe.py(arg1, arg2, arg3)
 document.getElementById("para").innerHTML = myResult
 }
 </script>
</head>
<body>
<button onclick="addParagraphText();">Click me</button>
<p id="para"></p>
</html>

and the following python file runMe.py:

import sys
myArg1 = sys.argv[1]
myArg2 = sys.argv[2]
myArg3 = sys.argv[3]
# some long calculations using various libraries and modules
myOutput = '''<table style=\"border:solid;\"><tr><td> bla </td><td> lol bla <button>myButton</button> </td></tr><tr><td> blabla </td><td> blablabla </td></tr></table>'''
return myOutput

Obviously it doesn't work. What would be the easiest and quickest way to do it? I have a very long Python script that does some calculations and outputs a rather long string in html format which I would like to present after user clicks on the button. As of now, I do not need a state of the art approach, I am rather looking for a fast and easy solution.

halfer
20.2k20 gold badges111 silver badges208 bronze badges
asked Apr 27, 2017 at 12:37
1
  • 1
    You can't run python scripts in the browser. What you can do, is run a server with your python script and make calls to it... Commented Apr 27, 2017 at 12:41

1 Answer 1

2

In short: JavaScript in a html page executed by a browser cannot start the python interpreter and cannot run python scripts.

Browser security will prevent this.

You'll have to use something like XHR and cgi like in this question. The first answer seems to be a complete example. Otherwise try wsgi python.

answered Apr 27, 2017 at 12:41
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.