Reputation: 3
i am doing maintenance on a website that is using Python and CGI. The site currently runs on Python, but for some weird reason I have to assign a Python variable to a JavaScript variable on the same Python file. I have tried many things and they have not worked, I don't know if it's even possible to do so. Any help would be greatly appreciated. I have simplified the code just to show the variable exchange from Python to JavaScript I need to do. My Python version is 3.5.2 and the site is running on IIS server. Again thank you very much for your help and time.
import cgi
print('')
temp = 78 #These is the Python variable I need to assign to the Javascript variable.
print('<html>')
print('<script type="text/javascript">')
print('var x = {{temp}};') #Here's how I'm trying to assign the python variable.
print('document.write(x);')
print('document.write("HELLO PYTHON VARIABLE!!");')
print('</script>')
print('</html>')
Upvotes: 0
Views: 3888
Reputation: 149
print('var x = {{temp}};') #Here's how I'm trying to assign the python variable.
It looks like you are trying to do a template thing without actually being in a templating engine.
Try this instead:
print( 'var x =' + temp + ';' )
Upvotes: 2
Reputation: 1292
Many ways to do this:
print ('var x = %d' % temp)
print ('var x = ' + str(temp) + ';')
print ('var x = {{temp}};'.replace("{{temp}}", str(temp)))
Upvotes: 1