<div dir="ltr"><div><div><div><div><div><div><div><div><div><div><div><div>Hey Conrad!<br><br></div>You're right and sorry I wasn't quite clear - this has approximately nothing to do with Python itself. IPython provides a language-agnostic way to use its interfaces, without ever dealing with Python, so really the only thing this project has to do with Python is that the frontends for IPython are implemented in it. The two IPython frontends that this has been tested with are the simple console (just a normal terminal console) and the notebook. The notebook is an interface modeled after Mathematica's notebook style, with code cells which you can evaluate interspersed with text and output. All of this is in a web browser, so anything that you can display as HTML can be output - this means that if you can display your plot in HTML, you can have the IPython notebook display it as output. (The HTML even includes javascript, I believe, which means the output itself can be interactive!)<br>

<br></div>The goal of IHaskell is exactly what you mention - plotting and viewing complex data structures in a REPL. The way this works in IPython is as follows:<br><br></div>1. IPython sends a "execute_request" message to the IHaskell kernel, which says, "please execute this code".<br>

</div>2. IHaskell can reply with several messages as it processes the code, one of which is called "display_data". This message encodes in it different representations of the same data - for instance, we can display something as plain text, or HTML, or SVG, or Latex, and the display_data message will have essentially a dictionary that maps mimetype ("text/plain", "text/latex", "text/html") to the actual data. <br>

</div>3. The IPython frontend gets the display_data message and decides the best representation to use. So, if the frontend is a text console, it won't use the HTML and image representations; however, if its the notebook console, it'll always choose the richest display, whether it's LaTex or HTML or SVG or anything else.<br>

<br></div>Thus, in order to display more complex data types, the IHaskell kernel has to know about those data type. It has to encode them somehow into HTML or Latex, and then send them in that form, and IHaskell notebook will display them.<br>

<br></div>This mechanism is already implemented in IHaskell. You can see that in action if you trigger an error or use a GHCi-style ":" directive. The ":" directives are unimplemented, so it just echoes back a bit of HTML that prints the directive in green. If you trigger an error, the error is wrapped in a bit of styled HTML to make it red and italic. These two are just "proof-of-concept" for the display_data communication - hopefully this can soon be extended to more interesting things, such as plotting. (Another example is JSON - if your output is a JSON string, it should be displayed as nice syntax-highlighted JSON. The HTML can even include Javascript, so the JSON output could have things fold and unfold or other interactive features, and the plotting output could have some sort of nice plot editing or animation via JS)<br>

<br></div>To give some code from IHaskell that might clarify some of these thoughts:<br><br></div><span style="font-family:courier new,monospace">-- Interface to the code evaluator. Takes a code string, outputs different data representations.<br>

evaluate :: String -> [DisplayData]<br><br></span></div></div><span style="font-family:courier new,monospace">-- Data for display: a string with associated MIME type.<br>data DisplayData = Display MimeType String<br><br>

-- Possible MIME types for the display data.<br></span></div><span style="font-family:courier new,monospace">-- Should eventually include latex, png, svg, etc.<br></span><div><span style="font-family:courier new,monospace">data MimeType = PlainText | MimeHtml<br>

<br></span></div><div><span style="font-family:courier new,monospace">-- The main evaluator which uses the GHC api (code simplified for presentation purposes)<br></span></div><div><div><div><div><div><div><div><div><div>
<div>
<span style="font-family:courier new,monospace">evalCommand (Statement stmt) = do<br></span></div><div><span style="font-family:courier new,monospace">    -- Capture stdout from the code we're running<br></span></div>

<div><span style="font-family:courier new,monospace">    (printed, result) <- capturedStatement stmt<br>    case result of<br>      RunOk boundNames -><br></span></div><div><span style="font-family:courier new,monospace">        -- On success, just return the printed text<br>

</span></div><div><span style="font-family:courier new,monospace">        return [Display PlainText printed]<br>      RunException exception -> do<br></span></div><div><span style="font-family:courier new,monospace">        -- On error, show the exception in red italics<br>

