metaframe-widget 0.3.3__tar.gz → 0.3.6__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: metaframe-widget
3
- Version: 0.3.3
3
+ Version: 0.3.6
4
4
  Summary: anywidget for embedding metaframe URLs in Jupyter and marimo notebooks
5
5
  Project-URL: Homepage, https://github.com/metapages/metaframe-js
6
6
  Project-URL: Repository, https://github.com/metapages/metaframe-js
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "metaframe-widget"
7
- version = "0.3.3"
7
+ version = "0.3.6"
8
8
  description = "anywidget for embedding metaframe URLs in Jupyter and marimo notebooks"
9
9
  license = "MIT"
10
10
  requires-python = ">=3.8"
@@ -1,5 +1,5 @@
1
1
  ESM = """
2
- const CDN_URL = "https://cdn.jsdelivr.net/npm/@metapages/metapage@1.10.6/+esm";
2
+ const CDN_URL = "https://cdn.jsdelivr.net/npm/@metapages/metapage@1.10.8/+esm";
3
3
 
4
4
  let _renderMetapage = null;
5
5
 
@@ -6,6 +6,15 @@ import traitlets
6
6
 
7
7
  from ._esm import ESM
8
8
 
9
+ import base64
10
+
11
+ import urllib.parse
12
+
13
+ # Equivalent to btoa(encodeURIComponent(value));
14
+ def string_to_base64_string(value: str) -> str:
15
+ encoded_uri_component = urllib.parse.quote(value, safe="-_.!~*'()")
16
+ return base64.b64encode(encoded_uri_component.encode("ascii")).decode("ascii")
17
+
9
18
 
10
19
  class MetaframeWidget(anywidget.AnyWidget):
11
20
  """Widget that renders a metaframe iframe (works in Jupyter and marimo).
@@ -28,6 +37,21 @@ class MetaframeWidget(anywidget.AnyWidget):
28
37
  height = traitlets.Unicode("400px").tag(sync=True)
29
38
  allow = traitlets.Unicode("").tag(sync=True)
30
39
 
40
+ def __init__(self, *args, **kwargs):
41
+ super().__init__(*args, **kwargs)
42
+ # Sync layout.height/width so ipywidgets' LayoutView doesn't clear
43
+ # the styles that the ESM sets on the element.
44
+ self.layout.height = self.height
45
+ self.layout.width = self.width
46
+ self.observe(self._sync_layout_height, names=["height"])
47
+ self.observe(self._sync_layout_width, names=["width"])
48
+
49
+ def _sync_layout_height(self, change):
50
+ self.layout.height = change["new"]
51
+
52
+ def _sync_layout_width(self, change):
53
+ self.layout.width = change["new"]
54
+
31
55
  def set_inputs(self, d: dict):
32
56
  """Merge a dict into the current inputs."""
33
57
  self.inputs = {**self.inputs, **d}
@@ -62,8 +86,8 @@ class MetaframeWidget(anywidget.AnyWidget):
62
86
  height: CSS height for the widget container (default "400px").
63
87
  allow: iframe allow attribute string (e.g. "camera; microphone").
64
88
  """
65
- encoded = base64.b64encode(js_code.encode()).decode()
66
- url = f"https://js.mtfm.io/#?js={urllib.parse.quote(encoded)}"
89
+ encoded = string_to_base64_string(js_code)
90
+ url = f"https://js.mtfm.io/#?js={encoded}"
67
91
  return cls(url=url, width=width, height=height, allow=allow, **kwargs)
68
92
 
69
93
  def pipe_to(self, target: "MetaframeWidget", output_key: str, input_key: str = None):
@@ -31,8 +31,10 @@ def test_set_input():
31
31
  def test_from_code():
32
32
  code = 'console.log("hello");'
33
33
  w = MetaframeWidget.from_code(code)
34
- encoded = base64.b64encode(code.encode()).decode()
35
- expected_url = f"https://js.mtfm.io/#?js={urllib.parse.quote(encoded)}"
34
+ # from_code does: encodeURIComponent(code) then base64
35
+ encoded_uri = urllib.parse.quote(code, safe="-_.!~*'()")
36
+ encoded = base64.b64encode(encoded_uri.encode("ascii")).decode("ascii")
37
+ expected_url = f"https://js.mtfm.io/#?js={encoded}"
36
38
  assert w.url == expected_url
37
39
 
38
40
 
@@ -75,6 +77,33 @@ def test_pipe_to_ignores_unrelated_keys():
75
77
  assert sink.inputs == {}
76
78
 
77
79
 
80
+ def test_layout_height_synced_on_init():
81
+ """layout.height must match the height trait so ipywidgets doesn't clear it."""
82
+ w = MetaframeWidget()
83
+ assert w.layout.height == "400px"
84
+ assert w.layout.width == "100%"
85
+
86
+
87
+ def test_layout_height_synced_with_custom_value():
88
+ w = MetaframeWidget(height="600px", width="80%")
89
+ assert w.layout.height == "600px"
90
+ assert w.layout.width == "80%"
91
+
92
+
93
+ def test_layout_height_synced_from_code():
94
+ w = MetaframeWidget.from_code("console.log(1)", height="420px")
95
+ assert w.layout.height == "420px"
96
+
97
+
98
+ def test_layout_height_updates_on_change():
99
+ """Changing height trait after init must propagate to layout."""
100
+ w = MetaframeWidget()
101
+ w.height = "500px"
102
+ assert w.layout.height == "500px"
103
+ w.width = "50%"
104
+ assert w.layout.width == "50%"
105
+
106
+
78
107
  def test_pipe_chain_five_widgets():
79
108
  """Five widgets piped in a chain propagate data end-to-end."""
80
109
  w1 = MetaframeWidget()