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

Simplify determining whether script is executed as application #4252

Merged
merged 3 commits into from
Jan 6, 2023

Conversation

philippjfr
Copy link
Member

@philippjfr philippjfr commented Jan 5, 2023

It is sometimes useful to write a Panel application in a script and also do some things that have side-effects such as setting pn.config options. If you also want this module to be importable without any side-effects you currently have to write something like this:

if __name__.startswith('bokeh_app_'):
    ...

This is ugly and weird since it looks like magic and it's a Panel app not a bokeh app. Therefore I think we should provide a helper that makes it easy to determine if we are in a panel serve execution context. Therefore I propose introducing pn.state.served:

if pn.state.served:
    ...

Ping @MarcSkovMadsen since you are the main person I've seen use this pattern. I don't love the in_app naming so am very eager to hear suggestions.

@philippjfr philippjfr requested review from hoxbro and maximlt January 5, 2023 17:53
@codecov
Copy link

codecov bot commented Jan 5, 2023

Codecov Report

Merging #4252 (6c919f1) into main (7419356) will decrease coverage by 0.00%.
The diff coverage is 79.16%.

@@            Coverage Diff             @@
##             main    #4252      +/-   ##
==========================================
- Coverage   82.70%   82.69%   -0.01%     
==========================================
  Files         237      237              
  Lines       34276    34283       +7     
==========================================
+ Hits        28347    28350       +3     
- Misses       5929     5933       +4     
Flag Coverage Δ
ui-tests 37.94% <79.16%> (-0.01%) ⬇️
unitexamples-tests 73.87% <79.16%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
panel/io/state.py 70.19% <58.33%> (+0.14%) ⬆️
panel/tests/pane/test_alert.py 65.62% <100.00%> (ø)
panel/tests/pane/test_echart.py 33.33% <100.00%> (ø)
...nel/tests/template/fast/test_fast_grid_template.py 52.72% <100.00%> (+0.87%) ⬆️
...nel/tests/template/fast/test_fast_list_template.py 79.31% <100.00%> (ø)
panel/tests/template/test_manual.py 61.61% <100.00%> (ø)
panel/tests/template/test_vanilla_manual.py 15.21% <100.00%> (ø)
panel/tests/widgets/test_speech_to_text.py 62.88% <100.00%> (ø)
panel/tests/widgets/test_terminal.py 58.87% <100.00%> (ø)
panel/tests/widgets/test_text_to_speech.py 77.50% <100.00%> (ø)
... and 4 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@hoxbro
Copy link
Member

hoxbro commented Jan 5, 2023

I also use it a lot and would prefer this method over it. 👍

For name suggestions: app_running, running_app, serving_app

@MarcSkovMadsen
Copy link
Collaborator

MarcSkovMadsen commented Jan 6, 2023

What about .served, .is_served or .in_served_context?

@philippjfr
Copy link
Member Author

Thanks @MarcSkovMadsen, decided pn.state.served was short and clear.

Copy link
Member

@maximlt maximlt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about .served, .is_served or .in_served_context?

I had in mind exactly these alternatives :) Happy with .served !

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.

4 participants