# Cellmagic that converts code cell into a function

**URL:** <https://discourse.jupyter.org/t/cellmagic-that-converts-code-cell-into-a-function/17554>\
**Category:** Show and Tell\
**Created:** [January 16, 2023, 9:03am UTC](https://discourse.jupyter.org/t/cellmagic-that-converts-code-cell-into-a-function/17554 "2023-01-16T09:03:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kolibril13](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/kolibril13/32/8343_2.png) [@kolibril13](https://discourse.jupyter.org/u/kolibril13)\
**Post date:** [January 16, 2023, 9:03am UTC](https://discourse.jupyter.org/t/cellmagic-that-converts-code-cell-into-a-function/17554/1 "2023-01-16T09:03:45Z")

</div>

I had the idea for a cellmagic that converts a code cell into a function, it’s just a hacky proof of concept and I don’t know if this is actually useful.  
As this new syntax allows to quickly create and remove functions from Jupyter cells without the need of code indentation, this could e.g. be used to accelerate the workflow of developing new functions.

Here is how it works:

```python
%%magic_func foo
x = 34
x + 10 # RETURN

```

```auto
#Out:
44

```

Note that the comment `# RETURN` is considered by the cellmagic.  
The cellmagic will rewrite the whole code block as a function that is called `foo()` and returns `x + 10`.  
In another cell, the function `foo` that can now be called like this:

```python
foo()

```

```auto
#Out
44

```

```auto
from IPython import get_ipython
from IPython.core.magic import Magics, cell_magic, magics_class
from io import StringIO

@magics_class
class MyMagic(Magics):
    @cell_magic
    def magic_func(self, line, cell):
        self.shell.run_cell(cell)
        new_codeblock = ""
        new_codeblock += f"def {line}():"
        new_codeblock += "\n"
        for codeline in StringIO(cell):
            new_codeblock += "\t"
            if "# RETURN" in codeline:
                new_codeblock += "return "
            new_codeblock += codeline

        self.shell.run_cell(new_codeblock)

ipy = get_ipython()
ipy.register_magics(MyMagic)

```

---

<div class="post-metadata">

**Author:** ![lysakowski](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/lysakowski/32/984_2.png) [@lysakowski](https://discourse.jupyter.org/u/lysakowski)\
**Post date:** [January 20, 2023, 5:34am UTC](https://discourse.jupyter.org/t/cellmagic-that-converts-code-cell-into-a-function/17554/2 "2023-01-20T05:34:29Z")

</div>

This is a great idea for students and lazy programmers who don’t feel like formalizing their statements into a formal function.

Can this be added to the core Jupyter Magics library? Pardon my ignorance at the moment, but is there a “Jupyter Magics” package for this to find home? What is the process for accepting such enhancements into core package?

---

<div class="post-metadata">

**Author:** ![kolibril13](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/kolibril13/32/8343_2.png) [@kolibril13](https://discourse.jupyter.org/u/kolibril13)\
**Post date:** [January 20, 2023, 7:09am UTC](https://discourse.jupyter.org/t/cellmagic-that-converts-code-cell-into-a-function/17554/3 "2023-01-20T07:09:04Z")

</div>

Thanks for your interest!

There are cell magics in the Ipython core library ([built-in-magic-commands docs](https://ipython.readthedocs.io/en/stable/interactive/magics.html#built-in-magic-commands)) but I think for `%%magic_func` magic this is not the right place.  
In its current state, `%%magic_func` is quite hacky, and the defined functions are not understood by an ide (you won’t get auto-complete and there might be the warning “Function not defined” from the editor.)  
Further, it’s not possible to phrase function arguments. That would be easy to implement:

- Idea one would be to use the magic argument `@magic_arguments.argument` that can be used like `%%magic_func --arg1 "x=3"`
- Idea two: similar to the `x + 10 # RETURN` syntax, function arguments could be annotated with `ARGUMENT`, e.g.

```auto
%%magic_func foo
x = 34 # ARGUMENT
x + 10 # RETURN

```

This could become a separate pypi project, e.g. `community-cellmagic` , similar to the way I’ve written [jupyter\_capture\_output](https://github.com/Octoframes/jupyter_capture_output).

Some other cell magics that I have in mind and that could be part of that package:

A cell magic that runs certain lines multiple times:

```auto
%%linemultiplyer
x = 34
x + 10 # RUN2X
x * 2 # RUN10X

```

A cell magic that streams the code together with its output to communication platforms like discord or the Jupyter discourse:

```auto
%%cellstream
import matplotlib.pyplot as plt
plt.plot([1,2], [10,20], c = "orange")

```
