Rendering HTML from a pipe

15

1

I would like to be able to generate HTML then pipe it to a program which will render it, something like this:

for i in 1 2 3
do
    for j in a b c
    do
        echo "<table border="1"><tr><td>$i</td><td>$j</td></tr></table>"
    done
done | /usr/bin/firefox

Unfortunately, firefox can't render data piped in from stdin. Neither can google-chrome. lynx can, but who wants to use that?

I tried creating a named pipe, opening that in chrome and/or firefox and then piping data to that -- but the browser didn't update when I sent data through the named pipe.

Are there any non text-based browsers which will render html from stdin? The output doesn't need to be glitzy, I'm mostly interested in making delimited data a little more readable, on the fly.

Edit:

I tried using bash's process substitution, e.g. firefox <(sh /tmp/tablegen.sh), that didn't work either. Worst case scenario, I could output to a temp file, render, then delete, but I would prefer a slightly more elegant solution.

Barton Chittenden

Posted 2012-12-31T14:15:32.123

Reputation: 1 698

1

Similar question here: http://unix.stackexchange.com/questions/24931/how-to-make-firefox-read-stdin

– wmz – 2012-12-31T14:34:13.900

Good link; pretty well confirmed my suspicion that using a temp file is the only way to go. – Barton Chittenden – 2013-01-01T17:24:15.737

Answers

14

From one of the answers on this question I found bcat:

NAME

  bcat - browser cat

DESCRIPTION

  The bcat utility reads from standard input, or one or
  more files, and pipes output into a web browser. file
  may be '-', in which case standard input is concatenated
  at that position.

  When invoked as btee, all input is written immediately
  to standard output in addition to being piped into
  the browser.

Now I can run a script like this:

$ python foo.py | bcat

...and the resultant HTML output opens in a new Firefox tab!

On Ubuntu and other Debian-based Linux distributions you can install bcat with this command:

$ sudo aptitude install ruby-bcat

dotancohen

Posted 2012-12-31T14:15:32.123

Reputation: 9 798

Hi from 2019, the above install didn't work but sudo gem install bcat did. :-) – roufamatic – 2019-11-06T22:06:49.830