Reputation: 12316
I have some python utilities that generate HTML output. Instead of saving to a temporary file, and opening that in a web browser, I would like to be able to just redirect the output to the browser to preview it. For example, something like:
myscript.py | open /Applications/Safari.app
Is there any way to do such a thing? I am on OS X.
Upvotes: 5
Views: 2907
Reputation: 66677
curl https://www.google.com | bcat
If you can have ruby on your system you may install bcat
:
gem install bcat
Make sure you have the gem binary folder in your path, e.g.:
/home/{YOUR_USER_NAME}/.local/share/gem/ruby/{VERSION}/bin
in my case that would be:
/home/philipp/.local/share/gem/ruby/3.0.0/bin
Then you'll have bcat
command at your disposal in which you can pipe in html and which will then open your browser as shown above.
Upvotes: 3
Reputation: 1027
open
utility in macOS is quite powerful, check man open
. Although -f
argument description mentions default text editor, it actually works with all apps. So for your case this should work:
myscript.py | open -f -a safari
Unfortunately open
just saves your input to a temporary .txt
file and browser treats it as plain text so you can't render html that way.
Upvotes: 0
Reputation:
About the best I can come up with is encoding the HTML as a data:
URL and making a browser open it:
import webbrowser, base64
html = "<b>hello world</b>"
b64url = "data:text/html;base64," + base64.b64encode(html)
webbrowser.get("safari").open(b64url)
However, this doesn't seem to work reliably for any browser other than Safari. I'm not sure how well it will work with large pages, either; the URL may eventually get too large to handle.
Upvotes: 2