-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(custom_js): example with custom JS
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from pathlib import Path | ||
|
||
from trame.app import get_server | ||
from trame.widgets import vuetify3 | ||
from trame.ui.vuetify3 import SinglePageLayout | ||
|
||
|
||
def load_my_js(server): | ||
# must run before server start | ||
js_file = Path(__file__).with_name("my_utils.js").resolve() | ||
server.enable_module( | ||
dict( | ||
serve={"my_code": str(js_file.parent)}, scripts=[f"my_code/{js_file.name}"] | ||
) | ||
) | ||
|
||
|
||
class CustomAddOnJS: | ||
def __init__(self, server=None, table_size=10): | ||
self.server = get_server(server, client_type="vue3") | ||
load_my_js(self.server) | ||
self.ui = self._build_ui() | ||
|
||
def _build_ui(self): | ||
with SinglePageLayout(self.server, full_height=True) as layout: | ||
with layout.content: | ||
vuetify3.VTextField( | ||
v_model=("text_1", "1.2"), | ||
rules=("[utils.my_code.rules.number]",), | ||
) | ||
vuetify3.VTextField( | ||
v_model=("text_2", "2"), | ||
rules=("[utils.my_code.rules.int]",), | ||
) | ||
|
||
|
||
def main(): | ||
app = CustomAddOnJS() | ||
app.server.start() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,16 @@ | ||
window.trame.utils.my_code = { | ||
rules: { | ||
number(v) { | ||
if (Number.isNaN(Number(v))) { | ||
return "Value needs to be a number"; | ||
} | ||
return true; | ||
}, | ||
int(v) { | ||
if (!Number.isInteger(Number(v))) { | ||
return "Value needs to be an integer"; | ||
} | ||
return true; | ||
} | ||
} | ||
} |