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>')
-
Is it just a number, or does the variable hold user input? (because then there's security involved)RemcoGerlich– RemcoGerlich2017年01月13日 18:25:15 +00:00Commented Jan 13, 2017 at 18:25
2 Answers 2
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 + ';' )
1 Comment
Many ways to do this:
print ('var x = %d' % temp)
print ('var x = ' + str(temp) + ';')
print ('var x = {{temp}};'.replace("{{temp}}", str(temp)))
4 Comments
temp = "Occam's Razor" print ("var x = '" + temp + "'") you could get in trouble fast keeping track of them.