Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Fix state access within notebook (#968) (#969)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhlongviolin1 authored Oct 11, 2023
1 parent c9341e3 commit 1652bd1
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
20 changes: 19 additions & 1 deletion src/taipy/gui/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
from operator import attrgetter
from types import FrameType

from .utils import _get_module_name_from_frame
from flask import has_app_context

from .utils import _get_module_name_from_frame, _is_in_notebook
from .utils._attributes import _attrsetter

if t.TYPE_CHECKING:
Expand Down Expand Up @@ -125,6 +127,14 @@ def __getattribute__(self, name: str) -> t.Any:
raise AttributeError(f"Variable '{name}' is not available to be accessed in shared callback.")
if name not in super().__getattribute__(State.__attrs[1]):
raise AttributeError(f"Variable '{name}' is not defined.")
if not has_app_context() and _is_in_notebook():
with gui.get_flask_app().app_context():
# Code duplication is ugly but necessary due to frame resolution
set_context = self._set_context(gui)
encoded_name = gui._bind_var(name)
attr = getattr(gui._bindings(), encoded_name)
self._reset_context(gui, set_context)
return attr
set_context = self._set_context(gui)
encoded_name = gui._bind_var(name)
attr = getattr(gui._bindings(), encoded_name)
Expand All @@ -139,6 +149,14 @@ def __setattr__(self, name: str, value: t.Any) -> None:
raise AttributeError(f"Variable '{name}' is not available to be accessed in shared callback.")
if name not in super().__getattribute__(State.__attrs[1]):
raise AttributeError(f"Variable '{name}' is not accessible.")
if not has_app_context() and _is_in_notebook():
with gui.get_flask_app().app_context():
# Code duplication is ugly but necessary due to frame resolution
set_context = self._set_context(gui)
encoded_name = gui._bind_var(name)
setattr(gui._bindings(), encoded_name, value)
self._reset_context(gui, set_context)
return
set_context = self._set_context(gui)
encoded_name = gui._bind_var(name)
setattr(gui._bindings(), encoded_name, value)
Expand Down
59 changes: 45 additions & 14 deletions tests/taipy/gui/notebook/simple_gui.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,7 @@
"import"
]
},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'taipy_proxy.gui'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"Input \u001b[0;32mIn [1]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtaipy_proxy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mgui\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Gui, Markdown\n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'taipy_proxy.gui'"
]
}
],
"outputs": [],
"source": [
"from importlib.util import find_spec\n",
"\n",
Expand All @@ -42,7 +30,8 @@
},
"outputs": [],
"source": [
"page = Markdown(\"# Hello\")"
"a = 10\n",
"page = Markdown(\"# Hello\\n<|{a}|>\")"
]
},
{
Expand Down Expand Up @@ -74,6 +63,48 @@
"gui.run(run_browser=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b722848",
"metadata": {
"tags": [
"get_variable"
]
},
"outputs": [],
"source": [
"gui.state.a"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "12e0df89",
"metadata": {
"tags": [
"set_variable"
]
},
"outputs": [],
"source": [
"gui.state.a = 20"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8332b3b5",
"metadata": {
"tags": [
"re_get_variable"
]
},
"outputs": [],
"source": [
"gui.state.a"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
10 changes: 10 additions & 0 deletions tests/taipy/gui/notebook/test_notebook_simple_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ def test_notebook_simple_gui(tb, helpers):
while not helpers.port_check():
time.sleep(0.1)
assert ">Hello</h1>" in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
assert 'defaultValue=\\"10\\"' in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
# Test state manipulation within notebook
tb.execute_cell("get_variable")
assert "10" in tb.cell_output_text("get_variable")
assert 'defaultValue=\\"10\\"' in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
tb.execute_cell("set_variable")
assert 'defaultValue=\\"20\\"' in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
tb.execute_cell("re_get_variable")
assert "20" in tb.cell_output_text("re_get_variable")
# Test page reload
tb.execute_cell("gui_stop")
with pytest.raises(Exception) as exc_info:
urlopen("http://127.0.0.1:5000/taipy-jsx/page1")
Expand Down

0 comments on commit 1652bd1

Please sign in to comment.