0

I have a HTML input and onClick with a button, I get the value of text input and store in a varibale in Javascipt. Now how can I use this varibale as a paramter to a python function?

In JavaScript I have:

var text = document.getElementById("text").value;

and in Python I have:

someFunction(text):
   print(text) # text is what I want to be passed from javascript

I am using pyscript to run my python codes

Kyle Klaw
  • 1
  • 2
  • Does this answer your question? [How to pass HTML input text value as a parameter to python function?](https://stackoverflow.com/questions/74438369/how-to-pass-html-input-text-value-as-a-parameter-to-python-function) – Dakeyras Nov 14 '22 at 23:49
  • @Dakeyras that's actually a thread i made myself about an hour ago. I got partial answers but it wasnt about javascript to python – Kyle Klaw Nov 15 '22 at 00:07
  • Use an HTML button element that on click calls the Python function. The PyScript website has many examples. I wrote about 20 articles on PyScript: https://www.jhanley.com/blog/category/pyscript/ – John Hanley Nov 15 '22 at 00:13
  • There's also a guide in the PyScript documentation that might help you out. [How to Pass Objects from PyScript to Javascript (and Vice Versa)](https://docs.pyscript.net/latest/guides/passing-objects.html) – mdlatt Nov 17 '22 at 20:58

2 Answers2

0

You can submit the form to send values to a Python file or use API's with Ajax this article about difference between server-side and client-side.

And in regard to @JohnHanley comment you can use py-script to do that, take a look here

<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>

<script>
    name = "Guido" //A JS variable

    // Define a JS Function
    function addTwoNumbers(x, y){
        return x + y;
    }
</script>

<py-script>
    # Import and use JS function and variable into Python
    from js import name, addTwoNumbers

    print(f"Hello {name}")
    print("Adding 1 and 2 in Javascript: " + str(addTwoNumbers(1, 2)))
</py-script>
Mohamed EL-Gendy
  • 566
  • 4
  • 11
  • 1
    PyScript/Python runs in the browser. – John Hanley Nov 15 '22 at 00:11
  • @JohnHanley You're right, I totally forgot about `py-script` that's my bad – Mohamed EL-Gendy Nov 15 '22 at 00:41
  • @MohamedEL-Gendy As per OP, He want to get the updated text into the python script dynamically. But as per your demo it is print the static calculation based on param passed. Can you please make it as per the dynamic input passed from the text box ? – Debug Diva Nov 21 '22 at 07:26
  • @RohìtJíndal Maybe you will need to use the event listener handler in PyScript. Check this tutorial [how to add an Event Listener to your PyScript](https://morioh.com/p/38f31ff907e9) – Mohamed EL-Gendy Nov 21 '22 at 13:33
0

You can do a GET or a POST from your page to the python script. AJAX makes this quite easy for you - there is an existing answer to a similar question here on stack, have a look at:

Passing Javascript variable to Python

You might find it easier to parcel up the variable in a little json, which makes the GET/POST nice and simple, then import json in your python code and modify the function accordingly.

Also probably need to define an endpoint so the server knows to execute the python script

Noscere
  • 417
  • 3
  • 8