Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support custom vizzu backend #7114

Merged
merged 1 commit into from
Aug 12, 2024

Conversation

MarcSkovMadsen
Copy link
Collaborator

@MarcSkovMadsen MarcSkovMadsen commented Aug 9, 2024

Inspired by https://discourse.holoviz.org/t/how-to-create-a-custom-holoviews-plotting-backend/7555 I was trying to create a minimal Vizzu HoloViews backend.

This PR fixes issues in Panel discovered when trying to use a custom backend.

Example

I would like to end up with a working example. My current example is below. Next step is to figure out what to return in the generate_plot. The problem is that the fig is given to the Vizzu.object. Instead is should be split into two and given to Vizzu.object and Vizzu.config. How do to that?

import holoviews as hv
import panel as pn
import param

from holoviews.plotting.plot import GenericElementPlot, DimensionedPlot, CallbackPlot
from holoviews.plotting.renderer import Renderer

class VizzuRenderer(Renderer):

    backend = param.String(default='vizzu', doc="The backend name.")

    
    mode_formats = {'fig': ['html', ],
                    'holomap': ['widgets', 'scrubber', 'gif', 'auto']}
    
    _render_with_panel = True

    @classmethod
    def load_nb(cls, inline=True):
        """
        Loads the vizzu notebook resources.
        """
        import panel.models.vizzu # noqa
        cls._loaded = True
        if 'vizzu' not in getattr(pn.extension, '_loaded_extensions', ['vizzu']):
            pn.extension._loaded_extensions.append('vizzu')

    @classmethod
    def plot_options(cls, obj, percent_size):
        return {}

    
    def _figure_data(self, plot, fmt, as_script=False, **kwargs):
        return plot.handles['fig']


class VizzuPlot(DimensionedPlot, CallbackPlot):

    backend = 'vizzu'

    def initialize_plot(self, ranges=None, is_geo=False):
        return self.generate_plot(self.keys[-1], ranges, is_geo=is_geo)


    def update_frame(self, key, ranges=None, is_geo=False):
        return self.generate_plot(key, ranges, is_geo=is_geo)
    
    @property
    def state(self):
        """
        The plotting state that gets updated via the update method and
        used by the renderer to generate output.
        """
        return self.handles['fig']


class CurvePlot(VizzuPlot, GenericElementPlot):

    def generate_plot(self, key, ranges, element=None, is_geo=False):
        if element is None:
            element = self._get_frame(key)
        else:
            self.current_frame = element

        xdim, ydim = element.dimensions()[:2]
        
        import numpy as np
        data = {
            'Name': ['Alice', 'Bob', 'Ted', 'Patrick', 'Jason', 'Teresa', 'John'],
            'Weight': 50+np.random.randint(0, 10, 7)*10
        }
        config={'geometry': 'rectangle', 'x': 'Name', 'y': 'Weight', 'title': 'Weight by person'}
        
        
        self.handles['fig'] = fig = {
            "data": data,
            "config": config,
        }
        return fig
        

pn.pane.HoloViews.param.backend.objects.append('vizzu')
pn.pane.HoloViews._panes['vizzu'] = pn.pane.Vizzu

hv.Store.renderers['vizzu'] = VizzuRenderer.instance()
hv.Store.register({hv.Curve: CurvePlot}, backend='vizzu')

# Needs a fix in hv.util.__init__.py
# hv.extension._backends['vizzu'] = 'vizzu'
hv.extension('vizzu')
hv.Store.current_backend = 'vizzu'


# Need another fix in the HoloViews pane


import panel as pn

pn.extension("vizzu")
plot = hv.Curve([1, 2, 3])

pn.panel(plot).servable()

Copy link

codecov bot commented Aug 9, 2024

Codecov Report

Attention: Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Project coverage is 82.25%. Comparing base (9404b43) to head (341d603).

Files Patch % Lines
panel/pane/holoviews.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7114      +/-   ##
==========================================
- Coverage   82.25%   82.25%   -0.01%     
==========================================
  Files         327      327              
  Lines       48784    48785       +1     
==========================================
  Hits        40127    40127              
- Misses       8657     8658       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@philippjfr philippjfr marked this pull request as ready for review August 12, 2024 12:05
@philippjfr philippjfr merged commit 8799cda into main Aug 12, 2024
15 of 16 checks passed
@philippjfr philippjfr deleted the enhancement/support-custom-holoviews-backend branch August 12, 2024 12:05
@MarcSkovMadsen
Copy link
Collaborator Author

Do you have a hint. How should I update the above such that data can go to object parameter and config to config parameter of Vizzu Pane.

With the Vega example everything goes to the object parameter of the Vega pane.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants