0

I want to be able to use variables like returnJson["hdrul"] in the html, but I'm not sure how.

out = open("nasa.html", "w")
link = "https://api.nasa.gov/planetary/apod?api_key=api"
x = urllib.request.urlopen(link)
url = x.read()

returnJson = json.loads(url)
print('\n')
print(returnJson["hdurl"])


html = """<html>
<head></head> 
<body> 
<img src= returnJson["hdurl"]>
</body> 


</html> """

out.write(html)
out.close()
False King
  • 37
  • 3
  • 4
    You should use string formatting or f-strings if possible. – SaGaR Feb 27 '21 at 01:29
  • @costaparas f-strings are a better alternative. – SaGaR Feb 27 '21 at 01:38
  • 1
    @sagar Yes, I agree, f-strings are the preferred way. If you look closely, there are many answers in that duplicate link, including f-strings. So there's really nothing new being asked here. All the standard methods have already been explained in-depth many times over. – costaparas Feb 27 '21 at 01:39
  • @costaparas i agree – SaGaR Feb 27 '21 at 01:45
  • I'll mess around with both and see which works better, but the link is definitely helpful. – False King Feb 27 '21 at 01:46

1 Answers1

1

This is an example of f-string that you can use:

data = 596
html = f"""<html>
<head></head> 
<body> 
The data is : {data}
</body> 
"""

Whatever is in curly brackets get interpreted as normal python.

SaGaR
  • 534
  • 4
  • 11