Hello everyone. I have some html in a string that I would like to display (rendered, not as plain text). Would someone know a quick and easy way to do this using Pythonista? I would really appreciate it. Thanks so much.
Forum Archive
Displaying html text rendered, not as plain text
jaalburquerque
May 16, 2022 - 08:14
cvp
May 16, 2022 - 08:30
@jaalburquerque perhaps something like
import ui
html = '''
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Title</title>
<style type="text/css">
/* CSS styles here... */
</style>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
'''
v = ui.WebView(frame=(0, 0, 320, 100))
v.scales_page_to_fit = False
v.load_html(html)
v.present('sheet')
jaalburquerque
May 16, 2022 - 14:22
Yes, that helps. Thanks so much.
mikael
May 17, 2022 - 14:00
And if you just need the text directly:
from bs4 import BeautifulSoup
html = '''
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Title</title>
<style type="text/css">
/* CSS styles here... */
</style>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
'''
soup = BeautifulSoup(html, "html5lib")
# Strip empty lines and extra white space
text = '\n'.join(
line.strip() for line in soup.text.splitlines()
if line.strip()
)
print(text)
There’s also a library you could install to get a nicer text representation as markdown, where h1 titles are underlined etc.
cvp
May 17, 2022 - 14:04
@mikael welcome back, long time isn't it?
I think he wants the rendered text...
mikael
May 17, 2022 - 14:39
@cvp, have been following along again now that the update emails work again. Thank you, or anyone else who might have made that happen.
jaalburquerque
May 17, 2022 - 17:32
Thank you for the information. I am working with HTML and I may need to reduce it to plain text for easy searching and processing later. The information is useful if I should need to reduce the HTML to plain text. Thanks again.