-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add layout and fix rendering using python script, not notebook env
- Loading branch information
1 parent
025650b
commit 90012c2
Showing
11 changed files
with
520 additions
and
69 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,74 @@ | ||
from __future__ import annotations | ||
from typing import TYPE_CHECKING | ||
from typing import TYPE_CHECKING, Literal | ||
if TYPE_CHECKING: | ||
from pfund_plot.types.core import tFigure | ||
from pfund_plot.types.literals import tDisplayMode | ||
|
||
import panel as pn | ||
|
||
from contextlib import contextmanager | ||
|
||
from panel.layout.gridstack import GridStack | ||
|
||
from pfund_plot.state import state | ||
from pfund_plot.renderer import render | ||
|
||
|
||
def layout_plot( | ||
__all__ = ['layout'] | ||
|
||
|
||
@contextmanager | ||
def layout( | ||
streaming: bool = False, | ||
display_mode: Literal['browser', 'desktop'] = 'browser', | ||
num_cols: int = 3, | ||
allow_drag: bool = True, | ||
# FIXME: plots can't be resized when using GridStack, not sure if it's a bottleneck or a bug | ||
# allow_resize: bool = True, | ||
): | ||
assert display_mode.lower() in ['browser', 'desktop'], "display_mode must be 'browser' or 'desktop'" | ||
|
||
# Setup state | ||
state.layout.in_layout = True | ||
state.layout.streaming = streaming | ||
state.layout.components = [] | ||
|
||
try: | ||
yield state.layout | ||
finally: | ||
components = state.layout.components | ||
return _layout_plot( | ||
*components, | ||
display_mode=display_mode, | ||
num_cols=num_cols, | ||
allow_drag=allow_drag, | ||
# allow_resize=allow_resize, | ||
) | ||
|
||
|
||
def _layout_plot( | ||
*figs: tFigure, | ||
display_mode: tDisplayMode = 'notebook', | ||
display_mode: Literal['browser', 'desktop'] = 'browser', | ||
num_cols: int = 3, | ||
allow_drag: bool = True, | ||
# allow_resize: bool = False, | ||
): | ||
# use gridstack to layout the plots automatically | ||
return render(combined_fig, display_mode) | ||
if not state.layout.in_layout: | ||
raise ValueError("layout_plot() must be called within a layout context manager") | ||
|
||
gstack = GridStack( | ||
sizing_mode='stretch_both', | ||
allow_drag=allow_drag, | ||
allow_resize=False, | ||
) | ||
|
||
# standardize the figures | ||
max_height = max(fig.height or 0 for fig in figs) | ||
for fig in figs: | ||
fig.param.update(height=max_height, width=None, sizing_mode='stretch_width') | ||
|
||
num_figs = len(figs) | ||
# num_rows = math.ceil(num_figs / num_cols) | ||
for i in range(num_figs): | ||
gstack[i // num_cols, i % num_cols] = figs[i] | ||
|
||
periodic_callbacks = state.layout.periodic_callbacks | ||
state.reset_layout() | ||
return render(gstack, display_mode, periodic_callbacks=periodic_callbacks) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.