omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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

    Pythonista
    3
    7
    1113
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • jaalburquerque
      jaalburquerque last edited by

      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 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @jaalburquerque last edited by 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')
        
        1 Reply Last reply Reply Quote 0
        • jaalburquerque
          jaalburquerque last edited by

          Yes, that helps. Thanks so much.

          1 Reply Last reply Reply Quote 0
          • mikael
            mikael last edited by

            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 1 Reply Last reply Reply Quote 0
            • cvp
              cvp @mikael last edited by

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

              mikael 1 Reply Last reply Reply Quote 0
              • mikael
                mikael @cvp last edited by

                @cvp, have been following along again now that the update emails work again. Thank you, or anyone else who might have made that happen.

                1 Reply Last reply Reply Quote 0
                • jaalburquerque
                  jaalburquerque last edited by

                  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.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post
                  Powered by NodeBB Forums | Contributors