Skip to content

Commit

Permalink
chore: _anywidget -> anywidget (#912)
Browse files Browse the repository at this point in the history
* chore: _anywidget -> anywidget

fixes docs to not show internal/private APIs

* lint

* test fix
  • Loading branch information
akshayka committed Mar 6, 2024
1 parent 0c288b4 commit b8ce1be
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 15 deletions.
4 changes: 2 additions & 2 deletions docs/api/inputs/anywidget.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ widget.value
---

```{eval-rst}
.. autoclass:: marimo._plugins.ui._impl.from_anywidget._anywidget
.. autoclass:: marimo.ui.anywidget
:members:
.. autoclasstoc:: marimo._plugins.ui._impl.from_anywidget._anywidget
.. autoclasstoc:: marimo._plugins.ui._impl.from_anywidget.anywidget
```
2 changes: 1 addition & 1 deletion marimo/_plugins/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from marimo._plugins.ui._impl.data_explorer import data_explorer
from marimo._plugins.ui._impl.dataframes.dataframe import dataframe
from marimo._plugins.ui._impl.dictionary import dictionary
from marimo._plugins.ui._impl.from_anywidget import _anywidget as anywidget
from marimo._plugins.ui._impl.from_anywidget import anywidget
from marimo._plugins.ui._impl.input import (
button,
checkbox,
Expand Down
15 changes: 9 additions & 6 deletions marimo/_plugins/ui/_impl/from_anywidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@
from marimo._plugins.ui._core.ui_element import UIElement

if TYPE_CHECKING:
import anywidget # type: ignore [import-not-found, unused-ignore]
from anywidget import ( # type: ignore [import-not-found,unused-ignore] # noqa: E501
AnyWidget,
)


# Weak dictionary
# When the widget is deleted, the UIElement will be deleted as well
cache: Dict[Any, UIElement[Any, Any]] = weakref.WeakKeyDictionary() # type: ignore[no-untyped-call, unused-ignore, assignment]
cache: Dict[Any, UIElement[Any, Any]] = weakref.WeakKeyDictionary() # type: ignore[no-untyped-call, unused-ignore, assignment] # noqa: E501


def from_anywidget(widget: "anywidget.AnyWidget") -> UIElement[Any, Any]:
def from_anywidget(widget: "AnyWidget") -> UIElement[Any, Any]:
"""Create a UIElement from an AnyWidget."""
if widget not in cache:
cache[widget] = _anywidget(widget) # type: ignore[no-untyped-call, unused-ignore, assignment]
cache[widget] = anywidget(widget) # type: ignore[no-untyped-call, unused-ignore, assignment]
return cache[widget]


T = Dict[str, Any]


@mddoc
class _anywidget(UIElement[T, T]):
class anywidget(UIElement[T, T]):
"""
Create a UIElement from an AnyWidget.
Expand All @@ -51,7 +54,7 @@ class _anywidget(UIElement[T, T]):
- `widget`: The widget to wrap.
"""

def __init__(self, widget: "anywidget.AnyWidget"):
def __init__(self, widget: "AnyWidget"):
self.widget = widget

# Get all the traits of the widget
Expand Down
1 change: 1 addition & 0 deletions marimo/_server/models/completion.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright 2024 Marimo. All rights reserved.
from dataclasses import dataclass


Expand Down
1 change: 1 addition & 0 deletions marimo/_smoke_tests/carousel.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright 2024 Marimo. All rights reserved.
import marimo

__generated_with = "0.2.13"
Expand Down
12 changes: 6 additions & 6 deletions tests/_plugins/ui/_impl/test_anywidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
import pytest

from marimo._dependencies.dependencies import DependencyManager
from marimo._plugins.ui._impl.from_anywidget import _anywidget
from marimo._plugins.ui._impl.from_anywidget import anywidget
from marimo._runtime.runtime import Kernel
from tests.conftest import ExecReqProvider

HAS_DEPS = DependencyManager.has_anywidget()

if HAS_DEPS:
import anywidget
import anywidget as _anywidget
import traitlets

class CounterWidget(anywidget.AnyWidget):
class CounterWidget(_anywidget.AnyWidget):
_esm = """
function render({ model, el }) {
let getCount = () => model.get("count");
Expand All @@ -32,7 +32,7 @@ class CounterWidget(anywidget.AnyWidget):
class TestAnywidget:
@staticmethod
async def test_instances(k: Kernel, exec_req: ExecReqProvider) -> None:
import anywidget
import anywidget as _anywidget

await k.run(
[
Expand Down Expand Up @@ -66,7 +66,7 @@ class CounterWidget(anywidget.AnyWidget):
)
assert k.globals["are_same"] is True
assert k.globals["are_different"] is True
assert isinstance(k.globals["base_widget"], anywidget.AnyWidget)
assert isinstance(k.globals["base_widget"], _anywidget.AnyWidget)
assert "marimo-anywidget" in k.globals["as_marimo_element"].text

@staticmethod
Expand Down Expand Up @@ -105,5 +105,5 @@ class CounterWidget(anywidget.AnyWidget):
),
]
)
assert isinstance(k.globals["w"], _anywidget)
assert isinstance(k.globals["w"], anywidget)
assert k.globals["w_value"]["count"] == 10

0 comments on commit b8ce1be

Please sign in to comment.