Skip to content

Commit

Permalink
fix(tools): widgets generator handle methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Feb 1, 2024
1 parent e9a57dd commit b796009
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/api/source/tools.widgets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ The configuration file should be as follow.
help: ... # help for doc string
- name: [event_name_py, event-name-js]
help: ... # help for doc string
methods: # list of methods available on vue component
- name: [method_py, methodNameInJS]
help: ... # help for doc string
NextComponentName:
component: next-component-name
properties: []
Expand Down
45 changes: 44 additions & 1 deletion trame/tools/widgets/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ async def create_base_structure(ref_path, config, output):
events = class_info.get("events", [])
file.write(f"\nclass {class_name}(HtmlElement):")
to_py_help(file, class_info)
if class_info.get("methods"):
file.write("\n _next_id = 0")
file.write(
"\n def __init__(self, children=None, **kwargs):"
)
Expand All @@ -113,7 +115,48 @@ async def create_base_structure(ref_path, config, output):
for item in events:
to_py_attr(file, item, 4 * 3)
file.write("\n ]")
file.write("\n\n")

# Generate ref
if class_info.get("methods"):
file.write(
f"""\n {class_name}._next_id += 1"""
)
file.write(
f"""\n self.__ref = kwargs.get("ref", f"{class_name}_{{{class_name}._next_id}}")"""
)
file.write(
"""\n self._attributes["ref"] = f'ref="{self.__ref}"'"""
)
file.write("\n\n")

file.write("\n @property")
file.write("\n def ref(self):")
file.write("\n return self.__ref")
file.write("\n\n")

for entry in class_info.get("methods"):
method = entry.get("name")
help = entry.get("help")
if isinstance(method, (list, tuple)):
py_m, js_m = method
else:
py_m = method
js_m = method
file.write(f"\n def {py_m}(self, *args):")
if help:
file.write(f'\n """{help}')
if help.endswith("\n"):
file.write(' """')
elif "\n" in help:
file.write('\n """')
else:
file.write('"""')
file.write(
f'\n self.server.js_call(self.ref, "{js_m}", *args)'
)
file.write("\n\n")
else:
file.write("\n\n")

file.write("\n__all__ = [")
for class_name in all_class_names:
Expand Down

0 comments on commit b796009

Please sign in to comment.