holoviz / panel

Panel: The powerful data exploration & web app framework for Python
https://panel.holoviz.org
BSD 3-Clause "New" or "Revised" License
4.78k stars 518 forks source link

Enable combining pn.serve a function with .servable #3680

Closed MarcSkovMadsen closed 2 years ago

MarcSkovMadsen commented 2 years ago

Background

I'm trying to contribute a PanelFrontend to lightning.ai in https://github.com/Lightning-AI/lightning/pull/13531.

The feedback is that they would like an api where the PanelFrontend is instantiated with a render_fn function that runs the Panel code. The main argument is that this is consistent with the StreamlitFrontend.

Personally I think this is problematic because 1) Most panel users are familiar with panel serve of a script or notebook. pn.serve of a function works a bit differently because 2) You can't use .servable with pn.serve and a function. But my impression is that starting with an api similar to what they already have is easier for them to understand and approve.

Example

import panel as pn

def view_1():
    text_input = pn.widgets.TextInput(name="Input", value="abcd")

    return pn.Column(
        "# Input",
        text_input,
    )

def view_2():
    pn.panel("# Input").servable()
    pn.widgets.TextInput(name="Input", value="abcd").servable()
    return pn.Column() # Hack: Without it the server will never show

pn.serve(
    {"view1": view_1, "view2": view_2}
)

Request

My request is to support the combination of pn.serve of a function using .servable as defined in view_2.

Extra Motivation

The .servable api is simpler and similar to the Streamlit api. Its easier to work with for a lot of users.

Discussion

I think it would be possible to wrap the view_x functions in some other function that returns the result of view_1 because it is not none and does something (what) to collect the .servable marked items of view_2 and return them in a template. BUT HOW?

MarcSkovMadsen commented 2 years ago

I guess the main question for me is how do I meet the _curdoc().session_context criterion in the servable function? What should I do to mimic what panel serve does?

image

MarcSkovMadsen commented 2 years ago

I have added support for serving script with lightning which makes this issue less important.

MarcSkovMadsen commented 2 years ago

I've worked around this issue by using panel serve as outlined here https://github.com/holoviz/panel/issues/3681