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
  • 19,824
  • 17
  • 99
  • 186
  • 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... – ngstschr Apr 27 '17 at 12:41

1 Answers1

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.

Community
  • 1
  • 1
AndreasT
  • 9,417
  • 11
  • 46
  • 60