</span></div><div><span style="font-family:courier new,monospace">        return [Display MimeHtml $ makeError $ show exception]</span><br><br></div><div>Note the "on success" bit, which returns just <span style="font-family:courier new,monospace">[Display PlainText printed]</span>. We can use the GHC API here to look at the types and values of any bound names, or potentially parse the stdout output, and then use those to generate DisplayData values that are more useful than just the printed plain text. This is where we can extend IHaskell greatly, and make it give us <i>really</i> pretty output. I have not figured out exactly how to use the GHC API to do all of this and how to make it modular, but that's the ultimate goal. (Hopefully, I can figure out some way such that if you install a package - say, "ihaskell-repa" - which includes some typeclass instances that we use for converting to DisplayData, IHaskell will automatically detect and use the installed package. This might involve even more GHC hackery and dynamic linking, though, but I think it should be possible.)<br>

<br></div><div>Hope this clarified things. Let me know if you have any more questions :)<br></div><div>-- Andrew<br></div></div></div></div></div></div></div></div></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">

On Mon, Oct 21, 2013 at 4:08 PM, Conrad Parker <span dir="ltr"><<a href="mailto:conrad@metadecks.org" target="_blank">conrad@metadecks.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div dir="ltr"><br><div class="gmail_extra"><div class="gmail_quote"><div><div class="h5">On 22 October 2013 08:32, Andrew Gibiansky <span dir="ltr"><<a href="mailto:andrew.gibiansky@gmail.com" target="_blank">andrew.gibiansky@gmail.com</a>></span> wrote:<br>



<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div>Hey everyone!<br><br>I'm excited to announce the initial release of IHaskell, a Haskell language kernel for IPython:<br>



<br><a href="http://gibiansky.github.io/IHaskell" target="_blank">http://gibiansky.github.io/IHaskell</a><br>

<br></div>IHaskell allows IPython to communicate with the kernel and evaluate Haskell code. This enables using IPython interfaces such as console, qtconsole, and most importantly, IPython notebook with Haskell. The IPython Notebook supports image, HTML, and LaTeX output, which enables things such as Javascript viewers for complex data structures or embedded graphs and makes it a great platform for interactive computing.<br>





<br></div>You can take a look at the code and install instructions here:<br><br><a href="https://github.com/gibiansky/IHaskell" target="_blank">https://github.com/gibiansky/IHaskell</a><br><br></div>A general overview of the architecture is available as well:<br>





<br><a href="http://andrew.gibiansky.com/blog/ipython/ipython-kernels/" target="_blank">http://andrew.gibiansky.com/blog/ipython/ipython-kernels/</a><br><br></div>Contributing: IHaskell is a very young project, but one that I think would make a great contribution to the Haskell community. There's still tons and tons left to do before it's on par with what IPython notebook is capable of, so if you're at all interested in contributing, please get in touch! I've tried to keep everything well-documented and readable, but I will also be happy to answer questions about the code myself.<br>





<br></div><div>If you find any bugs or edge cases or installation difficulties, please send me an email or create a Github issue for it!<br></div></div></blockquote><div><br></div></div></div><div>Hi Andrew,</div><div><br>

</div><div>

this looks very cool! At first I was a bit wary of the "python" aspects of it, but I dug around a bit in the links you provided, and it seems:</div><div><br></div><div>  * IHaskell is implemented purely in Haskell</div>



<div>  * the communication with IPython is via ZeroMQ sockets</div><div>  * once you throw data over the wall (through ZeroMQ) it gets routed via python to whatever backend (web, LaTeX etc.)</div><div><br></div><div>One thing there has been a lot of demand for in the Haskell community is plotting from a repl. It looks like IHaskell could solve that well, given that all the plumbing already exists. How would you go about displaying, say, a Vector Double?</div>



<div><br></div><div>I assume displaying more complex types would require some custom code. Do you have any examples of how you would use IHaskell to display a data structure in a web browser?</div><span class="HOEnZb"><font color="#888888"><div>

<br></div><div>Conrad.</div>

</font></span></div></div></div>
</blockquote></div><br></div>