<div dir="ltr">Thomas, many thanks! When the history of IPython is written, we'll have to have a chapter about Lucky House Thai ;)</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Feb 26, 2014 at 5:40 PM, Thomas Kluyver <span dir="ltr"><<a href="mailto:takowl@gmail.com" target="_blank">takowl@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>Several people have mentioned that it's awkward when you want to reuse exploratory code written in a notebook, first by moving it into a function for other cells in the same notebook, and then by moving it into a module to use from other notebooks and other modules.<br>



<br></div>At lunch today, we came up with the idea of a tool to transform a cell into a function, to address the first of those steps. Indenting and adding 'def foo()' by hand is easy enough, but in large cells it's easy to overlook a critical variable. So the tool scans the code in the cell, and creates function parameters for any variables referenced before they're defined, along with a return statement including any variables defined in the cell. That will usually be more than you want, but deleting code is easier than writing it.<br>



<br></div>Using the extension is as simple as putting a "%%cell2function" at the top of cells you want to turn into a function. Please give the extension a try and let me know whether it's useful in your workflow, and any annoyances you find using it.<br>



<br></div>Install command:<br><pre><span>%</span><span>install_ext</span> <span>https</span><span>:</span><span>//</span><span>raw</span><span>.</span><span>github</span><span>.</span><span>com</span><span>/</span><span>takluyver</span><span>/</span><span>cell2function</span><span>/</span><span>master</span><span>/</span><span>cell2function</span><span>.</span><span>py</span>
<br></pre><pre></pre>Github project: <a href="https://github.com/takluyver/cell2function" target="_blank">https://github.com/takluyver/cell2function</a><br></div>Example notebook: <a href="http://nbviewer.ipython.org/github/takluyver/cell2function/blob/master/Cell2function%20demo.ipynb" target="_blank">http://nbviewer.ipython.org/github/takluyver/cell2function/blob/master/Cell2function%20demo.ipynb</a><br>



<br></div>Thanks,<br>Thomas<br></div>
<br>_______________________________________________<br>
IPython-dev mailing list<br>
<a href="mailto:IPython-dev@scipy.org">IPython-dev@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/ipython-dev" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-dev</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br>Fernando Perez (@fperez_org; <a href="http://fperez.org" target="_blank">http://fperez.org</a>)<br>fperez.net-at-gmail: mailing lists only (I ignore this when swamped!)<br>

fernando.perez-at-berkeley: contact me here for any direct mail<br>
</div>