From 8ce2f6f5ca4474b5bd52913ac4b000f746825a3e Mon Sep 17 00:00:00 2001 From: langway Date: Wed, 1 Jan 2025 12:18:52 +0800 Subject: [PATCH] add exaple code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、fix AppBar Example2 bugs 2、add exaple code tab(some example can not see tab) --- .idea/.gitignore | 8 ++ .idea/examples.iml | 12 ++ .idea/inspectionProfiles/Project_Default.xml | 57 ++++++++ .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 7 + .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 + .../components/examples_view.py | 124 ++++++++++++++---- .../navigation/appbar/02_appbar_example.py | 3 +- python/apps/controls-gallery/gallerydata.py | 60 +++++---- 10 files changed, 242 insertions(+), 49 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/examples.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..35410cac --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/examples.iml b/.idea/examples.iml new file mode 100644 index 00000000..8b8c3954 --- /dev/null +++ b/.idea/examples.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..52f042be --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,57 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..105ce2da --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..db8786c0 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..704ff0e8 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/python/apps/controls-gallery/components/examples_view.py b/python/apps/controls-gallery/components/examples_view.py index 0265519b..cf86e8f5 100644 --- a/python/apps/controls-gallery/components/examples_view.py +++ b/python/apps/controls-gallery/components/examples_view.py @@ -22,39 +22,111 @@ def display(self, grid_item): self.control_name_text.value = grid_item.name self.control_description.value = grid_item.description + # for example in grid_item.examples: + # self.examples.controls.append( + # ft.Column( + # controls=[ + # ft.Container( + # content=ft.Row( + # alignment=ft.MainAxisAlignment.SPACE_BETWEEN, + # controls=[ + # ft.Text( + # example.name, + # theme_style=ft.TextThemeStyle.TITLE_MEDIUM, + # weight=ft.FontWeight.W_500, + # ), + # ft.IconButton( + # content=ft.Image( + # src="github-mark.svg", + # width=24, + # height=24, + # color=ft.Colors.ON_SURFACE, + # ), + # url=f"https://github.com/flet-dev/examples/blob/main/python/apps/controls-gallery/examples/{example.file_name}", + # url_target=ft.UrlTarget.BLANK, + # ), + # ], + # ), + # bgcolor=ft.Colors.SECONDARY_CONTAINER, + # padding=5, + # border_radius=5, + # ), + # ft.Container( + # content=example.example(), + # clip_behavior=ft.ClipBehavior.NONE, + # ), + # ], + # ) + # ) + for example in grid_item.examples: self.examples.controls.append( - ft.Column( - controls=[ - ft.Container( - content=ft.Row( - alignment=ft.MainAxisAlignment.SPACE_BETWEEN, + ft.Tabs( + selected_index=0, + animation_duration=300, + tabs=[ + ft.Tab( + text="视图", + tab_content=ft.Icon(ft.Icons.VIEW_DAY), + content=self.get_controls_view(example), + ), + ft.Tab( + text="代码", + tab_content=ft.Icon(ft.Icons.CODE), + content=ft.Column( controls=[ - ft.Text( - example.name, - theme_style=ft.TextThemeStyle.TITLE_MEDIUM, - weight=ft.FontWeight.W_500, - ), - ft.IconButton( - content=ft.Image( - src="github-mark.svg", - width=24, - height=24, - color=ft.Colors.ON_SURFACE, + ft.Container( + content=ft.Markdown( + f"```python\r\n{example.source_code}\r\n```", + # example.source_code, + selectable=True, + code_theme=ft.MarkdownCodeTheme.GITHUB, + extension_set=ft.MarkdownExtensionSet.GITHUB_WEB, + # on_tap_link=lambda e: page.launch_url(e.data), ), - url=f"https://github.com/flet-dev/examples/blob/main/python/apps/controls-gallery/examples/{example.file_name}", - url_target=ft.UrlTarget.BLANK, + # clip_behavior=ft.ClipBehavior.NONE, ), ], ), - bgcolor=ft.Colors.SECONDARY_CONTAINER, - padding=5, - border_radius=5, - ), - ft.Container( - content=example.example(), - clip_behavior=ft.ClipBehavior.NONE, + # content=ft.Markdown(example.source_code), ), ], - ) + expand=1, + ), + ) + + def get_controls_view(self, example): + return ft.Column( + controls=[ + ft.Container( + content=ft.Row( + alignment=ft.MainAxisAlignment.SPACE_BETWEEN, + controls=[ + ft.Text( + example.name, + theme_style=ft.TextThemeStyle.TITLE_MEDIUM, + weight=ft.FontWeight.W_500, + ), + ft.IconButton( + content=ft.Image( + src="github-mark.svg", + width=24, + height=24, + color=ft.Colors.ON_SURFACE, + ), + url=f"https://github.com/flet-dev/examples/blob/main/python/apps/controls-gallery/examples/{example.file_name}", + url_target=ft.UrlTarget.BLANK, + ), + ], + ), + bgcolor=ft.Colors.SECONDARY_CONTAINER, + padding=5, + border_radius=5, + ), + ft.Container( + content=example.example(), + clip_behavior=ft.ClipBehavior.NONE, + ), + ], + ) diff --git a/python/apps/controls-gallery/examples/navigation/appbar/02_appbar_example.py b/python/apps/controls-gallery/examples/navigation/appbar/02_appbar_example.py index b76a57cc..87a2c07f 100644 --- a/python/apps/controls-gallery/examples/navigation/appbar/02_appbar_example.py +++ b/python/apps/controls-gallery/examples/navigation/appbar/02_appbar_example.py @@ -1,6 +1,7 @@ import flet as ft -def example2(): +name = "AppBar Example2" +def example(): pagelet = ft.Pagelet( appbar = ft.AppBar( title=ft.Text("TITLE", color=ft.Colors.WHITE), diff --git a/python/apps/controls-gallery/gallerydata.py b/python/apps/controls-gallery/gallerydata.py index c3bf8b7c..0d3e6642 100644 --- a/python/apps/controls-gallery/gallerydata.py +++ b/python/apps/controls-gallery/gallerydata.py @@ -5,6 +5,7 @@ from pathlib import Path import flet as ft +import inspect class GridItem: @@ -21,7 +22,7 @@ def __init__(self): self.file_name = None self.order = None self.example = None - # self.source_code = None + self.source_code = None class ControlGroup: @@ -143,7 +144,7 @@ def list_control_dirs(self, dir): f for f in os.listdir(file_path) if not isfile(f) - and f not in ["index.py", "images", "__pycache__", ".venv", ".git"] + and f not in ["index.py", "images", "__pycache__", ".venv", ".git"] ] return control_dirs @@ -160,7 +161,7 @@ def import_modules(self): grid_item = GridItem(control_dir) for file in self.list_example_files( - control_group_dir.name, control_dir + control_group_dir.name, control_dir ): file_name = os.path.join(control_group_dir.name, control_dir, file) module_name = file_name.replace("/", ".").replace(".py", "") @@ -175,28 +176,43 @@ def import_modules(self): spec = importlib.util.spec_from_file_location( module_name, file_path ) - module = importlib.util.module_from_spec(spec) - sys.modules[module_name] = module - spec.loader.exec_module(module) - print(f"{module_name!r} has been imported") - if file == "index.py": - grid_item.name = module.name - grid_item.description = module.description - else: - example_item = ExampleItem() - example_item.example = module.example - - example_item.file_name = ( - module_name.replace(".", "/") + ".py" - ) - example_item.name = module.name - example_item.order = file[ - :2 - ] # first 2 characters of example file name (e.g. '01') - grid_item.examples.append(example_item) + module = None + try: + module = importlib.util.module_from_spec(spec) + except Exception as e: + a = 1 + + if module: + sys.modules[module_name] = module + spec.loader.exec_module(module) + print(f"{module_name!r} has been imported") + if file == "index.py": + grid_item.name = module.name + grid_item.description = module.description + else: + example_item = ExampleItem() + example_item.example = module.example + + example_item.file_name = ( + module_name.replace(".", "/") + ".py" + ) + example_item.name = module.name + example_item.order = file[ + :2 + ] # first 2 characters of example file name (e.g. '01') + + # example_item.source_code=inspect.getsource(module.example) + example_item.source_code = read_source_code(file_path) + grid_item.examples.append(example_item) grid_item.examples.sort(key=lambda x: x.order) control_group_dir.grid_items.append(grid_item) try: control_group_dir.grid_items.sort(key=lambda x: x.name) except: print(control_group_dir.name, control_group_dir.grid_items) + + +def read_source_code(file_path): + with open(file_path, "r", encoding='utf-8') as f: + source = f.read() + return source