tchichua25
Posts: 26
Joined: Sun Sep 20, 2015 12:27 pm
Location: Georgia/Tbilisi

Using Python Variables in HTML in multiline Python string

Thu Aug 10, 2017 9:55 pm

Hey everyone,
I´m trying to write HTML code using python and execute it from browser. Here is my code:

Code: Select all

import webbrowser

f = open('image.html','w')

message = """<html>
<head></head>
<body><img src="URL"></body>
</html>"""

f.write(message)
f.close()

filename = 'file:///home/pi/' + 'image.html'
webbrowser.open_new_tab(filename)
Simple code, works like a charm!

Now I want to make little ¨UI¨, so that user will be able to input the URL. So my question is, can I put Python Variable into the HTML code instead of URL?
for example:

Code: Select all

a = ¨webpage.com/image.jpg¨
...
<img src="a">
...
For sure, I know that the syntax is super wrong, I just wanted to give you an example of what I´m trying to achieve.
cheers!

User avatar
elParaguayo
Posts: 1874
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Using Python Variables in HTML in multiline Python string

Fri Aug 11, 2017 5:16 am

Yes. You can do this:

Code: Select all

a = "webpage.com/image.jpg"

message = """<html>
<head></head>
<body><img src="{URL}"></body>
</html>"""

new_message = message.format(URL=a)
# I've put the formatted message in a new variable 
# so you can reuse "message" as a template
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

tchichua25
Posts: 26
Joined: Sun Sep 20, 2015 12:27 pm
Location: Georgia/Tbilisi

Re: Using Python Variables in HTML in multiline Python string

Fri Aug 11, 2017 9:00 am

elParaguayo wrote:
Fri Aug 11, 2017 5:16 am
Yes. You can do this:

Code: Select all

a = "webpage.com/image.jpg"

message = """<html>
<head></head>
<body><img src="{URL}"></body>
</html>"""

new_message = message.format(URL=a)
# I've put the formatted message in a new variable 
# so you can reuse "message" as a template
thanks for the answer,
I have updated my code:

Code: Select all

import webbrowser

lat = "41.716328"
lon = "44.807572"

f = open('map.html','w')

message = """<html>
<head></head>
<body><img src="https://maps.googleapis.com/maps/api/staticmap?center={Latitude},{Longitude}&zoom=12&size=400x400&key=[my_api_key]"></body>
</html>"""

new_message = message.format(Latitude = lat)
new_message = message.format(Longitude = lon)

f.write(message)
f.close()

filename = 'file:///home/pi/' + 'map.html'
webbrowser.open_new_tab(filename)
Unfortunately, I'm getting the error:
Traceback (most recent call last):
File "/home/pi/mapper.py", line 13, in <module>
new_message = message.format(Latitude = lat)
KeyError: 'Longitude'

I'm afraid I'm not using this method right. Can you give me any advice how to solve this problem and what subject to read to get little bit more idea about how this things work?
Thanks again!

scotty101
Posts: 2291
Joined: Fri Jun 08, 2012 6:03 pm

Re: Using Python Variables in HTML in multiline Python string

Fri Aug 11, 2017 9:03 am

Do the format operation in a single command. Format expects that every word contained inside curly braces {} will have an associated named parameter.

Code: Select all

new_message = message.format(Latitude = lat, Longitude = lon)
Format works well for simple webpages but if you want to do something more complicated look at Jinja2 which is a fully functional HTML templating language
http://jinja.pocoo.org/
https://pythonadventures.wordpress.com/ ... -template/
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

tchichua25
Posts: 26
Joined: Sun Sep 20, 2015 12:27 pm
Location: Georgia/Tbilisi

Re: Using Python Variables in HTML in multiline Python string

Fri Aug 11, 2017 9:29 am

scotty101 wrote:
Fri Aug 11, 2017 9:03 am
Do the format operation in a single command. Format expects that every word contained inside curly braces {} will have an associated named parameter.

Code: Select all

new_message = message.format(Latitude = lat, Longitude = lon)
Format works well for simple webpages but if you want to do something more complicated look at Jinja2 which is a fully functional HTML templating language
http://jinja.pocoo.org/
https://pythonadventures.wordpress.com/ ... -template/
Thank you so much!
Syntax error was solved. The thing now is that it's giving me the wrong location. I mean, when I'm typing latitude and longitude in URL it shows me my location, but when I'm using variables it's giving me different picture. So I've checked HTML file and unfortunately this what I'm seeing:

Code: Select all

<html>
<head></head>
<body><img src="https://maps.googleapis.com/maps/api/staticmap?center={Latitude},{Longitude}&zoom=12&size=400x400&key=[my_api_code]></body>
</html>
It means that Latitude and Longitude are not replaced with lat and lon variable values. What am I doing wrong?

this is the code:

Code: Select all

import webbrowser

lat = "41.716328"
lon = "44.807572"

f = open('map.html','w')

message = """<html>
<head></head>
<body><img src="https://maps.googleapis.com/maps/api/staticmap?center={Latitude},{Longitude}&zoom=12&size=400x400&key=[My_api_code]"></body>
</html>"""

new_message = message.format(Latitude = lon, Longitude = lat)

f.write(message)
f.close()

filename = 'file:///home/pi/' + 'map.html'
webbrowser.open_new_tab(filename)
Last edited by tchichua25 on Fri Aug 11, 2017 9:34 am, edited 1 time in total.

scotty101
Posts: 2291
Joined: Fri Jun 08, 2012 6:03 pm

Re: Using Python Variables in HTML in multiline Python string

Fri Aug 11, 2017 9:34 am

You are writing message not new_message to the html file.

Running the modified code on my machine give the following HTML

Code: Select all

<html>
<head></head>
<body><img src="https://maps.googleapis.com/maps/api/staticmap?center=44.807572,41.716328&zoom=12&size=400x400&key=[My_api_code]"></body>
</html>
Looks correct aside from the missing api key.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

tchichua25
Posts: 26
Joined: Sun Sep 20, 2015 12:27 pm
Location: Georgia/Tbilisi

Re: Using Python Variables in HTML in multiline Python string

Fri Aug 11, 2017 9:40 am

scotty101 wrote:
Fri Aug 11, 2017 9:34 am
You are writing message not new_message to the html file.

Running the modified code on my machine give the following HTML

Code: Select all

<html>
<head></head>
<body><img src="https://maps.googleapis.com/maps/api/staticmap?center=44.807572,41.716328&zoom=12&size=400x400&key=[My_api_code]"></body>
</html>
Looks correct aside from the missing api key.
Works perfectly! Thank you so much for your help! Both of you have saved my day!
I liked what I read about Jinja2 for now, I'll go deeper little later.
Can you advice me what to read about all this "formatting message" stuff?

scotty101
Posts: 2291
Joined: Fri Jun 08, 2012 6:03 pm

Re: Using Python Variables in HTML in multiline Python string

Fri Aug 11, 2017 10:11 am

Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

tchichua25
Posts: 26
Joined: Sun Sep 20, 2015 12:27 pm
Location: Georgia/Tbilisi

Re: Using Python Variables in HTML in multiline Python string

Fri Aug 11, 2017 11:28 am

Thank you so much!

Return to “Python”

Who is online

Users browsing this forum: SlowBro and 17 guests