0

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>')
1
  • Is it just a number, or does the variable hold user input? (because then there's security involved) Commented Jan 13, 2017 at 18:25

2 Answers 2

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 + ';' )
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Evan Thompson! You'r absolutely right I need to do that, in meantime, these solved mi immediate problem, it worked perfectly, thank you very much!
1

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

Thank you very much GantTheWanderer!! It is just numbers i wanted to exchange, both ways worked perfectly!!! I had many days stuck whit these, you just saved a lot of time, again thanks for your kind response. Best regards from Guatemala.
De nada, writing programs that write programs can be tricky at first, but with practice it become second nature. Other pitfalls include having to deal with quotes (ie temp = "Occam's Razor" print ("var x = '" + temp + "'") you could get in trouble fast keeping track of them.
Muchas gracias otra vez Grant! Wow, you are right it is really hard to keep track of the quotes, i could not get your string example to work, but i was not taking to count that i'am using version 3.5 and i think, yours is 2.7 because of the double quotes on "Occam's Razor" it worked for mi like this 'Occam's Razor' with a single quote, thanks for the example with strings, because i just stumble upon with exactly that issue right now, thanks a lot!!
Right on hombre! Don't worry it wont be the last time you see such things. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.