IPython cell magic for C# usage in jupyter notebooks (clrmagic)

1,057 views
Skip to first unread message

Denis Akhiyarov

unread,
Sep 14, 2016, 12:40:04 AM9/14/16
to Project Jupyter, Xavier Dupré
As you may know there is already C# kernel for jupyter notebooks that was discussed previously here:


But sometimes using Python and C# in separate kernels is not very convenient.

Last week I noticed a blog post on how to use C# magic cells with pythonnet from IPython kernel by Xavier Dupré @sdpython from Microsoft:

http://www.xavierdupre.fr/blog/2014-09-20_nojs.html

As a result of this, we put together a package clrmagic that can be installed and loaded into jupyter notebooks:

pip install clrmagic

%reload_ext clrmagic

Here is a small demo:

%%CS mypower System.dll
public static double mypower(double x, double y)
{
 
if (y == 0) return 1.0;
 
return System.Math.Pow(x,y);
}

mypower
(3.0,3.0)
27.0

Note that clrmagic works and is tested both on Windows (.NET) and Linux (Mono)! It should also work on OSX (Mono), but I have not tested.

Currently the source code is hosted here:

https://github.com/denfromufa/clrmagic


The clrmagic was modeled after fortran magic which uses f2py to extend Python with Fortran.


Regards,

Denis

Thomas Kluyver

unread,
Sep 14, 2016, 6:45:16 AM9/14/16
to Project Jupyter
Neat, thanks Denis!

It's gathering dust a bit, but we do still have an IPython extensions index on the wiki, so feel free to add this there:
https://github.com/ipython/ipython/wiki/Extensions-Index

Thomas

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/31f2a31c-355c-4396-ba5e-74009758de6c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Thomas Kluyver

unread,
Sep 14, 2016, 10:21:49 AM9/14/16
to Project Jupyter
On 14 September 2016 at 15:00, Denis Akhiyarov <denis.a...@gmail.com> wrote:
I also noticed this blog post from 2013 by Brian Granger about pythonnet (underlying library for clrmagic) and it is completely invalid anymore. None of this black magic is required anymore. 

So can I delete it or mark obsolete?

I'd probably add a note at the top saying it's obsolete, with pointers to what people should do instead. If it's been linked to from elsewhere, an update is more useful than taking the page down entirely.

Lawrence D’Oliveiro

unread,
Sep 15, 2016, 12:07:00 AM9/15/16
to Project Jupyter, xavier...@gmail.com
On Wednesday, September 14, 2016 at 4:40:04 PM UTC+12, Denis Akhiyarov wrote:

But sometimes using Python and C# in separate kernels is not very convenient.

It’s a tradeoff: a kernel can maintain context across cells, but a magic makes it easy to mix different languages in a notebook.
 
Currently the source code is hosted here:
Have you thought of adding the option for graphical output? Or even Markdown or HTML output?

Denis Akhiyarov

unread,
Sep 15, 2016, 9:20:20 AM9/15/16
to Project Jupyter, xavier...@gmail.com
Can you provide any example of "graphical output" with any current language cell magics written for IPython kernel?

Doug Blank

unread,
Sep 15, 2016, 9:32:25 AM9/15/16
to jup...@googlegroups.com, xavier...@gmail.com
On Thu, Sep 15, 2016 at 9:20 AM, Denis Akhiyarov <denis.a...@gmail.com> wrote:
Can you provide any example of "graphical output" with any current language cell magics written for IPython kernel?

Depending on how your kernel is written, you might have to use the file system, like matlab or the octave kernels:



If your kernel can use the kernel wrapper, you might also want to look at basing your kernel on MetaKernel, to get additional, common magics, shell, etc:


-Doug
 

MinRK

unread,
Sep 15, 2016, 9:34:35 AM9/15/16
to Project Jupyter

On Thu, Sep 15, 2016 at 3:20 PM, Denis Akhiyarov <denis.a...@gmail.com> wrote:

Can you provide any example of "graphical output" with any current language cell magics written for IPython kernel?

rpy2 provides the %%R magic, which handles plotting.

-MinRK



On Wednesday, September 14, 2016 at 11:07:00 PM UTC-5, Lawrence D’Oliveiro wrote:
On Wednesday, September 14, 2016 at 4:40:04 PM UTC+12, Denis Akhiyarov wrote:

But sometimes using Python and C# in separate kernels is not very convenient.

It’s a tradeoff: a kernel can maintain context across cells, but a magic makes it easy to mix different languages in a notebook.
 
Currently the source code is hosted here:
Have you thought of adding the option for graphical output? Or even Markdown or HTML output?

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.

Denis Akhiyarov

