Panel/Hvplot interaction when variable is changing

2024/9/20 9:38:08

I'm trying to create a dashboard with two holoviews objects: a panel pn.widgets.Select object that contains a list of xarray variables, and a hvplot object that takes the selected variable on input, like this:

def hvmesh(var=None):mesh = ds[var].hvplot.quadmesh(x='x', y='y', rasterize=True, crs=crs, width=600, height=400, groupby=list(ds[var].dims[:-2]), cmap='jet')return mesh

Here's what an example mesh looks like for a particular variable (one that has both time and height dimensions): enter image description here

I would like to have the map update when I select a variable from the panel widget: enter image description here I tried to do this as a dynamic map, like this:

from holoviews.streams import Params
import holoviews as hvvar_stream = Params(var_select, ['value'], rename={'value': 'var'})mesh = hv.DynamicMap(hvmesh, streams=[var_stream])

but when I try to display the map, I get:

Exception: Nesting a DynamicMap inside a DynamicMap is not supported.

It would seem a common need to select the variable for hvplot from a panel widget. What is the best way to accomplish this with pyviz?

In case it's useful, here is my full attempt Jupyter Notebook.

Answer

Because the groupby changes with each variable selected, a list of variables can not be passed to hvplot. So one solution is to just recreate the plot each time a new variable is selected. This works:

import holoviews as hv
from holoviews.streams import Paramsdef plot(var=None, tiles=None):var = var or var_select.valuetiles = tiles or map_select.valuemesh = ds[var].hvplot.quadmesh(x='x', y='y', rasterize=True, crs=crs, title=var,width=600, height=400, groupby=list(ds[var].dims[:-2]), cmap='jet')return mesh.opts(alpha=0.7) * tilesdef on_var_select(event):var = event.obj.valuecol[-1] = plot(var=var)def on_map_select(event):tiles = event.obj.valuecol[-1] = plot(tiles=tiles)var_select.param.watch(on_var_select, parameter_names=['value']);
map_select.param.watch(on_map_select, parameter_names=['value']);col = pn.Column(var_select, map_select, plot(var_select.value) * tiles)

producing: enter image description here

Here is the full notebook.

https://en.xdnf.cn/q/72517.html

Related Q&A

asyncio matplotlib show() still freezes program

I wish to run a simulation while at the same time output its progress in a plot. Ive been looking through a lot of examples of threading and multiprocessing, but they are all pretty complex. So I thoug…

celery .delay hangs (recent, not an auth problem)

I am running Celery 2.2.4/djCelery 2.2.4, using RabbitMQ 2.1.1 as a backend. I recently brought online two new celery servers -- I had been running 2 workers across two machines with a total of ~18 thr…

Python ttk.combobox force post/open

I am trying to extend the ttk combobox class to allow autosuggestion. the code I have far works well, but I would like to get it to show the dropdown once some text has been entered without removing fo…

How to pull from the remote using dulwich?

How to do something like git pull in python dulwich library.

convert python programme to windows executable

i m trying to create windows executable from python program which has GUI . i m using following scriptfrom distutils.core import setup import py2exesetup(console=[gui.py]) it gives following errorWarni…

Must explicitly set engine if not passing in buffer or path for io in Panda

When running the following Python Panda code:xl = pd.ExcelFile(dataFileUrl)sheets = xl.sheet_namesdata = xl.parse(sheets[0])colheaders = list(data)I receive the ValueError:Must ex…

How to access the keys or values of Python GDB Value

I have a struct in GDB and want to run a script which examines this struct. In Python GDB you can easily access the struct via(gdb) python mystruct = gdb.parse_and_eval("mystruct")Now I got t…

How can I fire a Traits static event notification on a List?

I am working through the traits presentation from PyCon 2010. At about 2:30:45 the presenter starts covering trait event notifications, which allow (among other things) the ability to automatically ca…

Calculate moving average in numpy array with NaNs

I am trying to calculate the moving average in a large numpy array that contains NaNs. Currently I am using:import numpy as npdef moving_average(a,n=5):ret = np.cumsum(a,dtype=float)ret[n:] = ret[n:]-r…

Python: numpy.insert NaN value

Im trying to insert NaN values to specific indices of a numpy array. I keep getting this error:TypeError: Cannot cast array data from dtype(float64) to dtype(int64) according to the rule safeWhen tryin…