maidr 1.4.10__py3-none-any.whl → 1.6.0__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.
maidr/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
- __version__ = "1.4.10"
1
+ __version__ = "1.6.0"
2
2
 
3
3
  from .api import close, render, save_html, show, stacked
4
4
  from .core import Maidr
maidr/core/maidr.py CHANGED
@@ -7,6 +7,8 @@ import os
7
7
  import tempfile
8
8
  import uuid
9
9
  import webbrowser
10
+ import subprocess
11
+ from pathlib import Path
10
12
  from typing import Any, Literal, cast
11
13
 
12
14
  import matplotlib.pyplot as plt
@@ -137,7 +139,48 @@ class Maidr:
137
139
  html_file_path = self.save_html(
138
140
  temp_file_path
139
141
  ) # This will use use_iframe=False
140
- webbrowser.open(f"file://{html_file_path}")
142
+ if Environment.is_wsl():
143
+ wsl_distro_name = Environment.get_wsl_distro_name()
144
+
145
+ # Validate that WSL distro name is available
146
+ if not wsl_distro_name:
147
+ raise ValueError(
148
+ "WSL_DISTRO_NAME environment variable is not set or is empty. "
149
+ "Cannot construct WSL file URL. Please ensure you are running in a WSL environment."
150
+ )
151
+
152
+ # Ensure html_file_path is an absolute POSIX path for proper WSL URL construction
153
+ html_file_path = Path(html_file_path).resolve().as_posix()
154
+
155
+ url = f"file://wsl$/{wsl_distro_name}{html_file_path}"
156
+
157
+ # Try to open the file in Windows using explorer.exe with robust path detection
158
+ explorer_path = Environment.find_explorer_path()
159
+ if explorer_path:
160
+ try:
161
+ result = subprocess.run(
162
+ [explorer_path, url],
163
+ capture_output=True,
164
+ text=True,
165
+ timeout=10
166
+ )
167
+
168
+ if result.returncode == 0:
169
+ return
170
+ else:
171
+ # Fall back to webbrowser.open() if explorer.exe fails
172
+ webbrowser.open(url)
173
+
174
+ except subprocess.TimeoutExpired:
175
+ webbrowser.open(url)
176
+ except Exception:
177
+ # Fall back to webbrowser.open() if explorer.exe fails
178
+ webbrowser.open(url)
179
+ else:
180
+ webbrowser.open(url)
181
+
182
+ else:
183
+ webbrowser.open(f"file://{html_file_path}")
141
184
 
142
185
  def _create_html_tag(self, use_iframe: bool = True) -> Tag:
143
186
  """Create the MAIDR HTML using HTML tags."""
@@ -311,7 +354,7 @@ class Maidr:
311
354
  # Render the plot inside an iframe if in a Jupyter notebook, Google Colab
312
355
  # or VSCode notebook. No need for iframe if this is a Quarto document.
313
356
  # For TypeScript we will use iframe by default for now
314
- if use_iframe and (Environment.is_notebook() or Environment.is_shiny()):
357
+ if use_iframe and (Environment.is_flask() or Environment.is_notebook() or Environment.is_shiny()):
315
358
  unique_id = "iframe_" + Maidr._unique_id()
316
359
 
317
360
  def generate_iframe_script(unique_id: str) -> str:
maidr/util/environment.py CHANGED
@@ -1,11 +1,51 @@
1
1
  import json
2
2
  import os
3
+ import subprocess
3
4
  import sys
5
+ from typing import Union
4
6
 
5
7
 
6
8
  class Environment:
7
9
  _engine = "ts"
8
10
 
11
+ @staticmethod
12
+ def is_flask() -> bool:
13
+ """
14
+ Check if the current environment is a Flask application.
15
+
16
+ This method detects Flask applications by checking if Flask's app context
17
+ is available using `flask.has_app_context()`. The app context is Flask's
18
+ way of tracking the current application state and is only available when
19
+ code is running within a Flask application.
20
+
21
+ Returns
22
+ -------
23
+ bool
24
+ True if the environment is a Flask application, False otherwise.
25
+
26
+ Examples
27
+ --------
28
+ >>> from maidr.util.environment import Environment
29
+ >>> Environment.is_flask()
30
+ False # When not in a Flask app
31
+ """
32
+ try:
33
+ # Import Flask's has_app_context function
34
+ from flask import has_app_context
35
+
36
+ # has_app_context() returns True only when code is running within
37
+ # a Flask application context. This is Flask's built-in mechanism
38
+ # for detecting if the current execution environment is a Flask app.
39
+ #
40
+ # The app context is automatically created by Flask when:
41
+ # - A Flask app is running (app.run())
42
+ # - Code is executed within a Flask request context
43
+ # - The app context is manually pushed
44
+ return has_app_context()
45
+ except ImportError:
46
+ # Flask is not installed, so we're definitely not in a Flask app
47
+ return False
48
+
9
49
  @staticmethod
