[IPython-User] Can I interpolate python variables into cell magics?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[IPython-User] Can I interpolate python variables into cell magics?

Patrick Surry-2
This seems like a stupid question, but if I'm using a cell magic like %%html or %%writefile, is there any way I can interpolate
data from notebook variables into the cell?  A kind of macro substitution if you will.

Thanks,
Patrick

_______________________________________________
IPython-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|

Re: Can I interpolate python variables into cell magics?

Matthias Bussonnier

Le 16 avr. 2014 à 09:52, Patrick Surry a écrit :

> This seems like a stupid question, but if I'm using a cell magic like %%html or %%writefile, is there any way I can interpolate
> data from notebook variables into the cell?  A kind of macro substitution if you will.

IIRC, this only work on the first line of the cell for these magics.

But I think you can write a magic that uses  @needs_local_scope and then access the local scope with
self._magic_locals then format the string by your self.

See IPtyhon/core/magics/execution.py

for example.

Does that make sense ?
--
M

>
> Thanks,
> Patrick
> _______________________________________________
> IPython-User mailing list
> [hidden email]
> http://mail.scipy.org/mailman/listinfo/ipython-user

_______________________________________________
IPython-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|

Re: Can I interpolate python variables into cell magics?

Thomas Kluyver-2
On 16 April 2014 07:42, Matthias BUSSONNIER <[hidden email]> wrote:
But I think you can write a magic that uses  @needs_local_scope and then access the local scope with
self._magic_locals then format the string by your self.

Small correction: If you use @needs_local_scope, the local namespace is passed into the magic as a keyword argument, local_ns. The main shell namespace is accessible as self.shell.user_ns.

_______________________________________________
IPython-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-user