Sorry, but I still don’t see the relation to the modal-browser issue.
Do you think because of endless dmap retriggering the object is not rendered in chrome/edge?
Oh sorry, I’m just using this thread to take notes for an eventual best practices guide; not as a reply to your question.
I would love to see how you create more complicated Holoviews objects from scratch and even more interested in how you set options. I have been struggling setting opts
such as line_color
or other more niche options while overlaying geofeatures and other plots (Say FilledContours
, WindBarbs
and coastline
). It gets even trickier if you try and change them later as now it is an Overlay
.
I have been struggling setting opts
such as line_color
or other more niche options while overlaying geofeatures and other plots
You can target specific elements: Overlay([hv_obj1, hv_obj2]).opts("Curve", line_color="red")
Collecting more best practices things here:
Generally, try to replace HoloViews usage with hvPlot. At a certain point of complexity, such as with the use of ‘.select’, it might be better to stick with HoloViews.
Almost always, try to replace the use of datashade
with rasterize
(read this page ). Essentially, rasterize
allows Bokeh to handle the colormapping instead of Datashader.
Remove all pn.interact
usage
Try to avoid .param.watch()
usage. This is pretty low-level and verbose approach Prefer using pn.bind()
. Read this page for explanation.
For apps built using a class approach, when they create a view()
method and call it directly, update the class by inheriting from pn.viewable.Viewer
and replace view()
by __panel__()
. Here is an example .
3 Likes
Revising:
set loading=True/False inside a try/finally block 6 so that widgets are disabled while loading, and upon completion/something goes wrong, the widgets are always reactivated.
Philipp mentioned we could simply do with param.update
import time
import panel as pn
pn.extension()
layout = pn.WidgetBox(pn.widgets.TextInput(), pn.widgets.Button(name="Click me!"))
layout
with layout.param.update(loading=True):
time.sleep(2)
layout[0].value = "Loaded!"
Using flexbox instead of column / row
2 Likes
Adding another to the list: toggling axiswise, framewise, shared_axes,
I’m doing some experiments in a notebook to work out some data flow for a larger application. I have the following code:
# cell1
import holoviews as hv
import numpy as np
import pandas as pd
import panel as pn
hv.extension('bokeh')
pn.extension()
# cell2
df = pd.DataFrame({k: np.random.default_rng().random(100) + i for i, k in enumerate('abcd')})
pipe = hv.streams.Pipe()
dm = hv.DynamicMap(hv.Dataset, streams=[pipe])
# cell3
pipe.send(df)
# cell4
def plot(elem, x_col, y_col):
range_x = …
Started migrating over here; let me know if you have feedback!
holoviz:main
← holoviz:add_best_practices
opened 08:25PM - 09 May 24 UTC
Starts migrating the collection from https://discourse.holoviz.org/t/personal-op… inions-about-best-practices-for-panel-holoviews/6789/27 to the official docs page, with examples
Thus far, I think it can be broken down into two sections: Dev Experience (or I was also thinking 'Nice to knows') and User Experience, and thus two separate notebooks.
Feedback appreciated on how to proceed (and what's missing; in particular, how to add it to the index).
1 Like
Widgets should not be used like param
opened 04:26AM - 14 Sep 23 UTC
type-feature
Param was designed to support nested Parameterized objects, where the value of a… Parameter is a Parameterized that has its own Parameters:
```python
import param, panel as pn
class P(param.Parameterized):
x = param.Integer(5)
y = param.ClassSelector(param.Parameterized, default=pn.widgets.IntSlider(value=7))
@param.depends("y.value", watch=True)
def fn(self):
self.x = self.y.value
p = P()
p.y.value=32
p.x
# 32
```
Here the value of `y` is a Parameterized that happens to be a [HoloViz Panel](https://panel.holoviz.org) widget that has a Parameter named `value`, and you can see that Param code can depend on that widget's Parameters.
However, several Panel core developers have noticed Panel users writing classes like:
```python
import param, panel as pn
class P(param.Parameterized):
x = param.Integer(5)
y = pn.widgets.IntSlider(value=7)
@param.depends("y.value", watch=True)
def fn(self):
self.x = self.y.value
p = P()
p.y.value=32
p.x
# 32
```
This code was not anticipated when writing Param but it happens to work in this case, because the dependency code does not actually check that `y` was defined as a Parameter before fetching the `value` parameter from that object. And the code makes sense at a semantic level, because both a `param.Integer` and a `pn.widgets.IntSlider` are objects that have an underlying integer value, so it would seem reasonable to be able to use a widget the same way one uses a Parameter.
However, because this case was never intended to be supported, a widget used as if it were a Parameter does not actually work like a Parameter in all cases. In particular, the Parameter `x` will be instantiated into an object, giving the object an independent copy of the value for `x`, while the widget `y` is a normal Python class attribute, and will thus not be instantiated. So things will seem to be working, but if the class P is ever instantiated more than once, `x` will be independent per object, and `y` will be shared across all objects, leading to untold subtle bugs.
A user can currently avoid this issue by moving the widget definition out of the class and into the constructor:
```python
import param, panel as pn
class P(param.Parameterized):
x = param.Integer(5)
def __init__(self, **params):
super().__init__(**params)
self.y = pn.widgets.IntSlider(value=7)
@param.depends("y.value", watch=True)
def fn(self):
self.x = self.y.value
p = P()
p.y.value=21
p.x
# 5 # Why is this not 32?
```
But that's a lot wordier, fails to declare the Parameter value in the class's manifest of Parameters, and in this case doesn't work (maybe because of https://github.com/holoviz/param/issues/829?).
Because people seem to fall into the trap of putting widgets at the class level quite a bit, I think we should do something about it. The options I can think of are:
1. Warn people not to do this! If we find a Parameterized as a class attribute, and certainly if we find one being depended on, warn that this attribute will not behave like a true Parameter and will not be instantiated per object, and point people to an implementation like the first one above.
2. If we find a Parameterized `q` as a class attribute of a Parameterized class, treat it as if there were a Parameter declared like `param.ClassSelector(param.Parameterized, default=q)`. We probably wouldn't actually create such a Parameter, but we'd instantiate a copy of the Parameterized into each object as if we had. I.e., just accept that people will do this, and treat it as legal. In practice I'd guess it's only Panel users who expect such usage to work, but the implementation would be for any Parameterized, and maybe there are other similar applications for this approach.
3. Under the theory that people are treating Panel Widgets and Parameters as interchangeable, one could imagine doing even more magic, so that if we find a Panel Widget (or e.g. use duck typing and find a Parameterized with a `value` Parameter), treat it as if they had actually declared that Parameter in this class (`y=param.Integer(7)` here, where `y` becomes a reference to the `value` parameter of this widget). It's hard to pin down how that could work, and it would be Panel-specific deep magic, so it would be hard to convince me that it's a good idea, but I wanted to list the idea concretely just so that we can be sure that what people seem to be assuming would work is really not something we should ever be supporting.
Personally, I vote against 3, and by default I'd vote for 1 because 2 doesn't solve a problem I have. But if 2 makes sense to other people and would support new users better, I don't think it would be difficult to implement, and I don't think it would affect code that's currently working properly. Any thoughts from others?
I don’t it’s good practice to define widgets like you would define params.
Instead, you can define params like this:
import panel as pn
import param
pn.extension()
class MyWidget(param.Parameterized):
button_event = param.Event()
value = param.Integer(default=0)
def __init__(self, **params):
super().__init__(**params)
self.button = pn.widgets.Button.from_param(self.param.button_event)
self.value_pane = pn.widgets.IntInput.from_param(self.param.value)
…
It is now live!
If you have additional opinions on what to be added, please create a PR to add it!
https://panel.holoviz.org/how_to/best_practices/index.html
1 Like