10
50
  def is_interactive_shell() -> bool:
11
51
  """Return True if the environment is an interactive shell."""
@@ -61,6 +101,93 @@ class Environment:
61
101
  return False
62
102
  except ImportError:
63
103
  return False
104
+ @staticmethod
105
+ def is_wsl() -> bool:
106
+ """
107
+ Check if the current environment is WSL (Windows Subsystem for Linux).
108
+
109
+ This method detects WSL environments by reading the `/proc/version` file
110
+ and checking for 'microsoft' or 'wsl' keywords in the version string.
111
+ WSL environments typically contain these identifiers in their kernel version.
112
+
113
+ Returns
114
+ -------
115
+ bool
116
+ True if the environment is WSL, False otherwise.
117
+
118
+ Examples
119
+ --------
120
+ >>> from maidr.util.environment import Environment
121
+ >>> Environment.is_wsl()
122
+ False # When not in WSL
123
+ """
124
+ try:
125
+ with open('/proc/version', 'r') as f:
126
+ version_info = f.read().lower()
127
+ if 'microsoft' in version_info or 'wsl' in version_info:
128
+ return True
129
+ except FileNotFoundError:
130
+ pass
131
+ return False
132
+
133
+ @staticmethod
134
+ def get_wsl_distro_name() -> str:
135
+ """
136
+ Get the WSL distribution name from environment variables.
137
+
138
+ This method retrieves the WSL distribution name from the `WSL_DISTRO_NAME`
139
+ environment variable, which is automatically set by WSL when running
140
+ in a WSL environment.
141
+
142
+ Returns
143
+ -------
144
+ str
145
+ The WSL distribution name (e.g., 'Ubuntu-20.04', 'Debian') if set,
146
+ otherwise an empty string.
147
+
148
+ Examples
149
+ --------
150
+ >>> from maidr.util.environment import Environment
151
+ >>> Environment.get_wsl_distro_name()
152
+ '' # When not in WSL or WSL_DISTRO_NAME not set
153
+ """
154
+ return os.environ.get('WSL_DISTRO_NAME', '')
155
+
156
+ @staticmethod
157
+ def find_explorer_path() -> Union[str, None]:
158
+ """
159
+ Find the correct path to explorer.exe in WSL environment.
160
+
161
+ This method checks if explorer.exe is available in the PATH
162
+ and returns the path if found.
163
+
164
+ Returns
165
+ -------
166
+ str | None
167
+ The path to explorer.exe if found, None otherwise.
168
+
169
+ Examples
170
+ --------
171
+ >>> from maidr.util.environment import Environment
172
+ >>> Environment.find_explorer_path()
173
+ '/mnt/c/Windows/explorer.exe' # When found
174
+ """
175
+ # Check if explorer.exe is in PATH
176
+ try:
177
+ result = subprocess.run(
178
+ ['which', 'explorer.exe'],
179
+ capture_output=True,
180
+ text=True,
181
+ timeout=5
182
+ )
183
+ if result.returncode == 0:
184
+ return result.stdout.strip()
185
+ except Exception:
186
+ pass
187
+
188
+ return None
189
+
190
+
64
191
 
65
192
  @staticmethod
66
193
  def get_renderer() -> str:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: maidr
3
- Version: 1.4.10
3
+ Version: 1.6.0
4
4
  Summary: Multimodal Access and Interactive Data Representations
5
5
  Project-URL: Homepage, https://xability.github.io/py-maidr
6
6
  Project-URL: Repository, https://github.com/xability/py-maidr
@@ -34,12 +34,14 @@ Provides-Extra: jupyter
34
34
  Requires-Dist: ipykernel>=6.0.0; extra == 'jupyter'
35
35
  Requires-Dist: jupyter<2,>=1.0.0; extra == 'jupyter'
36
36
  Provides-Extra: test
37
+ Requires-Dist: flask>=3.0.0; extra == 'test'
37
38
  Requires-Dist: jupyter<2,>=1.0.0; extra == 'test'
38
39
  Requires-Dist: matplotlib>=3.8; extra == 'test'
39
40
  Requires-Dist: pytest-mock<4,>=3.12.0; extra == 'test'
40
41
  Requires-Dist: pytest<8,>=7.3.2; extra == 'test'
41
42
  Requires-Dist: seaborn>=0.12; extra == 'test'
42
43
  Provides-Extra: visualization
44
+ Requires-Dist: flask>=3.0.0; extra == 'visualization'
43
45
  Requires-Dist: matplotlib>=3.8; extra == 'visualization'
44
46
  Requires-Dist: seaborn>=0.12; extra == 'visualization'
45
47
  Requires-Dist: statsmodels>=0.14.4; extra == 'visualization'
