<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 class="">%</span><span class="">install_ext</span> <span class="">https</span><span class="">:</span><span class="">//</span><span class="">raw</span><span class="">.</span><span class="">github</span><span class="">.</span><span class="">com</span><span class="">/</span><span class="">takluyver</span><span class="">/</span><span class="">cell2function</span><span class="">/</span><span class="">master</span><span class="">/</span><span class="">cell2function</span><span class="">.</span><span class="">py</span>
<br></pre><pre></pre>Github project: <a href="https://github.com/takluyver/cell2function">https://github.com/takluyver/cell2function</a><br></div>Example notebook: <a href="http://nbviewer.ipython.org/github/takluyver/cell2function/blob/master/Cell2function%20demo.ipynb">http://nbviewer.ipython.org/github/takluyver/cell2function/blob/master/Cell2function%20demo.ipynb</a><br>

<br></div>Thanks,<br>Thomas<br></div>