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.

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...
    – ngstschr
    CommentedApr 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.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.