@@ -1,9 +1,9 @@
1
- maidr/__init__.py,sha256=EYV2pThkXEDNDVeWxbEJNzphGq2yMnSr1bl4DYIP_cI,416
1
+ maidr/__init__.py,sha256=bvyrTCLC6rRb_Iu5wCMtnDYCY0tbaZ--c21hFuNMICE,415
2
2
  maidr/api.py,sha256=gRNLXqUWpFGdD-I7Nu6J0_LeEni9KRAr0TBHwHaDAsc,1928
3
3
  maidr/core/__init__.py,sha256=WgxLpSEYMc4k3OyEOf1shOxfEq0ASzppEIZYmE91ThQ,25
4
4
  maidr/core/context_manager.py,sha256=6cT7ZGOApSpC-SLD2XZWWU_H08i-nfv-JUlzXOtvWYw,3374
5
5
  maidr/core/figure_manager.py,sha256=jXs-Prkeru1Pahj21hjh8BAwXM9ZFUZ3GFfKUfIRX_M,4117
6
- maidr/core/maidr.py,sha256=tELie62HFSzZXjMosvVhinqQMzb1G25Odrxb7RBL694,14089
6
+ maidr/core/maidr.py,sha256=1f3H6E2gXTWTdO_vNmiGmPq1dDn-Q96wjirkosxIgPg,15802
7
7
  maidr/core/enum/__init__.py,sha256=9ee78L0dlxEx4ulUGVlD-J23UcUZmrGu0rXms54up3c,93
8
8
  maidr/core/enum/library.py,sha256=e8ujT_L-McJWfoVJd1ty9K_2bwITnf1j0GPLsnAcHes,104
9
9
  maidr/core/enum/maidr_key.py,sha256=ljG0omqzd8K8Yk213N7i7PXGvG-IOlnE5v7o6RoGJzc,795
@@ -41,7 +41,7 @@ maidr/patch/regplot.py,sha256=k86ekd0E4XJ_L1u85zObuDnxuXlM83z7tKtyXRTj2rI,3240
41
41
  maidr/patch/scatterplot.py,sha256=kln6zZwjVsdQzICalo-RnBOJrx1BnIB2xYUwItHvSNY,525
42
42
  maidr/util/__init__.py,sha256=eRJZfRpDX-n7UoV3JXw_9Lbfu_qNl_D0W1UTvLL-Iv4,81
43
43
  maidr/util/dedup_utils.py,sha256=RpgPL5p-3oULUHaTCZJaQKhPHfyPkvBLHMt8lAGpJ5A,438
44
- maidr/util/environment.py,sha256=-2LyZUpHojBCMEbkr_xkcC-_IDqtGDALB8683v7kTdI,5253
44
+ maidr/util/environment.py,sha256=SSfxzjmY2ZVA98B-IVql8n9AX3d6h2f7_R4t3_53dYI,9452
45
45
  maidr/util/mplfinance_utils.py,sha256=aWNRkNS2IAF4YYEF9w7CcmcKWMGg3KkYJAv0xL8KcyY,14417
46
46
  maidr/util/plot_detection.py,sha256=bgLHoDcHSRwOiyKzUK3EqGwdAIhF44ocHW5ox6xYGZw,3883
47
47
  maidr/util/regression_line_utils.py,sha256=yFKr-H0whT_su2YVZwNksBLp5EC5s77sr6HUFgNcsyY,2329
@@ -51,7 +51,7 @@ maidr/util/mixin/extractor_mixin.py,sha256=oHtwpmS5kARvaLrSO3DKTPQxyFUw9nOcKN7rz
51
51
  maidr/util/mixin/merger_mixin.py,sha256=V0qLw_6DUB7X6CQ3BCMpsCQX_ZuwAhoSTm_E4xAJFKM,712
52
52
  maidr/widget/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
53
53
  maidr/widget/shiny.py,sha256=wrrw2KAIpE_A6CNQGBtNHauR1DjenA_n47qlFXX9_rk,745
54
- maidr-1.4.10.dist-info/METADATA,sha256=xoaRoRcpbRdP37x78jYL6TnQ927PW8J4NitwTzK2WQ4,3095
55
- maidr-1.4.10.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
56
- maidr-1.4.10.dist-info/licenses/LICENSE,sha256=IwGE9guuL-ryRPEKi6wFPI_zOhg7zDZbTYuHbSt_SAk,35823
57
- maidr-1.4.10.dist-info/RECORD,,
54
+ maidr-1.6.0.dist-info/METADATA,sha256=e3l2lyk61dpyniYkOGEIO2Ycyt10LWgPfM-z6OR8fYU,3193
55
+ maidr-1.6.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
56
+ maidr-1.6.0.dist-info/licenses/LICENSE,sha256=IwGE9guuL-ryRPEKi6wFPI_zOhg7zDZbTYuHbSt_SAk,35823
57
+ maidr-1.6.0.dist-info/RECORD,,
File without changes