Forum Archive

Displaying html text rendered, not as plain text

jaalburquerque

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.

cvp

@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

Yes, that helps. Thanks so much.

mikael

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

@mikael welcome back, long time isn't it?
I think he wants the rendered text...

mikael

@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

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.