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

Proposal to add context managers to all Layouts + Templates #6712

Closed
ahuang11 opened this issue Apr 11, 2024 · 4 comments
Closed

Proposal to add context managers to all Layouts + Templates #6712

ahuang11 opened this issue Apr 11, 2024 · 4 comments
Labels
type: feature A major new feature

Comments

@ahuang11
Copy link
Contributor

ahuang11 commented Apr 11, 2024

Inspired by @MarcSkovMadsen's comment #6617 (comment)

Many times, I find myself doing something like:

widget_box = pn.WidgetBox(pn.widgets.TextInput())

def process(event):
    try:
        widget_box.loading = True
        ...
    finally:
        widget_box.loading = False

I'm wondering whether we can simplify it:

widget_box = pn.WidgetBox(pn.widgets.TextInput())

with widget_box:
    ...

For templates, it'd be the same:

template = pn.templates.FastListTemplate()
with template:  # disables all
    ...

I think this would work with all layouts, except maybe Swipe (which I think is more fitting to be a Pane).

Additionally, for a future pn.StatusCard (or currently pn.chat.ChatStep), I imagine it would also update the status param on finally.

@ahuang11 ahuang11 added TRIAGE Default label for untriaged issues type: feature A major new feature and removed TRIAGE Default label for untriaged issues labels Apr 11, 2024
@ahuang11
Copy link
Contributor Author

ahuang11 commented Apr 11, 2024

Maybe it can be useful for BooleanIndicators too.

processing = pn.indicators.BooleanStatus(value=False)
try:
    processing.value = True
    ...
finally:
    processing.value = False
with processing:
    ...

@ahuang11
Copy link
Contributor Author

ahuang11 commented Apr 11, 2024

Ah Philipp mentioned that we could simply use with param.update

import time
import panel as pn

pn.extension()

layout = pn.WidgetBox(pn.widgets.TextInput(), pn.widgets.Button(name="Click me!"))
layout

I think there's no need for this proposal then!

with layout.param.update(loading=True):
    time.sleep(2)
    layout[0].value = "Loaded!"

I think the only need is to document this in the Best Practices guide eventually.

@MarcSkovMadsen
Copy link
Collaborator

MarcSkovMadsen commented Apr 11, 2024

I have also seen context_managers pn.config.set and .set_values. Would be Nice to align on .update to reduce What you need to remember.

@maximlt
Copy link
Member

maximlt commented Apr 11, 2024

<obj>.param.update() has been added in Param 2.0 so it's quite recent. It can be used as a context manager or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature A major new feature
Projects
None yet
Development

No branches or pull requests

3 participants