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.


    Open files in the webbroser via the file:/// url scheme

    Pythonista
    2
    3
    2919
    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.
    • halloleo235
      halloleo235 last edited by

      With Pyhonista I want to write a small documentation viewer for html files I have created in Pythonista.

      Let's say I have created a file "doco.html" under Pythonista's local file area. How can I view such a file via the webbrowser module?

      webbrowser.open("file:///doco.html")
      

      does not work for me. However the file certainly exists: With open("doco.html") as f I can read the lines of the html file straight away.

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

        You need to use the full path for a file URL, i.e. it needs to include the current directory path. You can get that via <code>os.getcwd()</code>. Here's a full example:

        <pre>import os
        import urllib
        import webbrowser

        filename = 'doco.html'
        with open(filename, 'w') as f:
        f.write('<h1>Hello World</h1>')
        full_path = os.path.join(os.getcwd(), filename)
        fileurl = 'file://' + urllib.pathname2url(full_path)

        webbrowser.open(fileurl)</pre>

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

          Many thanks, omz. That's exactly what I want! :-)

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