Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Displaying html text rendered, not as plain text
-
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.
-
@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')
-
Yes, that helps. Thanks so much.
-
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.
-
@mikael welcome back, long time isn't it?
I think he wants the rendered text... -
@cvp, have been following along again now that the update emails work again. Thank you, or anyone else who might have made that happen.
-
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.