Share data between cells

3,058 views
Skip to first unread message

Aurélien Hébert

unread,
Apr 5, 2016, 4:08:38 AM4/5/16
to Project Jupyter
Hello,

  I wrote multiple cells in my Jupyter notebook, and I would like to share some variable between different cells. Is there an easy way to do it ? More specifically, I would like to know, if when cells magics commands are used (html or bash), is it possible to replace some content by python variable ?

Best regards,

Aurélien

Matthias Bussonnier

unread,
Apr 5, 2016, 1:59:50 PM4/5/16
to jup...@googlegroups.com
Hi Aurélien,

I'm not completely understand what you want to do. Wether you can
access variable in a cell magic depend on the cell magic itself.
For example the %%R cel magic use a persistent R process so you will
still have access to already defined variable.

The bash and HTML magics shipped with IPython do not allow to share
variable with Python, though you can defined your own magic to do so.
You should be able to figure out which can do that thanks to the
@needs_local_scope decorator present on each of them, that pass
local_ns to the magic function.

Does that answer your question ?

--
Matthias
> --
> 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.
> 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/64ce3eb8-fb2f-4fa5-a2ae-365403015fc2%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Aurélien Hébert

unread,
Apr 6, 2016, 8:26:25 AM4/6/16
to jup...@googlegroups.com
Hello,
thanks for your quick answer. Let's admit that I declare some variables in a python script, then to access them in my own magic cell, I have to use a @needs_local_scope decorator. Am I right ?
Thanks

---
Aurélien Hébert
Developer at Cityzen Data

You received this message because you are subscribed to a topic in the Google Groups "Project Jupyter" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jupyter/zWHgP-wnnMw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jupyter+u...@googlegroups.com.

To post to this group, send email to jup...@googlegroups.com.

Thomas Kluyver

unread,
Apr 6, 2016, 1:11:53 PM4/6/16
to Project Jupyter
On 6 April 2016 at 05:26, Aurélien Hébert <aurelie...@cityzendata.com> wrote:
thanks for your quick answer. Let's admit that I declare some variables in a python script, then to access them in my own magic cell, I have to use a @needs_local_scope decorator. Am I right ?

If the variables are in the global namespace when the magic runs, you don't need anything special. The code for the magic can access shell.user_ns to examine these.

The @needs_local_scope decorator is for the corner case where you want to call a magic inside a function, like this:

x = 2
def f():
    y = 3
    %my_magic

Without @needs_local_scope, my_magic can only get x. With it, it can get y as well.

This is only relevant for line magics, as you can't call a cell magic inside a function.

Thomas
Reply all
Reply to author
Forward
0 new messages