How to use text/html MIME types in IJulia

543 views
Skip to first unread message

Glen Hertz

unread,
Nov 30, 2013, 5:22:12 PM11/30/13
to julia...@googlegroups.com
Hi,

I'm trying to figure out how to display text/html MIME types in IJulia.  I have the following

type Foo
    a
end
function writemime(stream, ::MIME"text/html", x::Foo)
   
"""
  <b><blink>HTML of Foo...</blink></b>
    """
 
end
println
(Foo(1))
Foo(1)  # this is displayed


How do I get it to display in HTML?  Also, do I need a complete html block (eg starting with <html>) or just an html fragment (as above)? 

Thanks,

Glen

Keno Fischer

unread,
Nov 30, 2013, 5:24:31 PM11/30/13
to julia...@googlegroups.com
You need to actually do the io, e.g. 

function writemime(stream, ::MIME"text/html", x::Foo)

    print(stream,
"""

  <b><blink>HTML of Foo...</blink></b>
    """
  
end

Other than that, I don't think println will actually go through the display system, however, the last value in the cell (i.e. the "return value" of the cell will). 

Steven G. Johnson

unread,
Nov 30, 2013, 5:56:32 PM11/30/13
to julia...@googlegroups.com
As Keno pointed out, your writemime needs to actually write the HTML data to the stream, not return it.

The print(foo) function always displays the text/plain MIME type.

If you want to display text/html, assuming you have a writemime method for text/html, you can either:

 * use display(foo)
* use display("text/html", foo)
* evaluate a code cell in which foo is the last expression evaluated (and hence is the output value)

Note also that you need
    import Base.writemime
before you add a new writemime method.

Glen Hertz

unread,
Nov 30, 2013, 7:35:23 PM11/30/13
to julia...@googlegroups.com
Hi,

Thanks. Things are now working with:

type Foo
  a
end
f
= Foo(1)
function Base.writemime(stream, ::MIME"text/html", x::Foo)
  println
(stream, """

  <b><blink>HTML of Foo...</blink></b>
    """
 
 
)
end


display
(f)

HTML of Foo...

Is there a function that escapes special HTML characters (like '<')?

Glen

Steven G. Johnson

unread,
Nov 30, 2013, 7:47:45 PM11/30/13
to julia...@googlegroups.com
On Saturday, November 30, 2013 7:35:23 PM UTC-5, Glen Hertz wrote:
Is there a function that escapes special HTML characters (like '<')?

Not that I know of.

Leah Hanson

unread,
Dec 1, 2013, 4:17:34 PM12/1/13
to julia...@googlegroups.com
Is there a function that escapes special HTML characters (like '<')?

In the HttpCommon package, there's a function `escapeHTML` that takes care of &, <, >, and ".

-- Leah
Reply all
Reply to author
Forward
0 new messages