In Python, how to open a string representing HTML in the browser?
Question:
I’d like to view a Django template in the browser. This particular template is called in render_to_string
, but is not connected to a view, so I can’t just runserver
and navigate to the URL at my localhost
.
My idea was to simply call render_to_string
in the Django shell and somehow pass the resulting string to a web browser such as Chrome to view it. However, as far as I can tell the webbrowser
module only accepts url
arguments and can’t be used to render strings representing HTML.
Any idea how I could achieve this?
Answers:
you could convert the html string to url:
Use Data URL:
import base64
html = b"..."
url = "text/html;base64," + base64.b64encode(html)
webbrowser.open(url)
Following Launch HTML code in browser (that is generated by BeautifulSoup) straight from Python, I wrote a test case in which I write the HTML to a temporary file and use the file://
prefix to turn that into a url
accepted by webbrowser.open()
:
import tempfile
import webbrowser
from django.test import SimpleTestCase
from django.template.loader import render_to_string
class ViewEmailTemplate(SimpleTestCase):
def test_view_email_template(self):
html = render_to_string('ebay/activate_to_family.html')
fh, path = tempfile.mkstemp(suffix='.html')
url = 'file://' + path
with open(path, 'w') as fp:
fp.write(html)
webbrowser.open(url)
(Unfortunately, I found that the page does not contain images referenced by Django’s static
tag, but that’s a separate issue).
Here’s a more concise solution which gets around the possible ValueError: startfile: filepath too long for Windows
error in the solution by @marat:
from pathlib import Path
Path("temp.html").write_text(html, encoding='utf-8')
webbrowser.open("temp.html")
Path("temp.html").unlink()
I’d like to view a Django template in the browser. This particular template is called in render_to_string
, but is not connected to a view, so I can’t just runserver
and navigate to the URL at my localhost
.
My idea was to simply call render_to_string
in the Django shell and somehow pass the resulting string to a web browser such as Chrome to view it. However, as far as I can tell the webbrowser
module only accepts url
arguments and can’t be used to render strings representing HTML.
Any idea how I could achieve this?
you could convert the html string to url:
Use Data URL:
import base64
html = b"..."
url = "text/html;base64," + base64.b64encode(html)
webbrowser.open(url)
Following Launch HTML code in browser (that is generated by BeautifulSoup) straight from Python, I wrote a test case in which I write the HTML to a temporary file and use the file://
prefix to turn that into a url
accepted by webbrowser.open()
:
import tempfile
import webbrowser
from django.test import SimpleTestCase
from django.template.loader import render_to_string
class ViewEmailTemplate(SimpleTestCase):
def test_view_email_template(self):
html = render_to_string('ebay/activate_to_family.html')
fh, path = tempfile.mkstemp(suffix='.html')
url = 'file://' + path
with open(path, 'w') as fp:
fp.write(html)
webbrowser.open(url)
(Unfortunately, I found that the page does not contain images referenced by Django’s static
tag, but that’s a separate issue).
Here’s a more concise solution which gets around the possible ValueError: startfile: filepath too long for Windows
error in the solution by @marat:
from pathlib import Path
Path("temp.html").write_text(html, encoding='utf-8')
webbrowser.open("temp.html")
Path("temp.html").unlink()