metaframe-widget 0.3.2__tar.gz → 0.3.5__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.2
3
+ Version: 0.3.5
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.2"
7
+ version = "0.3.5"
8
8
  description = "anywidget for embedding metaframe URLs in Jupyter and marimo notebooks"
9
9
  license = "MIT"
10
10
  requires-python = ">=3.8"
@@ -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).
@@ -62,8 +71,8 @@ class MetaframeWidget(anywidget.AnyWidget):
62
71
  height: CSS height for the widget container (default "400px").
63
72
  allow: iframe allow attribute string (e.g. "camera; microphone").
64
73
  """
65
- encoded = base64.b64encode(js_code.encode()).decode()
66
- url = f"https://js.mtfm.io/#?js={urllib.parse.quote(encoded)}"
74
+ encoded = string_to_base64_string(js_code)
75
+ url = f"https://js.mtfm.io/#?js={encoded}"
67
76
  return cls(url=url, width=width, height=height, allow=allow, **kwargs)
68
77
 
69
78
  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