2

I am currently following the Holoviews tutorial. Everything in there is done in notebooks, and makes use of the '%' syntax. I've never used notebooks (and don't particularly want to either). How can I write these lines in a normal .py file? Here's an example:

 %opts Graph [width=400 height=400]

I've tried a few things, such as:

graph=hv.Graph.opts(width=400, height=400)
graph=hv.Graph.opts(width=[400], height=[400])
hv.Graph(width=400, height=400)

These all throw errors. How does that % syntax actually work? How are there no commas in that list? I just want to set the width and height of my graph normally. Thanks!

Neil
  • 3,020
  • 4
  • 25
  • 48
  • 1
    Are you asking about this particular iPython magic method or in general? Because in general, you'll have to recreate the functionality in different ways. They abstract away all sorts of code for convenience. – roganjosh Jun 02 '18 at 11:38
  • Hm. Well this is my current concern but there are a few examples in this tutorial. Is there a general strategy? Here are some other examples: – Neil Jun 02 '18 at 11:40
  • %%output size=150 %%opts Curve [height=100 width=600 xaxis=None tools=['hover']] %%opts Curve (color='red' line_width=1.5) %%opts Spikes [height=100 width=600 yaxis=None] (color='grey' line_width=0.25) – Neil Jun 02 '18 at 11:40
  • I'm afraid I'm not familiar with `%opts` so I'm not sure I can help with this one. For the general strategy, you'll have to Google around to see what they actually do. For example `%timeit` can be replaced by the [`timeit`](https://docs.python.org/3/library/timeit.html) module, but using it is more involved. – roganjosh Jun 02 '18 at 11:45
  • I dont think opts is actually a 'built it' special method. From a quick search it looks like it's grabbing parameters and passing them to something. Opts is a method of Graph in the holoviews library – Neil Jun 02 '18 at 11:50
  • Yeah, which makes me doubly unqualified to answer because I don't use `holoviews` :) However, I do suggest you make the question title/content a bit more focussed on that specific functionality because I'm not sure a general solution exists. – roganjosh Jun 02 '18 at 11:52
  • That's a bummer. Okay, thanks anyhow. – Neil Jun 02 '18 at 11:56
  • `ipython` can be run as an interactive shell, an enhanced version of coding directly in a `python` session. Most of the notebook `magics` are available there. SO posts that show `In[]`,`Out[]` code segements are using `ipython`. – hpaulj Jun 02 '18 at 15:43

1 Answers1

1

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.

alexis
  • 48,685
  • 16
  • 101
  • 161