figpack 0.2.4__py3-none-any.whl → 0.2.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of figpack might be problematic. Click here for more details.

@@ -2,6 +2,9 @@
2
2
  Base view class for figpack visualization components
3
3
  """
4
4
 
5
+ import os
6
+ import random
7
+ import string
5
8
  from typing import Union
6
9
 
7
10
  import zarr
@@ -15,31 +18,38 @@ class FigpackView:
15
18
  def show(
16
19
  self,
17
20
  *,
21
+ title: str,
22
+ description: Union[str, None] = None,
18
23
  port: Union[int, None] = None,
19
- open_in_browser: bool = False,
20
- allow_origin: Union[str, None] = None,
24
+ open_in_browser: Union[bool, None] = None,
21
25
  upload: Union[bool, None] = None,
22
- ephemeral: Union[bool, None] = None,
23
- _dev: bool = False,
24
- title: Union[str, None] = None,
25
- description: Union[str, None] = None,
26
26
  inline: Union[bool, None] = None,
27
27
  inline_height: int = 600,
28
+ ephemeral: Union[bool, None] = None,
29
+ allow_origin: Union[str, None] = None,
30
+ wait_for_input: Union[bool, None] = None,
31
+ _dev: Union[bool, None] = None,
28
32
  ):
29
33
  """
30
- Display the visualization component
34
+ Display a figpack view component with intelligent environment detection and flexible display options.
35
+ See https://flatironinstitute.github.io/figpack/show_function.html for complete documentation.
36
+
37
+ Automatically adapts behavior based on context (Jupyter, Colab, JupyterHub, standalone).
38
+ Display modes include local browser, inline notebook, and remote upload with ephemeral options.
39
+ Environment variables (FIGPACK_UPLOAD, FIGPACK_INLINE, FIGPACK_OPEN_IN_BROWSER) can control default behaviors.
31
40
 
32
41
  Args:
33
- port: Port number for local server
34
- open_in_browser: Whether to open in browser automatically
35
- allow_origin: CORS allow origin header
36
- upload: Whether to upload the figure
37
- ephemeral: Whether to upload as ephemeral figure (None=auto-detect, True=force ephemeral, False=force regular)
38
- _dev: Development mode flag
39
- title: Title for the browser tab and figure
40
- description: Description text (markdown supported) for the figure
41
- inline: Whether to display inline in notebook (None=auto-detect, True=force inline, False=force browser)
42
- inline_height: Height in pixels for inline iframe display (default: 600)
42
+ title: Title for browser tab and figure (required)
43
+ description: Description text with markdown support (optional)
44
+ port: Local server port, random if None
45
+ open_in_browser: Auto-open in browser, auto-detects by environment
46
+ upload: Upload figure to figpack servers, auto-detects by environment
47
+ ephemeral: Use temporary figure for cloud notebooks, auto-detects
48
+ inline: Display inline in notebook, auto-detects by environment
49
+ inline_height: Height in pixels for inline display (default: 600)
50
+ allow_origin: CORS allow-origin header for local server
51
+ wait_for_input: Wait for Enter before continuing, auto-detects
52
+ _dev: Developer mode for figpack development
43
53
  """
44
54
  from ._show_view import (
45
55
  _show_view,
@@ -48,25 +58,55 @@ class FigpackView:
48
58
  _is_in_jupyterhub,
49
59
  )
50
60
 
51
- if ephemeral is None and upload is None:
52
- # If we haven't specified both, then let's check if we're in a notebook in a non-local environment
61
+ # determine upload
62
+ if upload is None:
63
+ upload = os.environ.get("FIGPACK_UPLOAD") == "1"
64
+ else:
65
+ if upload is True and ephemeral is True:
66
+ # ephemeral is reserved for the case where we don't specify upload
67
+ # and we are in a notebook in a remote environment such as
68
+ # colab or jupyterhub
69
+ raise ValueError("ephemeral cannot be set if upload is set")
70
+
71
+ # determine inline
72
+ if inline is None:
73
+ inline = _is_in_notebook() or os.environ.get("FIGPACK_INLINE") == "1"
74
+
75
+ # determine open_in_browser
76
+ if open_in_browser is None:
77
+ open_in_browser = os.environ.get("FIGPACK_OPEN_IN_BROWSER") == "1"
78
+
79
+ # determine ephemeral
80
+ if ephemeral is None:
81
+ ephemeral = False # default to False
53
82
  if _is_in_notebook():
54
83
  if _is_in_colab():
55
84
  # if we are in a notebook and in colab, we should show as uploaded ephemeral
56
85
  print("Detected Google Colab notebook environment.")
57
86
  upload = True
58
87
  ephemeral = True
59
- if _is_in_jupyterhub():
88
+ elif _is_in_jupyterhub():
60
89
  # if we are in a notebook and in jupyterhub, we should show as uploaded ephemeral
61
90
  print("Detected JupyterHub notebook environment.")
62
91
  upload = True
63
92
  ephemeral = True
64
93
 
94
+ # determine _dev
95
+ if _dev is None:
96
+ _dev = os.environ.get("FIGPACK_DEV") == "1"
97
+
98
+ # determine wait_for_input
99
+ if wait_for_input is None:
100
+ wait_for_input = not _is_in_notebook()
101
+
65
102
  # Validate ephemeral parameter
66
103
  if ephemeral and not upload:
67
104
  raise ValueError("ephemeral=True requires upload=True to be set")
68
105
 
69
106
  if _dev:
107
+ if open_in_browser:
108
+ print("** Note: In dev mode, open_in_browser is forced to False **")
109
+ open_in_browser = False
70
110
  if port is None:
71
111
  port = 3004
72
112
  if allow_origin is not None:
@@ -76,16 +116,14 @@ class FigpackView:
76
116
  raise ValueError("Cannot upload when _dev is True.")
77
117
 
78
118
  # make a random figure name
79
- import random
80
- import string
81
-
82
119
  _local_figure_name = "fig_" + "".join(
83
120
  random.choices(string.ascii_lowercase + string.digits, k=8)
84
121
  )
122
+ print("** Development mode **")
85
123
  print(
86
124
  f"For development, run figpack-gui in dev mode and use http://localhost:5173?data=http://localhost:{port}/{_local_figure_name}/data.zarr"
87
125
  )
88
- open_in_browser = False
126
+ print("")
89
127
 
90
128
  _show_view(
91
129
  self,
@@ -98,9 +136,20 @@ class FigpackView:
98
136
  description=description,
99
137
  inline=inline,
100
138
  inline_height=inline_height,
139
+ wait_for_input=wait_for_input,
101
140
  _local_figure_name=_local_figure_name if _dev else None,
102
141
  )
103
142
 
143
+ def save(self, output_path: str) -> None:
144
+ """
145
+ Save as figure either to a folder or to a .tar.gz file
146
+ Args:
147
+ output_path: Output path (destination folder or .tar.gz file path)
148
+ """
149
+ from ._save_figure import _save_figure
150
+
151
+ _save_figure(self, output_path)
152
+
104
153
  def _write_to_zarr_group(self, group: zarr.Group) -> None:
105
154
  """
106
155
  Write the view data to a Zarr group. Must be implemented by subclasses.