简体   繁体   中英

Python to Javascript via JSON

I'm trying to send int data from Python to Javascript. I'd like to use input from python to control elements in Javascript. I'm just having trouble getting data sent from Python's JSON to Javascript. Below are my attempts with little luck, Python code works excellent, how to I get Javascript to read and turn that data into a variable?

Thanks in Advance: :)

import json
    
    x = 1
    
    while True:
    
        x = int(input())
    
        if 10 <= x <= 900:
            x += 100
    
            print(x)
    
            json.dumps(x)

Javascript:

<script type = "text/javascript">

  document.getElementById("page").style.fontWeight = json.dumps(x);

    </script>

I'm assuming you're new to programming since you seem to have an oversimplified understanding of making two units of code communicate. The proper way to do that depends greatly on the context in which the code is executed.

From the snippets you included one can guess you want to run Python server-side and JavaScript client-side in the browser. For this setup you probably want to set up a web server or web socket in Python and query it or connect to it with your JS script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM