<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <atom:link href="http://kitchingroup.cheme.cmu.edu/blog/feed/index.xml" rel="self" type="application/rss+xml" />
    <title>The Kitchin Research Group</title>
    <link>https://kitchingroup.cheme.cmu.edu/blog</link>
    <description>Chemical Engineering at Carnegie Mellon University</description>
    <pubDate>Sat, 01 Nov 2025 13:47:46 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    
    <item>
      <title>An improvement for figures in ipython + scimax</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2019/03/12/An-improvement-for-figures-in-ipython-+-scimax</link>
      <pubDate>Tue, 12 Mar 2019 14:18:26 EDT</pubDate>
      <category><![CDATA[ipython]]></category>
      <guid isPermaLink="false">7JS92IXaWxu9Qw0ujiw9pNqIeuk=</guid>
      <description>An improvement for figures in ipython + scimax</description>
      <content:encoded><![CDATA[


&lt;p&gt;
One of the best features of ipython in scimax is automatic inline images that you do not have to name. This has had a downside though, and that is it is not easy to use this &lt;i&gt;and&lt;/i&gt; put attributes like names (so you can reference them later) or captions, or if you want a specific filename to get that. No more. Now you can use the &lt;code&gt;:ipyfile&lt;/code&gt; header argument to control these. For example, if you use this in the header of the next block, it will save the images into the filenames you specified (in the order they are defined), and add attributes to the output. The syntax is just a list of plists (in elispese).
&lt;/p&gt;

&lt;pre class="example"&gt;
:ipyfile '((:name "clockwise" :filename "obipy-resources/clockwise.png" :caption "A clockwise spiral.") (:name "counterclockwise" :filename "obipy-resources/counterclockwise.png" :caption "A counterclockwise spiral."))
&lt;/pre&gt;

&lt;p&gt;
That allows you to refer to the clockwise one in Figure &lt;a href="#clockwise"&gt;clockwise&lt;/a&gt; and the counterclockwise in Fig.  &lt;a href="#counterclockwise"&gt;counterclockwise&lt;/a&gt;. That may be helpful when using Ipython to write papers or for presentations where you might prefer named figures that are easy to find. Enjoy!
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;%matplotlib inline
&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; matplotlib.pyplot &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; plt
&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; numpy &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; np

&lt;span style="color: #BA36A5;"&gt;t&lt;/span&gt; = np.linspace(0, 20 * np.pi, 350)
&lt;span style="color: #BA36A5;"&gt;x&lt;/span&gt; = np.exp(-0.1 * t) * np.sin(t)
&lt;span style="color: #BA36A5;"&gt;y&lt;/span&gt; = np.exp(-0.1 * t) * np.cos(t)

plt.plot(x, y)
plt.axis(&lt;span style="color: #008000;"&gt;'equal'&lt;/span&gt;)

plt.figure()
plt.plot(y, x)

plt.axis(&lt;span style="color: #008000;"&gt;'equal'&lt;/span&gt;)

&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(&lt;span style="color: #008000;"&gt;'Length of t = {}'&lt;/span&gt;.&lt;span style="color: #006FE0;"&gt;format&lt;/span&gt;(&lt;span style="color: #006FE0;"&gt;len&lt;/span&gt;(t)))
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(&lt;span style="color: #008000;"&gt;'x .dot. y = {}'&lt;/span&gt;.&lt;span style="color: #006FE0;"&gt;format&lt;/span&gt;(x @ y))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Length of t = 350
x .dot. y = 1.3598389888491538
&lt;/p&gt;



&lt;p&gt;
&lt;figure&gt;&lt;img src="/media/clockwise.png"&gt;&lt;figcaption&gt;A clockwise spiral.&lt;/figcaption&gt;&lt;/figure&gt; 
&lt;/p&gt;



&lt;p&gt;
&lt;figure&gt;&lt;img src="/media/counterclockwise.png"&gt;&lt;figcaption&gt;A counterclockwise spiral.&lt;/figcaption&gt;&lt;/figure&gt; 
&lt;/p&gt;
&lt;p&gt;Copyright (C) 2019 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2019/03/12/An-improvement-for-figures-in-ipython-+-scimax.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 9.2.1&lt;/p&gt;]]></content:encoded>
    </item>
    <item>
      <title>Org-mode and ipython enhancements in scimax</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2017/05/26/Org-mode-and-ipython-enhancements-in-scimax</link>
      <pubDate>Fri, 26 May 2017 16:54:16 EDT</pubDate>
      <category><![CDATA[orgmode]]></category>
      <category><![CDATA[emacs]]></category>
      <category><![CDATA[ipython]]></category>
      <guid isPermaLink="false">2U7yqcTW38o7Ws3S_FWYgzkOIag=</guid>
      <description>Org-mode and ipython enhancements in scimax</description>
      <content:encoded><![CDATA[


&lt;div id="table-of-contents"&gt;
&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;div id="text-table-of-contents"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#org696d7c2"&gt;1. Some convenience functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#orgef414e8"&gt;2. ob-ipython-inspect works&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#orgd62ef75"&gt;3. Getting selective output from Ipython&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#org5cac271"&gt;4. Where was that error?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#org3ebd0a8"&gt;5. Asynchronous Ipython&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
We have made some improvements to using Ipython in org-mode in the past including:
&lt;/p&gt;

&lt;ol class="org-ol"&gt;
&lt;li&gt;&lt;a href="http://kitchingroup.cheme.cmu.edu/blog/2017/01/29/ob-ipython-and-inline-figures-in-org-mode/"&gt;Inline figures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://kitchingroup.cheme.cmu.edu/blog/2017/01/21/Exporting-org-mode-to-Jupyter-notebooks/"&gt;Export to Jupyter notebooks&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
Today I will talk about a few new features and improvements I have introduced to scimax for using org-mode and Ipython together.
&lt;/p&gt;

&lt;p&gt;
The video for this post might be more obvious than the post:
&lt;/p&gt;
&lt;iframe width="560" height="315" src="https://www.youtube.com/embed/dMira3QsUdg" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

&lt;div id="outline-container-org696d7c2" class="outline-2"&gt;
&lt;h2 id="org696d7c2"&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; Some convenience functions&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;
&lt;p&gt;
There are a few nice shortcuts in the Jupyter notebook. Now we have some convenient commands in scimax to mimic those. My favorites are adding cells above or below the current cell. You can insert a new src block above the current one with (M-x &lt;code&gt;org-babel-insert-block&lt;/code&gt;). You can use a prefix arg to insert it below the current block.
&lt;/p&gt;


&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;&lt;span style="color: #8D8D84;"&gt;# &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;code&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;


&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;&lt;span style="color: #8D8D84;"&gt;# &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;below&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;&lt;span style="color: #8D8D84;"&gt;# &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;some code&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
I am particularly fond of splitting a large block into two smaller blocks. Use (M-x &lt;code&gt;org-babel-split-src-block&lt;/code&gt;) to do that and leave the point in the upper block. Use a prefix arg to leave the point in the lower block.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;&lt;span style="color: #8D8D84;"&gt;# &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;lots of code in large block&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;&lt;span style="color: #8D8D84;"&gt;# &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;Even more code&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;&lt;span style="color: #8D8D84;"&gt;# &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;The end of the long block&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
You can execute all the blocks up to the current point with (M-x &lt;code&gt;org-babel-execute-to-point&lt;/code&gt;).
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-orgef414e8" class="outline-2"&gt;
&lt;h2 id="orgef414e8"&gt;&lt;span class="section-number-2"&gt;2&lt;/span&gt; ob-ipython-inspect works&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;
&lt;p&gt;
In the original ob-ipython I found that ob-ipython-inspect did not work unless you were in special edit mode. That is too inconvenient. I modified a few functions to work directly from the org-buffer. I bind this to M-. in org-mode.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;%matplotlib inline
&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; numpy &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; np

&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; matplotlib.pyplot &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; plt

&lt;span style="color: #8D8D84;"&gt;# &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;Compute areas and colors&lt;/span&gt;
&lt;span style="color: #BA36A5;"&gt;N&lt;/span&gt; = 150
&lt;span style="color: #BA36A5;"&gt;r&lt;/span&gt; = 2 * np.random.rand(N)
&lt;span style="color: #BA36A5;"&gt;theta&lt;/span&gt; = 2 * np.pi * np.random.rand(N)
&lt;span style="color: #BA36A5;"&gt;area&lt;/span&gt; = 200 * r**2
&lt;span style="color: #BA36A5;"&gt;colors&lt;/span&gt; = theta

&lt;span style="color: #BA36A5;"&gt;ax&lt;/span&gt; = plt.subplot(111, projection=&lt;span style="color: #008000;"&gt;'polar'&lt;/span&gt;)
&lt;span style="color: #BA36A5;"&gt;c&lt;/span&gt; = ax.scatter(theta, r, c=colors, s=area, cmap=&lt;span style="color: #008000;"&gt;'hsv'&lt;/span&gt;, alpha=0.75)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;

&lt;/p&gt;

&lt;p&gt;
&amp;lt;matplotlib.figure.Figure at 0x114ded710&amp;gt;
&lt;img src="/media/ob-ipython-1758dfdd7a96829c50791c7cc9a39f3a.png"&gt; 
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;



&lt;div id="outline-container-orgd62ef75" class="outline-2"&gt;
&lt;h2 id="orgd62ef75"&gt;&lt;span class="section-number-2"&gt;3&lt;/span&gt; Getting selective output from Ipython&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-3"&gt;
&lt;p&gt;
Out of the box Ipython returns a lot of results. This block, for example returns a plain text, image and latex result as output.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;&lt;span style="color: #0000FF;"&gt;from&lt;/span&gt; sympy &lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; *
&lt;span style="color: #8D8D84;"&gt;# &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;commenting out init_printing() results in no output&lt;/span&gt;
init_printing()

var(&lt;span style="color: #008000;"&gt;'x y'&lt;/span&gt;)
x**2 + y
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;

&lt;/p&gt;

&lt;p&gt;
 2
x  + y
&lt;img src="/media/ob-ipython-da6fb3a34919a4f694cfaae45b6f0868.png"&gt; 
&lt;/p&gt;


&lt;p&gt;
We can select which one we want with a new header argument :ob-ipython-results. For this block you can give it the value of text/plain, text/latex or image/png.
&lt;/p&gt;


&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;var(&lt;span style="color: #008000;"&gt;'x y'&lt;/span&gt;)
x**2 + y
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
 2
x  + y
&lt;/p&gt;

&lt;p&gt;
Or to get the image:
&lt;/p&gt;


&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;var(&lt;span style="color: #008000;"&gt;'x y'&lt;/span&gt;)
x**2 + y
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
&lt;img src="/media/ob-ipython-da6fb3a34919a4f694cfaae45b6f0868.png"&gt; 
&lt;/p&gt;


&lt;p&gt;
This shows up with &lt;a href="https://emacs.stackexchange.com/questions/33005/python-org-mode-babel-output-column-headers-misaligned/33016#33016"&gt;pandas too&lt;/a&gt;. This block creates a table of data and then shows the first 5 rows. Ipython returns both plain text and html here.
&lt;/p&gt;


&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; pandas &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; pd
&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; numpy &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; np
&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; datetime &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; dt

&lt;span style="color: #0000FF;"&gt;def&lt;/span&gt; &lt;span style="color: #006699;"&gt;makeSim&lt;/span&gt;(nHosps, nPatients):
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt; = pd.DataFrame()
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'patientid'&lt;/span&gt;] = &lt;span style="color: #006FE0;"&gt;range&lt;/span&gt;(nPatients)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'hospid'&lt;/span&gt;] = np.random.randint(0, nHosps, nPatients)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'sex'&lt;/span&gt;] = np.random.randint(0, 2, nPatients)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'age'&lt;/span&gt;] = np.random.normal(65,18, nPatients)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'race'&lt;/span&gt;] = np.random.randint(0, 4, nPatients)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'cptCode'&lt;/span&gt;] = np.random.randint(1, 100, nPatients)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'rdm30d'&lt;/span&gt;] = np.random.uniform(0, 1, nPatients) &amp;lt; 0.1
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'mort30d'&lt;/span&gt;] = np.random.uniform(0, 1, nPatients) &amp;lt; 0.2
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'los'&lt;/span&gt;] = np.random.normal(8, 2, nPatients)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;return&lt;/span&gt; df

&lt;span style="color: #BA36A5;"&gt;discharges&lt;/span&gt; = makeSim(50, 10000)
discharges.head()
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;

&lt;/p&gt;

&lt;p&gt;
   patientid  hospid  sex        age  race  cptCode rdm30d mort30d        los
0          0      10    1  64.311947     0        8  False   False   8.036793
1          1       6    0  82.951484     1       73   True   False   7.996024
2          2      27    1  53.064501     3       95  False   False   9.015144
3          3      37    0  64.799128     0       93  False   False  10.099032
4          4      46    0  99.111394     2       25  False   False  11.711427
&lt;/p&gt;
&lt;div&gt;
&lt;table border="1" class="dataframe"&gt;
  &lt;thead&gt;
    &lt;tr style="text-align: right;"&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;patientid&lt;/th&gt;
      &lt;th&gt;hospid&lt;/th&gt;
      &lt;th&gt;sex&lt;/th&gt;
      &lt;th&gt;age&lt;/th&gt;
      &lt;th&gt;race&lt;/th&gt;
      &lt;th&gt;cptCode&lt;/th&gt;
      &lt;th&gt;rdm30d&lt;/th&gt;
      &lt;th&gt;mort30d&lt;/th&gt;
      &lt;th&gt;los&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;th&gt;0&lt;/th&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;10&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;64.311947&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;8&lt;/td&gt;
      &lt;td&gt;False&lt;/td&gt;
      &lt;td&gt;False&lt;/td&gt;
      &lt;td&gt;8.036793&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;1&lt;/th&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;6&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;82.951484&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;73&lt;/td&gt;
      &lt;td&gt;True&lt;/td&gt;
      &lt;td&gt;False&lt;/td&gt;
      &lt;td&gt;7.996024&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;2&lt;/th&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;27&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;53.064501&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;95&lt;/td&gt;
      &lt;td&gt;False&lt;/td&gt;
      &lt;td&gt;False&lt;/td&gt;
      &lt;td&gt;9.015144&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;3&lt;/th&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;37&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;64.799128&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;93&lt;/td&gt;
      &lt;td&gt;False&lt;/td&gt;
      &lt;td&gt;False&lt;/td&gt;
      &lt;td&gt;10.099032&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;4&lt;/th&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;46&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;99.111394&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;25&lt;/td&gt;
      &lt;td&gt;False&lt;/td&gt;
      &lt;td&gt;False&lt;/td&gt;
      &lt;td&gt;11.711427&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;


&lt;p&gt;
We can use the header to select only the plain text output!
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; pandas &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; pd
&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; numpy &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; np
&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; datetime &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; dt

&lt;span style="color: #0000FF;"&gt;def&lt;/span&gt; &lt;span style="color: #006699;"&gt;makeSim&lt;/span&gt;(nHosps, nPatients):
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt; = pd.DataFrame()
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'patientid'&lt;/span&gt;] = &lt;span style="color: #006FE0;"&gt;range&lt;/span&gt;(nPatients)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'hospid'&lt;/span&gt;] = np.random.randint(0, nHosps, nPatients)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'sex'&lt;/span&gt;] = np.random.randint(0, 2, nPatients)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'age'&lt;/span&gt;] = np.random.normal(65,18, nPatients)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'race'&lt;/span&gt;] = np.random.randint(0, 4, nPatients)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'cptCode'&lt;/span&gt;] = np.random.randint(1, 100, nPatients)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'rdm30d'&lt;/span&gt;] = np.random.uniform(0, 1, nPatients) &amp;lt; 0.1
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'mort30d'&lt;/span&gt;] = np.random.uniform(0, 1, nPatients) &amp;lt; 0.2
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;df&lt;/span&gt;[&lt;span style="color: #008000;"&gt;'los'&lt;/span&gt;] = np.random.normal(8, 2, nPatients)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;return&lt;/span&gt; df

&lt;span style="color: #BA36A5;"&gt;discharges&lt;/span&gt; = makeSim(50, 10000)
discharges.head()
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
   patientid  hospid  sex        age  race  cptCode rdm30d mort30d        los
0          0      21    0  73.633836     1       38  False   False   7.144019
1          1      16    1  67.518804     3       23  False   False   3.340534
2          2      15    0  44.139033     0        8  False   False   9.258706
3          3      29    1  45.510276     2        5  False   False  10.590245
4          4       7    0  52.974924     2        4  False    True   5.811064
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org5cac271" class="outline-2"&gt;
&lt;h2 id="org5cac271"&gt;&lt;span class="section-number-2"&gt;4&lt;/span&gt; Where was that error?&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-4"&gt;
&lt;p&gt;
A somewhat annoying feature of running cells in org-mode is when there is an exception there has not been a good way to jump to the line that caused the error to edit it. The lines in the src block are not numbered, so in a large block it can be tedious to find the line. In scimax, when you get an exception it will number the lines in the src block, and when you press q in the exception traceback buffer it will jump to the line in the block where the error occurred.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(1)
&lt;span style="color: #8D8D84;"&gt;#&lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;raise Exception('Here')&lt;/span&gt;
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(2)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
1
2
&lt;/p&gt;



&lt;p&gt;
If you don't like the numbers add this to your init file:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; ob-ipython-number-on-exception nil)
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org3ebd0a8" class="outline-2"&gt;
&lt;h2 id="org3ebd0a8"&gt;&lt;span class="section-number-2"&gt;5&lt;/span&gt; Asynchronous Ipython&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-5"&gt;
&lt;p&gt;
I have made a few improvements to the asynchronous workflow in Ipython. We now have a calculation queue, so you can use C-c C-c to execute several blocks in a row, and they will run asynchronously in the order you ran them. While they are running you can continue using Emacs, e.g. writing that paper, reading email, checking RSS feeds, tetris, &amp;#x2026; This also lets you run all the blocks up to the current point (M-x &lt;code&gt;org-babel-execute-ipython-buffer-to-point-async&lt;/code&gt;) or the whole buffer (of Ipython) blocks asynchronously (M-x &lt;code&gt;org-babel-execute-ipython-buffer-async&lt;/code&gt;).
&lt;/p&gt;

&lt;p&gt;
To turn this on by default put this in your init file:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; org-babel-async-ipython t)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
This requires all src blocks to have a name, and running the block will give it a name if you have not named the block. By default we use human-readable names. While the block is running, there will be a link indicating it is running. You can click on the link to cancel it. Running subsequent blocks will queue them to be run when the first block is done.
&lt;/p&gt;

&lt;p&gt;
Here is an example:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython" id="orgb3ddac3"&gt;&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; time
time.sleep(5)
&lt;span style="color: #BA36A5;"&gt;a&lt;/span&gt; = 5
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(&lt;span style="color: #008000;"&gt;'done'&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;


&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython" id="org5b7e30b"&gt;&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(3 * a)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
15
&lt;/p&gt;




&lt;p&gt;
Occasionally you will run into an issue. You can clear the queue with &lt;code&gt;org-babel-async-ipython-clear-queue&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2017 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2017/05/26/Org-mode-and-ipython-enhancements-in-scimax.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 9.0.5&lt;/p&gt;]]></content:encoded>
    </item>
    <item>
      <title>ob-ipython and inline figures in org-mode</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2017/01/29/ob-ipython-and-inline-figures-in-org-mode</link>
      <pubDate>Sun, 29 Jan 2017 16:05:22 EST</pubDate>
      <category><![CDATA[ipython]]></category>
      <category><![CDATA[python]]></category>
      <guid isPermaLink="false">pPhlgLpnvw7lbCFbahsNhbl5Nyk=</guid>
      <description>ob-ipython and inline figures in org-mode</description>
      <content:encoded><![CDATA[


&lt;div id="table-of-contents"&gt;
&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;div id="text-table-of-contents"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#org36f9eed"&gt;1. code for getting output and inline figures&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href="https://github.com/gregsexton/ob-ipython"&gt;ob-ipython&lt;/a&gt; provides some nice support for inline images, but it is a little limited. You can only have one inline plot, and you cannot capture the printed output. I often want both, and use more than one figure in a code block. So, here I look at a way to get that. 
&lt;/p&gt;

&lt;p&gt;
When ob-ipython executes a cell, it gets two things internally: the output and a list of result elements. The output is all the stuff that is printed, and the result contains result cells. So, we just have to check these for images, and append them to the output in an appropriate way.  I will do that using file links so that org automatically renders them. We will save the images as temp files, since they are regenerated each time you run the cell. 
&lt;/p&gt;

&lt;p&gt;
I want output and inline figures. This ipython block should output some text and two figures. Note we do not define file names anywhere! See &lt;a href="#org36f9eed"&gt;this section&lt;/a&gt; for details on how to get ob-ipython to do this.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;%matplotlib inline
&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; matplotlib.pyplot &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; plt
&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; numpy &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; np

&lt;span style="color: #BA36A5;"&gt;t&lt;/span&gt; = np.linspace(0, 20 * np.pi, 350)
&lt;span style="color: #BA36A5;"&gt;x&lt;/span&gt; = np.exp(-0.1 * t) * np.sin(t)
&lt;span style="color: #BA36A5;"&gt;y&lt;/span&gt; = np.exp(-0.1 * t) * np.cos(t)

plt.plot(x, y)
plt.axis(&lt;span style="color: #008000;"&gt;'equal'&lt;/span&gt;)

plt.figure()
plt.plot(y, x)
plt.axis(&lt;span style="color: #008000;"&gt;'equal'&lt;/span&gt;)

&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(&lt;span style="color: #008000;"&gt;'Length of t = {}'&lt;/span&gt;.&lt;span style="color: #006FE0;"&gt;format&lt;/span&gt;(&lt;span style="color: #006FE0;"&gt;len&lt;/span&gt;(t)))
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(&lt;span style="color: #008000;"&gt;'x .dot. y = {}'&lt;/span&gt;.&lt;span style="color: #006FE0;"&gt;format&lt;/span&gt;(x @ y))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Length of t = 350
x .dot. y = 1.3598389888491538
&lt;img src="/media/ob-ipython-86557tr2.png"&gt; 
&lt;img src="/media/ob-ipython-86557f1F.png"&gt; 
&lt;/p&gt;

&lt;p&gt;
Nice, success! Now my code blocks &lt;a href="http://kitchingroup.cheme.cmu.edu/blog/2017/01/21/Exporting-org-mode-to-Jupyter-notebooks/"&gt;export more cleanly to jupyter notebooks&lt;/a&gt;. Speaking of which, if you liked the post on that, there is a new library for it in scimax: &lt;a href="https://github.com/jkitchin/scimax/blob/master/ox-ipynb.el"&gt;https://github.com/jkitchin/scimax/blob/master/ox-ipynb.el&lt;/a&gt;. Yes, one day I will put it in its own repo, and probably put it up on MELPA. If it turns out to be useful over the next semester. 
&lt;/p&gt;



&lt;div id="outline-container-org36f9eed" class="outline-2"&gt;
&lt;h2 id="org36f9eed"&gt;&lt;a id="ID-44FC1FFF-A6EA-466E-B61C-85B22E58781D"&gt;&lt;/a&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; code for getting output and inline figures&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;
&lt;p&gt;
I wrote one new function that writes the base64 data out to a temporary file and returns a link to it. Then, I modified the org-babel-execute:ipython function to append these links onto the output. It seems like you need to use a header like this in your ob-ipython block, notably the results need to be in a drawer like this if you want org-mode to render the images. They do not show up in the results that have colons starting them.
&lt;/p&gt;

&lt;pre class="example"&gt;
#+BEGIN_SRC ipython :session :results output drawer
&lt;/pre&gt;

&lt;p&gt;
Here is the code.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;ob-ipython-inline-image&lt;/span&gt; (b64-string)
  &lt;span style="color: #036A07;"&gt;"Write the b64-string to a temporary file.&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;Returns an org-link to the file."&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((tfile (make-temp-file &lt;span style="color: #008000;"&gt;"ob-ipython-"&lt;/span&gt; nil &lt;span style="color: #008000;"&gt;".png"&lt;/span&gt;))
         (link (format &lt;span style="color: #008000;"&gt;"[[file:%s]]"&lt;/span&gt; tfile)))
    (ob-ipython--write-base64-string tfile b64-string)
    link))


