How to get the matplotlib inline output via jupyter_client

135 views
Skip to first unread message

Jeff Zhang

unread,
Jul 9, 2017, 8:37:35 AM7/9/17
to Project Jupyter

I try to use jupyter_client to get the matplotlib output and display it in my app. Here's what I did. But io.stdout is just a string. How can I get the actual image data of matplotlib. Thanks


In [6]: with capture_output() as io:
   ...:     reply = kc.execute_interactive("import matplotlib.pyplot as plt\n%matplotlib inline\ndata=[1,1,2,3,4]\nplt.figure()\nplt.plot(data)", timeout=TI
   ...: MEOUT)
   ...:
   ...:

In [8]: io.stdout
Out[8]: '[<matplotlib.lines.Line2D at 0x1086a8d30>]<matplotlib.figure.Figure at 0x106f220f0>'

Thomas Kluyver

unread,
Jul 10, 2017, 3:56:27 AM7/10/17
to Project Jupyter
Hi Jeff,

I think the output_hook parameter to execute_interactive() is what you want. Here's the docs:
http://jupyter-client.readthedocs.io/en/latest/api/client.html#jupyter_client.BlockingKernelClient.execute_interactive

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/3332cdec-b938-495e-821a-86132ac31749%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeff Zhang

unread,
Jul 10, 2017, 10:17:13 AM7/10/17
to Project Jupyter

Thanks Thomas, is there any sample code that I can refer ?


在 2017年7月10日星期一 UTC+8下午3:56:27,takowl写道:
Hi Jeff,

I think the output_hook parameter to execute_interactive() is what you want. Here's the docs:
http://jupyter-client.readthedocs.io/en/latest/api/client.html#jupyter_client.BlockingKernelClient.execute_interactive

Thomas
On 9 July 2017 at 13:37, Jeff Zhang <zjf...@gmail.com> wrote:

I try to use jupyter_client to get the matplotlib output and display it in my app. Here's what I did. But io.stdout is just a string. How can I get the actual image data of matplotlib. Thanks


In [6]: with capture_output() as io:
   ...:     reply = kc.execute_interactive("import matplotlib.pyplot as plt\n%matplotlib inline\ndata=[1,1,2,3,4]\nplt.figure()\nplt.plot(data)", timeout=TI
   ...: MEOUT)
   ...:
   ...:

In [8]: io.stdout
Out[8]: '[<matplotlib.lines.Line2D at 0x1086a8d30>]<matplotlib.figure.Figure at 0x106f220f0>'

--
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+u...@googlegroups.com.

Jeff Zhang

unread,
Jul 10, 2017, 10:19:11 AM7/10/17
to Project Jupyter

I try to find how jupyter display inline image of matplotlib, but could not find where jupyter use output_hook


在 2017年7月10日星期一 UTC+8下午10:17:13,Jeff Zhang写道:

Thomas Kluyver

unread,
Jul 13, 2017, 3:32:22 AM7/13/17
to Project Jupyter
It may not do exactly what you want, but the main example of programatically executing code through Jupyter is the execution preprocessor for nbconvert:

https://github.com/jupyter/nbconvert/blob/master/nbconvert/preprocessors/execute.py

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.

Roland Weber

unread,
Jul 17, 2017, 3:32:27 AM7/17/17
to Project Jupyter
Hi Jeff,

stdout will give you only text printed to standard output. Cell execution also yields an execution result, which can come in different mime-types. Have a look at that.

For example, execute the following in a Python notebook to see the difference:

    print ('standard output')
    'execution result'

hope that helps,
  Roland
Reply all
Reply to author
Forward
0 new messages