maidr 0.12.2__py3-none-any.whl → 0.13.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,5 +1,6 @@
1
- __version__ = "0.12.2"
1
+ __version__ = "0.13.0"
2
2
 
3
+ from .api import close, render, save_html, set_engine, show, stacked
3
4
  from .core import Maidr
4
5
  from .core.enum import PlotType
5
6
  from .patch import (
@@ -12,7 +13,6 @@ from .patch import (
12
13
  lineplot,
13
14
  scatterplot,
14
15
  )
15
- from .api import close, render, save_html, show, stacked
16
16
 
17
17
  __all__ = [
18
18
  "close",
maidr/api.py CHANGED
@@ -1,6 +1,6 @@
1
1
  from __future__ import annotations
2
2
 
3
- from typing import Literal, Any
3
+ from typing import Any, Literal
4
4
 
5
5
  from htmltools import Tag
6
6
  from matplotlib.axes import Axes
@@ -9,6 +9,7 @@ from matplotlib.container import BarContainer
9
9
  from maidr.core import Maidr
10
10
  from maidr.core.enum import PlotType
11
11
  from maidr.core.figure_manager import FigureManager
12
+ from maidr.util.environment import Environment
12
13
 
13
14
 
14
15
  def render(plot: Any) -> Tag:
@@ -39,3 +40,7 @@ def stacked(plot: Axes | BarContainer) -> Maidr:
39
40
  def close(plot: Any) -> None:
40
41
  ax = FigureManager.get_axes(plot)
41
42
  FigureManager.destroy(ax.get_figure())
43
+
44
+
45
+ def set_engine(engine: Literal["js", "ts"] = "js"):
46
+ Environment._set_engine(engine=engine)
maidr/core/maidr.py CHANGED
@@ -78,7 +78,10 @@ class Maidr:
78
78
  html = self._create_html_doc()
79
79
  return html.save_html(file, libdir=lib_dir, include_version=include_version)
80
80
 
81
- def show(self, renderer: Literal["auto", "ipython", "browser"] = "auto") -> object:
81
+ def show(
82
+ self,
83
+ renderer: Literal["auto", "ipython", "browser"] = "auto",
84
+ ) -> object:
82
85
  """
83
86
  Preview the HTML content using the specified renderer.
84
87
 
@@ -190,13 +193,11 @@ class Maidr:
190
193
  def _inject_plot(plot: HTML, maidr: str, maidr_id) -> Tag:
191
194
  """Embed the plot and associated MAIDR scripts into the HTML structure."""
192
195
 
193
- script_check = f"""
194
- function initializeMaidr(maidrId) {{
195
- if (window.init) {{
196
- window.init(maidrId);
197
- }}
198
- }}
196
+ engine = Environment.get_engine()
197
+
198
+ MAIDR_TS_CDN_URL = "https://cdn.jsdelivr.net/npm/maidr-ts/dist/maidr.js"
199
199
 
200
+ maidr_js_script = f"""
200
201
  if (!document.querySelector('script[src="https://cdn.jsdelivr.net/npm/maidr/dist/maidr.min.js"]')) {{
201
202
  var script = document.createElement('script');
202
203
  script.type = 'text/javascript';
@@ -205,19 +206,36 @@ class Maidr:
205
206
  window.init("{maidr_id}");
206
207
  }});
207
208
  document.head.appendChild(script);
209
+ }} else {{
210
+ window.init("{maidr_id}");
211
+ }}
212
+ """
213
+
214
+ maidr_ts_script = f"""
215
+ if (!document.querySelector('script[src="{MAIDR_TS_CDN_URL}"]'))
216
+ {{
217
+ var script = document.createElement('script');
218
+ script.type = 'module';
219
+ script.src = '{MAIDR_TS_CDN_URL}';
220
+ script.addEventListener('load', function() {{
221
+ window.main();
222
+ }});
223
+ document.head.appendChild(script);
208
224
  }} else {{
209
225
  document.addEventListener('DOMContentLoaded', function (e) {{
210
- window.init("{maidr_id}");
226
+ window.main();
211
227
  }});
212
228
  }}
213
229
  """
214
230
 
231
+ script = maidr_js_script if engine == "js" else maidr_ts_script
232
+
215
233
  base_html = tags.div(
216
234
  tags.link(
217
235
  rel="stylesheet",
218
236
  href="https://cdn.jsdelivr.net/npm/maidr/dist/maidr_style.min.css",
219
237
  ),
220
- tags.script(script_check, type="text/javascript"),
238
+ tags.script(script, type="text/javascript"),
221
239
  tags.div(plot),
222
240
  )
223
241
 
@@ -225,7 +243,10 @@ class Maidr:
225
243
 
226
244
  # Render the plot inside an iframe if in a Jupyter notebook, Google Colab
227
245
  # or VSCode notebook. No need for iframe if this is a Quarto document.
228
- if Environment.is_notebook() and not is_quarto:
246
+ # For TypeScript we will use iframe by default for now
247
+ if (Environment.is_notebook() and not is_quarto) or (
248
+ engine == "ts" and Environment.is_notebook()
249
+ ):
229
250
  unique_id = "iframe_" + Maidr._unique_id()
230
251
 
231
252
  def generate_iframe_script(unique_id: str) -> str:
@@ -6,6 +6,7 @@ from matplotlib.container import BarContainer
6
6
  from maidr.core.enum import MaidrKey, PlotType
7
7
  from maidr.core.plot import MaidrPlot
8
8
  from maidr.exception import ExtractionError
9
+ from maidr.util.environment import Environment
9
10
  from maidr.util.mixin import (
10
11
  ContainerExtractorMixin,
11
12
  DictMergerMixin,
@@ -18,18 +19,34 @@ class BarPlot(MaidrPlot, ContainerExtractorMixin, LevelExtractorMixin, DictMerge
18
19
  super().__init__(ax, PlotType.BAR)
19
20
 
20
21
  def _extract_axes_data(self) -> dict:
22
+ engine = Environment.get_engine()
23
+
21
24
  base_schema = super()._extract_axes_data()
22
- bar_ax_schema = {
23
- MaidrKey.X: {
24
- MaidrKey.LEVEL: self.extract_level(self.ax),
25
- },
26
- }
25
+ bar_ax_schema = {}
26
+ if engine == "js":
27
+ bar_ax_schema = {
28
+ MaidrKey.X: {
29
+ MaidrKey.LEVEL: self.extract_level(self.ax),
30
+ },
31
+ }
27
32
  return self.merge_dict(base_schema, bar_ax_schema)
28
33
 
29
34
  def _extract_plot_data(self) -> list:
35
+ engine = Environment.get_engine()
30
36
  plot = self.extract_container(self.ax, BarContainer, include_all=True)
31
37
  data = self._extract_bar_container_data(plot)
32
-
38
+ levels = self.extract_level(self.ax)
39
+ if engine == "ts":
40
+ formatted_data = []
41
+ combined_data = (
42
+ zip(levels, data) if plot[0].orientation == "vertical" else zip(levels, data) # type: ignore
43
+ )
44
+ if len(data) != len(plot): # type: ignore
45
+ for x, y in combined_data: # type: ignore
46
+ formatted_data.append({"x": x, "y": y})
47
+ return formatted_data
48
+ if len(formatted_data) == 0:
49
+ raise ExtractionError(self.type, plot)
33
50
  if data is None:
34
51
  raise ExtractionError(self.type, plot)
35
52
 
@@ -1,9 +1,11 @@
1
1
  from __future__ import annotations
2
+
2
3
  from abc import ABC, abstractmethod
3
4
 
4
5
  from matplotlib.axes import Axes
5
6
 
6
7
  from maidr.core.enum import MaidrKey, PlotType
8
+ from maidr.util.environment import Environment
7
9
 
8
10
 
9
11
  class MaidrPlot(ABC):
@@ -65,6 +67,9 @@ class MaidrPlot(ABC):
65
67
 
66
68
  def _extract_axes_data(self) -> dict:
67
69
  """Extract the plot's axes data"""
70
+ engine = Environment.get_engine()
71
+ if engine == "ts":
72
+ return {MaidrKey.X: self.ax.get_xlabel(), MaidrKey.Y: self.ax.get_ylabel()}
68
73
  return {
69
74
  MaidrKey.X: {
70
75
  MaidrKey.LABEL: self.ax.get_xlabel(),
maidr/util/environment.py CHANGED
@@ -1,8 +1,21 @@
1
1
  import json
2
2
  import os
3
+ from typing import Literal
3
4
 
4
5
 
5
6
  class Environment:
7
+ _engine = "js"
8
+
9
+ @classmethod
10
+ def _set_engine(cls, engine: Literal["js", "ts"]) -> None:
11
+ """Set the engine to use for the MAIDR instance."""
12
+ cls._engine = engine
13
+
14
+ @classmethod
15
+ def get_engine(cls) -> str:
16
+ """Get the engine to use for the MAIDR instance."""
17
+ return cls._engine
18
+
6
19
  @staticmethod
7
20
  def is_interactive_shell() -> bool:
8
21
  """Return True if the environment is an interactive shell."""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: maidr
3
- Version: 0.12.2
3
+ Version: 0.13.0
4
4
  Summary: Multimodal Access and Interactive Data Representations
5
5
  License: GPL-3.0-or-later
6
6
  Keywords: accessibility,visualization,sonification,braille,tactile,multimodal,data representation,blind,low vision,visual impairments
@@ -1,5 +1,5 @@
1
- maidr/__init__.py,sha256=776ah3rShNOZYxAPziBeiZHPXdYh2HxKUv3yWFuhhZA,357
2
- maidr/api.py,sha256=Zt7p8LV1UT6ERlptKrJz4L6r43j3XwiwLGY9GJeEjoo,1206
1
+ maidr/__init__.py,sha256=l8GxTHYGbPGuc6jgS4bbzP2Caqb90CQYYYwW7LWmbbo,369
2
+ maidr/api.py,sha256=u2WOQ8RWmXpzseEgbZJCB7FFZs7cEy-ya-Q18tGAYB4,1350
3
3
  maidr/core/__init__.py,sha256=WgxLpSEYMc4k3OyEOf1shOxfEq0ASzppEIZYmE91ThQ,25
4
4
  maidr/core/context_manager.py,sha256=HSzD4wpiALdI4Vm0QTdWqi1MWR5Uqy8BX_lOlCM3JYY,3463
5
5
  maidr/core/enum/__init__.py,sha256=9ee78L0dlxEx4ulUGVlD-J23UcUZmrGu0rXms54up3c,93
@@ -7,15 +7,15 @@ maidr/core/enum/library.py,sha256=e8ujT_L-McJWfoVJd1ty9K_2bwITnf1j0GPLsnAcHes,10
7
7
  maidr/core/enum/maidr_key.py,sha256=LNtt74d2gurefDNPD3SLLsTxKqO9qTyo3PBAUAyofnc,736
8
8
  maidr/core/enum/plot_type.py,sha256=pyfyIwlq3taqe2Z1sVUSbMsTp5QTvYBlZXsMMMclEVM,293
9
9
  maidr/core/figure_manager.py,sha256=HyYLIX3QzE-PmatSsTez9MmncTEu0wzPdFktZzE3NQM,3891
10
- maidr/core/maidr.py,sha256=HhZDgviRREyMxnHnPW1AytQ3cMGOQ35z_Ej9MWNu5lo,10738
10
+ maidr/core/maidr.py,sha256=2tOBxsyG9a_xIq7cg6P1U665WSC3_NkzKxuz1sS3KxU,11459
11
11
  maidr/core/plot/__init__.py,sha256=xDIpRGM-4DfaSSL3nKcXrjdMecCHJ6en4K4nA_fPefQ,83
12
- maidr/core/plot/barplot.py,sha256=ke4OWBbvWcczHrlwAcMAT2mRbfZLRg7_UOPu9HNmz8k,1834
12
+ maidr/core/plot/barplot.py,sha256=iKob48QXYjezxw5Pqw8C_W8KZvQB2fioEGWCtOeQ3JM,2605
13
13
  maidr/core/plot/boxplot.py,sha256=roADG8xJYRQlZPYbfGBQSQRw8974lhVnngGKEKXJttk,6090
14
14
  maidr/core/plot/grouped_barplot.py,sha256=pRmSAr6obcdjK4PPOt7-U77tMMKLM4lkKOiOklS8TbA,2061
15
15
  maidr/core/plot/heatmap.py,sha256=_Hn_wXsu-BQBYs4YO440-WRhmXAJ1WeBndBGVEUAP5M,2447
16
16
  maidr/core/plot/histogram.py,sha256=QV5W-6ZJQQcZsrM91JJBX-ONktJzH7yg_et5_bBPfQQ,1525
17
17
  maidr/core/plot/lineplot.py,sha256=iHPJgQ7CByM7UItBMDUy7YnkVV6tBRyW_vY4QoWib34,1140
18
- maidr/core/plot/maidr_plot.py,sha256=-QL_k3NtbHUPUpy_T24HtttgUWjyKZDZKPCE-_ZL3hQ,2971
18
+ maidr/core/plot/maidr_plot.py,sha256=_9Ugn3cUwi-URxeTRbDAKDZwASrB65p9TKOXhFX1zbU,3176
19
19
  maidr/core/plot/maidr_plot_factory.py,sha256=Ai-vFpn9YhZPQeVQxdjq0prVJNcGZ8NJvS3eKAxxZ_w,1681
20
20
  maidr/core/plot/scatterplot.py,sha256=sQhT80w0Sp9AsUAZi4v6lfWsHJAvuFcer2aAE2NpAYE,1240
21
21
  maidr/exception/__init__.py,sha256=PzaXoYBhyZxMDcJkuxJugDx7jZeseI0El6LpxIwXyG4,46
@@ -31,13 +31,13 @@ maidr/patch/histogram.py,sha256=tnyKkTMuzDXdyQBywhpHZgH3i2mXzOCSyfUSRM0tfUg,1315
31
31
  maidr/patch/lineplot.py,sha256=AIVsEPXMlkyUfYJ2xcU0GtZLpykEhHDLVFZjYnWCq9U,492
32
32
  maidr/patch/scatterplot.py,sha256=JZJXy3vWbJgzZh2zysNrqSA7MRcfSCms6lr_MY5CMD0,526
33
33
  maidr/util/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
34
- maidr/util/environment.py,sha256=1WuqCD3sN_7amnucz2OkxT8U4RQPyuGG5Xxv9Ik5SDo,4200
34
+ maidr/util/environment.py,sha256=xtfibTH4RXW2GC7R_9OQo119BTe5M3bx0u13eFZaiZs,4554
35
35
  maidr/util/mixin/__init__.py,sha256=aGJZNhtWh77yIVPc7ipIZm1OajigjMtCWYKPuDWTC-c,217
36
36
  maidr/util/mixin/extractor_mixin.py,sha256=Y-34424UR8ECHECAuQZ7zKhyk5QC6GOiUeCMRhI06QQ,3132
37
37
  maidr/util/mixin/merger_mixin.py,sha256=V0qLw_6DUB7X6CQ3BCMpsCQX_ZuwAhoSTm_E4xAJFKM,712
38
38
  maidr/widget/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
39
39
  maidr/widget/shiny.py,sha256=wrrw2KAIpE_A6CNQGBtNHauR1DjenA_n47qlFXX9_rk,745
40
- maidr-0.12.2.dist-info/LICENSE,sha256=IwGE9guuL-ryRPEKi6wFPI_zOhg7zDZbTYuHbSt_SAk,35823
41
- maidr-0.12.2.dist-info/METADATA,sha256=hV_PtugpUUFkYjxqBRyjOrJCLoK6sugXYXDO8KsXTbA,2688
42
- maidr-0.12.2.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
43
- maidr-0.12.2.dist-info/RECORD,,
40
+ maidr-0.13.0.dist-info/LICENSE,sha256=IwGE9guuL-ryRPEKi6wFPI_zOhg7zDZbTYuHbSt_SAk,35823
41
+ maidr-0.13.0.dist-info/METADATA,sha256=WNQKQgPsz3s7J8J5-MK9AsZWJcu6zTfybnoDXRoKJWQ,2688
42
+ maidr-0.13.0.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
43
+ maidr-0.13.0.dist-info/RECORD,,
File without changes