[julia-users] Re: @manipulate button not updating Markdown display

2015-07-21 Thread Renee Trochet
Thank you so much for your help. I fixed the problem of writemime calling 
display and am using the latest IJulia master. I can get (some) markdown to 
display correctly when the button is clicked (though line breaks don't work 
as expected). However, I have two problems:

   1. I can't get LaTeX to display in Markdown using writemime. The display 
   function works perfectly, but writemime does not. Are LaTeX equations 
   currently unsupported, or did I mess something up?
   2. The text doesn't clear when I click the button off. I've tried 
   replacing output by printing  and clearing output with IJulia.
   clear_output(false), but no luck.

Here's what I'm doing:

import Base: writemime, show, write

function write(io::IO, x::Revealable)
write(io, x.content)
end

function writemime(io::IO, ::MIMEtext/markdown, x::Revealable)
if x.show
write(io, x)
else
#write(io,)   -- I tried this too without success
IJulia.clear_output(false)
end
end

function show(io::IO, x::Revealable)
Base.print_quoted_literal(io, x.content)
end

Here's the whole thing if you want to run it:
using Reactive
using Interact
using IJulia

type Revealable
content::ASCIIString
label::ASCIIString
show::Bool
end

function revealable(x::Revealable)
@manipulate for n in togglebutton(; label=string(Show/Hide, x.label == 
 ?  : string( , uppercase(x.label[1]),x.label[2:end])), value=x.show, 
signal=Input(x.show))
x.show = n
x
end
end

import Base: writemime, show, write

function write(io::IO, x::Revealable)
write(io, x.content)
end

function writemime(io::IO, ::MIMEtext/markdown, x::Revealable)
if x.show
write(io, x)
else
#write(io,)   -- I tried this too without success
IJulia.clear_output(false)
end
end

function show(io::IO, x::Revealable)
Base.print_quoted_literal(io, x.content)
end

### To run it:
r = Revealable(#This is an *awesome* equation: \$e^u \\frac{du}{dx} e^x 
dx\$.,example,false)
revealable(r)



On Friday, July 17, 2015 at 7:06:37 AM UTC-7, Steven G. Johnson wrote:

  

 function writemime(stream, ::MIMEtext/latex, x::Revealable) #was 
 ::MIMEtext/markdown in my actual code
if x.show
 display(x.content)
 else
 display()
 end
 end


 This is wrong.  writemime should not call display.  It should just write 
 the LaTeX form of x to the (text) stream using print or write.  (You have 
 it backwards: display calls writemime.) 

 With regards to your other equation; for mixing equations and formatted 
 text, the latest IJulia master [Pkg.checkout(IJulia)] can display 
 text/markdown directly, including equations, e.g.:

  display(text/markdown, This is an *awesome* equation: \$e^u 
 \\frac{du}{dx} e^x dx\$.)

 So, in an @manipulate loop, you just need to return an object that knows 
 how to display itself as text/markdown (i.e. that has a writemime method to 
 output text/markdown text to an I/O stream).



[julia-users] @manipulate button not updating Markdown display

2015-07-16 Thread Renee Trochet
Hi all,

I'm working on a module that will use a toggle button to show and hide 
content in an IJulia notebook. Everything works as intended if the content 
is HTML, but I also need to be able to display equations. I have two 
questions:

   1. Is it possible to display equations within blocks of HTML? If so, 
   how? It would be nice to take advantage of the MathJax already running in 
   the notebooks. I'd love it if someone could point me in the right direction.
   2. How can I get Markdown to hide/show when the button is 
   clicked? Currently, clicking the button correctly changes the field that 
   determines whether to display the object, but you have to run the code cell 
   again for the display to change (see Section 2).

Below are the relevant bits of code.

I'd appreciate any pointers you can give. Thank you!

-

*1) *This code for HTML blocks works correctly:

using Reactive
using Interact
import Base.writemime

type Revealable
html::ASCIIString
divclass::ASCIIString
show::Bool
end

function revealable(x::Revealable)
@manipulate for n in togglebutton(; label=string(Show/Hide, x.divclass 
==  ?  : string( , uppercase(x.divclass[1]),x.divclass[2:end])), value
=x.show, signal=Input(x.show))
x.show = n
x
end
end

function Base.writemime(stream, ::MIMEtext/html, x::Revealable)
if x.show
println(stream, string(div class=', x.divclass, ', x.
html, /div))
else
println(stream, 

)
end
end

To run it:

h = Revealable(bAny HTML can go here!/b, hint, false)
revealable(h)



*2)* This code requires the user to re-run the cell after clicking the 
button:

using Markdown
using Reactive
using Interact
import Base.writemime

type Revealable
content::Markdown.MD
divclass::ASCIIString
show::Bool
end

function revealable(x::Revealable)
@manipulate for n in togglebutton(; label=string(Show/Hide, x.divclass 
==  ?  : string( , uppercase(x.divclass[1]),x.divclass[2:end])), value
=x.show, signal=Input(x.show))
x.show = n
x
end
end

function writemime(stream, ::MIMEtext/latex, x::Revealable)
   if x.show
display(x.content)
else
display()
end
end

To run it:

m = Revealable(md
#Heading!

Here is some LaTeX: ${3+a}\over{2-b^4}$
, hint, false)

revealable(m)