-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de562d6
commit 324f4cb
Showing
13 changed files
with
145 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,36 @@ | ||
import json | ||
import os | ||
from dataclasses import dataclass | ||
from typing import Any, Callable | ||
|
||
import mesop as me | ||
import mesop.labs as mel | ||
|
||
current_module_dir = os.path.dirname(os.path.abspath(__file__)) | ||
js_file_path = os.path.join(current_module_dir, "custom_component.js") | ||
print(f"JS file path: {js_file_path}") | ||
# current_module_dir = os.path.dirname(os.path.abspath(__file__)) | ||
# js_file_path = os.path.join(current_module_dir, "custom_component.js") | ||
# print(f"JS file path: {js_file_path}") | ||
|
||
|
||
@dataclass(kw_only=True) | ||
class CustomEvent(me.UserEvent): | ||
value: int | ||
# mel.register_event_mapper( | ||
# IncrementEvent, | ||
# lambda event: IncrementEvent(key=event.key, value=event.json["value"]), | ||
# ) | ||
|
||
# Instead of explicit event mapper. | ||
# We will log the target event class when sending the event... | ||
# Then, we will map the values to the class - key=key, othre attrs, based on key | ||
|
||
me.register_event_mapper( | ||
CustomEvent, | ||
lambda event, key: CustomEvent( | ||
key=key.key, value=json.loads(event.string_value)["value"] | ||
), | ||
) | ||
|
||
|
||
@me.web_component(js_path=js_file_path) | ||
@mel.web_component(path="./custom_component.js") | ||
def foo_custom_component( | ||
*, | ||
value: int, | ||
on_event: Callable[..., Any], | ||
on_increment: Callable[[mel.CustomEvent], Any], | ||
key: str | None = None, | ||
style: me.Style | None = None, | ||
): | ||
me.insert_web_component( | ||
mel.insert_web_component( | ||
name="foo-component", | ||
key=key, | ||
style=style, | ||
events={ | ||
"increment-event": on_increment, | ||
}, | ||
properties={ | ||
"value": value, | ||
"handlerId": me.register_event_handler(on_event, CustomEvent), | ||
}, | ||
) | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,26 @@ | ||
from pydantic import BaseModel | ||
|
||
import mesop as me | ||
import mesop.labs as mel | ||
|
||
from .custom_component import CustomEvent, foo_custom_component | ||
from .custom_component import foo_custom_component | ||
|
||
|
||
@me.page(path="/custom_component") | ||
def page(): | ||
me.text("custom_component12") | ||
foo_custom_component(value=me.state(State).value, on_event=on_foo_event) | ||
foo_custom_component(value=me.state(State).value, on_increment=on_increment) | ||
|
||
|
||
@me.stateclass | ||
class State: | ||
value: int = 3 | ||
|
||
|
||
def on_foo_event(e: CustomEvent): | ||
me.state(State).value = e.value | ||
class Increment(BaseModel): | ||
value: int | ||
|
||
|
||
def on_increment(e: mel.CustomEvent): | ||
increment = Increment(**e.value) | ||
me.state(State).value = increment.value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
from mesop.component_helpers.helper import ( | ||
insert_web_component as insert_web_component, | ||
) | ||
from mesop.events import CustomEvent as CustomEvent | ||
from mesop.labs.chat import ChatMessage as ChatMessage | ||
from mesop.labs.chat import chat as chat | ||
from mesop.labs.text_to_image import text_to_image as text_to_image | ||
from mesop.labs.text_to_text import text_io as text_io | ||
from mesop.labs.text_to_text import text_to_text as text_to_text | ||
from mesop.labs.web_component import ( | ||
web_component as web_component, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import inspect | ||
import os | ||
from functools import wraps | ||
from typing import Any, Callable, TypeVar, cast | ||
|
||
from mesop.runtime import runtime | ||
|
||
C = TypeVar("C", bound=Callable[..., Any]) | ||
|
||
|
||
def web_component(path: str): | ||
current_frame = inspect.currentframe() | ||
assert current_frame | ||
previous_frame = current_frame.f_back | ||
assert previous_frame | ||
caller_module_file = inspect.getfile(previous_frame) | ||
caller_module_dir = os.path.dirname(os.path.abspath(caller_module_file)) | ||
full_path = os.path.join(caller_module_dir, path) | ||
|
||
with open(full_path) as js_file: | ||
js_content = js_file.read() | ||
runtime().register_js_script(js_content) | ||
|
||
def component_wrapper(fn: C) -> C: | ||
@wraps(fn) | ||
def wrapper(*args: Any, **kw_args: Any): | ||
fn(*args, **kw_args) | ||
|
||
return cast(C, wrapper) | ||
|
||
return component_wrapper | ||
|
||
|
||
# @dataclass(kw_only=True) | ||
# class EventOutput: | ||
# json: dict[str, Any] | ||
# key: str | ||
|
||
|
||
# def register_event_mapper(event: type[_T], map_fn: Callable[[EventOutput], _T]): | ||
# runtime().register_event_mapper( | ||
# event=event, | ||
# map_fn=lambda event, key: map_fn( | ||
# EventOutput( | ||
# json=json.loads(event.string_value), | ||
# key=key.key, | ||
# ) | ||
# ), | ||
# ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.