(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;org-babel-execute:ipython&lt;/span&gt; (body params)
  &lt;span style="color: #036A07;"&gt;"Execute a block of IPython code with Babel.&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;This function is called by `&lt;/span&gt;&lt;span style="color: #D0372D;"&gt;org-babel-execute-src-block&lt;/span&gt;&lt;span style="color: #036A07;"&gt;'."&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((file (cdr (assoc &lt;span style="color: #006FE0;"&gt;:file&lt;/span&gt; params)))
         (session (cdr (assoc &lt;span style="color: #006FE0;"&gt;:session&lt;/span&gt; params)))
         (result-type (cdr (assoc &lt;span style="color: #006FE0;"&gt;:result-type&lt;/span&gt; params))))
    (org-babel-ipython-initiate-session session params)
    (&lt;span style="color: #0000FF;"&gt;-when-let&lt;/span&gt; (ret (ob-ipython--eval
                     (ob-ipython--execute-request
                      (org-babel-expand-body:generic (encode-coding-string body 'utf-8)
                                                     params (org-babel-variable-assignments:python params))
                      (ob-ipython--normalize-session session))))
      (&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((result (cdr (assoc &lt;span style="color: #006FE0;"&gt;:result&lt;/span&gt; ret)))
            (output (cdr (assoc &lt;span style="color: #006FE0;"&gt;:output&lt;/span&gt; ret))))
        (&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt; (eq result-type 'output)
            (concat
             output 
             (format &lt;span style="color: #008000;"&gt;"%s"&lt;/span&gt;
                     (mapconcat 'identity
                                (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for res in result
                                      if (eq 'image/png (car res))
                                      collect (ob-ipython-inline-image (cdr res)))
                                &lt;span style="color: #008000;"&gt;"\n"&lt;/span&gt;)))
          (ob-ipython--create-stdout-buffer output)
          (&lt;span style="color: #0000FF;"&gt;cond&lt;/span&gt; ((&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; file (string= (f-ext file) &lt;span style="color: #008000;"&gt;"png"&lt;/span&gt;))
                 (&lt;span style="color: #0000FF;"&gt;-&amp;gt;&amp;gt;&lt;/span&gt; result (assoc 'image/png) cdr (ob-ipython--write-base64-string file)))
                ((&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; file (string= (f-ext file) &lt;span style="color: #008000;"&gt;"svg"&lt;/span&gt;))
                 (&lt;span style="color: #0000FF;"&gt;-&amp;gt;&amp;gt;&lt;/span&gt; result (assoc 'image/svg+xml) cdr (ob-ipython--write-string-to-file file)))
                (file (&lt;span style="color: #ff0000; font-weight: bold;"&gt;error&lt;/span&gt; &lt;span style="color: #008000;"&gt;"%s is currently an unsupported file extension."&lt;/span&gt; (f-ext file)))
                (t (&lt;span style="color: #0000FF;"&gt;-&amp;gt;&amp;gt;&lt;/span&gt; result (assoc 'text/plain) cdr))))))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
org-babel-execute:ipython
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2017 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2017/01/29/ob-ipython-and-inline-figures-in-org-mode.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 9.0.3&lt;/p&gt;]]></content:encoded>
    </item>
  </channel>
</rss>