unread,
Sep 15, 2016, 9:40:46 AM9/15/16
to Project Jupyter
I updated the page:


Sorry for describing tricks from that wiki article as "black magic", what I really meant is "hacks", which are now taken care of by pythonnet. I know that a lot of people put effort into getting it to work!

Feel free to open issues in pythonnet if you experience any issues with IPython.

Thomas Kluyver

unread,
Sep 15, 2016, 9:49:29 AM9/15/16
to Project Jupyter
On 15 September 2016 at 14:40, Denis Akhiyarov <denis.a...@gmail.com> wrote:

Thanks Denis :-)
 
Sorry for describing tricks from that wiki article as "black magic", what I really meant is "hacks", which are now taken care of by pythonnet. I know that a lot of people put effort into getting it to work!

I don't think that's a problem - black magic often takes a lot of effort ;-)

Denis Akhiyarov

unread,
Sep 15, 2016, 9:55:53 AM9/15/16
to Project Jupyter, Xavier Dupré
@Doug, it looks like metakernel-based kernels are full-featured kernels for languages other than CPython. But clrmagic is just a magic language cell to embed .NET (CLR) languages in CPython/IPython using pythonnet. Metakernel should definitely be helpful to icsharp that I linked before.

--
You received this message because you are subscribed to a topic in the Google Groups "Project Jupyter" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jupyter/Wy7LnDF4O_Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jupyter+unsubscribe@googlegroups.com.

To post to this group, send email to jup...@googlegroups.com.

Denis Akhiyarov

unread,
Sep 15, 2016, 10:00:28 AM9/15/16
to Project Jupyter
@MinRK this is definitely something that could be implemented in clrmagic. Looks like rpy2 R-magic for IPython is the most powerful magic.

--
You received this message because you are subscribed to a topic in the Google Groups "Project Jupyter" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jupyter/Wy7LnDF4O_Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jupyter+unsubscribe@googlegroups.com.

To post to this group, send email to jup...@googlegroups.com.

Doug Blank

unread,
Sep 15, 2016, 10:11:32 AM9/15/16
to jup...@googlegroups.com, Xavier Dupré
On Thu, Sep 15, 2016 at 9:55 AM, Denis Akhiyarov <denis.a...@gmail.com> wrote:
@Doug, it looks like metakernel-based kernels are full-featured kernels for languages other than CPython.

Yes, but those kernels often have the same issues in getting rich media back to the frontend. The two sections I pointed to look similar to how the R magic saves, reads, and wraps up images.
 
But clrmagic is just a magic language cell to embed .NET (CLR) languages in CPython/IPython using pythonnet. Metakernel should definitely be helpful to icsharp that I linked before.

Thanks to the links to this work! Our first project was to write a complete kernel in C# for a variety of languages (Boo, IronPython, IronRuby, Java (wrapped up via IKVM), FSharp, etc) [1, 2]. But with these latest tools, I think I would stay in Python for that.

-Doug

 

On Thu, Sep 15, 2016 at 8:32 AM, Doug Blank <doug....@gmail.com> wrote:
On Thu, Sep 15, 2016 at 9:20 AM, Denis Akhiyarov <denis.a...@gmail.com> wrote:
Can you provide any example of "graphical output" with any current language cell magics written for IPython kernel?

Depending on how your kernel is written, you might have to use the file system, like matlab or the octave kernels:



If your kernel can use the kernel wrapper, you might also want to look at basing your kernel on MetaKernel, to get additional, common magics, shell, etc:


-Doug
 

--
You received this message because you are subscribed to a topic in the Google Groups "Project Jupyter" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jupyter/Wy7LnDF4O_Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jupyter+unsubscribe@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/CAAusYChMFT2eQiG21F8t7Tu%2BxKVrFOpR%2BchMyL4PFvs9CTM4%3Dw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.

To post to this group, send email to jup...@googlegroups.com.

Lawrence D’Oliveiro

unread,
Sep 15, 2016, 5:52:59 PM9/15/16
to Project Jupyter, xavier...@gmail.com
On Friday, September 16, 2016 at 1:20:20 AM UTC+12, Denis Akhiyarov wrote:
 
On Wednesday, September 14, 2016 at 11:07:00 PM UTC-5, Lawrence D’Oliveiro wrote:
 
Have you thought of adding the option for graphical output? Or even Markdown or HTML output?
 
Can you provide any example of "graphical output" with any current language cell magics written for IPython kernel?

<https://github.com/ldo/ipy_magics> has magics for 2D PostScript graphics (using Ghostscript) and 3D RenderMan graphics (using Aqsis).
Reply all
Reply to author
Forward
0 new messages