Tl;dr: If your real interest is to use holoviews
outside the jupyter notebook environment (like the OP does), see the section Outside the notebook in the holoviews documentation. Briefly, the magic %%opts Graph [width=400 height=400]
corresponds to calling the method g.options(width=400, height=400)
on some Graph
object g
.
If you are reading this because you are curious about jupyter
's % "magics", read on.
The "%" commands that you can use in notebooks are the so-called ipython "magics". You'll find a list of the standard ones here. So they are not available in ordinary Python, but ipython understands the %
syntax and translates it to calls to the particular library. So the simplest way to get access to them, and with the same %
syntax to boot, is to run ipython
instead of python
.
You also ask how the % syntax works. A third-party package can "register" some of its functions as custom ipython magics by using a simple decorator:
@line_magic
def lmagic(self, line):
"my line magic"
print("Full access to the main IPython object:", self.shell)
print("Variables in the user namespace:", list(self.shell.user_ns.keys()))
return line
See more in the documentation on defining custom magics.
Finally, you are of course more interested in going from the hv
magics to the corresponding Python code. It seems that the %%opts
magic that's been stumping you is a method of this class: holoviews.ipython.magics.OptsMagic
. I don't use holoviews, but it seems that something like this might be equivalent to the magic in your question:
from holoviews.ipython.magics import OptsMagic
om = OptsMagic()
om.opts("Graph [width=400 height=400]")
Note that you may also need to select a graphics backend, e.g. with the backend
keyword option.