Alligator
Alligator

Reputation: 730

How can I pass a variable from a javascript form to python (on the same computer)?

I have a javascript form with three sliders where a user can select three different values:

<form action="submit.php">
<div class="container">

<div class="slidecontainer">
    <input type="range" min="0" max="100" value="50" class="slider" id="red">
</div>

<div class="slidecontainer">
  <input type="range" min="0" max="100" value="50" class="slider" id="green">
</div>

<div class="slidecontainer">
  <input type="range" min="0" max="100" value="50" class="slider" id="blue">
</div>

<button type="submit" class="button">Submit</button>
</div>
</div>
</form>

I'd like to pass all three values (red, green, blue) to python so I can use them with other code, on the same computer. I tried using ajax and the example I saw here, but I think I'm submitting the form incorrectly. How can I post the data instead of calling submit.php so that I can read it with python?

Upvotes: 3

Views: 194

Answers (1)

logicOnAbstractions
logicOnAbstractions

Reputation: 2580

Simple. Use Cherrypy

Put that in a file (say main.py):

import random
import string

import cherrypy


class StringGenerator(object):
    @cherrypy.expose
    def index(self):
        return """<html>
          <head></head>
          <body>
<form name="search" action="/home" method="get">
Search: <input type="text" name="box">
<input type="submit" value="Submit">
<div class="container">

<div class="slidecontainer">
    <input type="range" min="0" max="100" value="50" class="slider" id="red">
</div>

<div class="slidecontainer">
  <input type="range" min="0" max="100" value="50" class="slider" id="green">
</div>

<div class="slidecontainer">
  <input type="range" min="0" max="100" value="50" class="slider" id="blue">
</div>

<button type="submit" class="button">Submit</button>
</div>
</div>
</form>
          </body>
        </html>"""

    @cherrypy.expose
    def generate(self, length=8):
        return ''.join(random.sample(string.hexdigits, int(length)))

    @cherrypy.expose
    def home(self, box):
        print("You've entered in the form: "+ str(box))

        return "THANKS"


if __name__ == '__main__':
    cherrypy.quickstart(StringGenerator())

Then in a terminal:

python main.py

Then go to : http://127.0.0.1:8080 & see magic happening when you click submit (the one next to the search box I added to your code):

You've entered in the form: I entered something in the form...

I litteraly just pasted an example from the doc (& tweaked to fit your code a bit more). Other than

pip install cherrypy

you really have nothing else to do to get started. By far the easiest way to make a webserver in python imo.

Essentially, when you do

@cherrypy.expose

the name of that method becomes https://127.0.0.1:8080/my_new_endpoint

You can easily serve stuff to different endpoints & pass arguments (as shown). So you can very easily add new enpoints. See docs for details.

Have fun!

Upvotes: 3

Related Questions