From 348838a7788e4c54fb1e7de1fcfd50f4b139c5f0 Mon Sep 17 00:00:00 2001 From: Lightbridge-KS Date: Sat, 23 Mar 2024 13:18:06 +0700 Subject: [PATCH] Try separate manual as data --- modules/app_change.py | 5 ++-- modules/app_spine_ht_loss.py | 5 ++-- {man => modules/man}/change_app_man.md | 0 {man => modules/man}/spine_ht_loss_man.md | 0 modules/manual.py | 36 ++++++++++++++++++----- 5 files changed, 35 insertions(+), 11 deletions(-) rename {man => modules/man}/change_app_man.md (100%) rename {man => modules/man}/spine_ht_loss_man.md (100%) diff --git a/modules/app_change.py b/modules/app_change.py index 7c129cf..07da222 100644 --- a/modules/app_change.py +++ b/modules/app_change.py @@ -5,7 +5,7 @@ ) import nm from utils import attempt_float, read_markdown_file -from modules.manual import man_app +from modules.manual import read_man # man_app # Change App class AppChange(ft.UserControl): @@ -35,7 +35,8 @@ def build(self): self.btn, self.output_text_field, ft.Text("How to use", theme_style=ft.TextThemeStyle.TITLE_MEDIUM), - ft.Markdown(man_app["change"], selectable=True) + # ft.Markdown(man_app["change"], selectable=True), + ft.Markdown(read_man("change_app_man.md"), selectable=True) ] ) ) diff --git a/modules/app_spine_ht_loss.py b/modules/app_spine_ht_loss.py index 6bb93a6..ab4539d 100644 --- a/modules/app_spine_ht_loss.py +++ b/modules/app_spine_ht_loss.py @@ -5,7 +5,7 @@ ) import nm from utils import parse_str_to_num_or_list, read_markdown_file -from modules.manual import man_app +from modules.manual import read_man # man_app # Spine Ht Loss App class AppSpineHtLoss(ft.UserControl): @@ -36,7 +36,8 @@ def build(self): self.btn, self.output_text_field, ft.Text("How to use", theme_style=ft.TextThemeStyle.TITLE_MEDIUM), - ft.Markdown(man_app["spine_ht_loss"], selectable=True) + # ft.Markdown(man_app["spine_ht_loss"], selectable=True) + ft.Markdown(read_man("spine_ht_loss_man.md"), selectable=True) ] ) ) diff --git a/man/change_app_man.md b/modules/man/change_app_man.md similarity index 100% rename from man/change_app_man.md rename to modules/man/change_app_man.md diff --git a/man/spine_ht_loss_man.md b/modules/man/spine_ht_loss_man.md similarity index 100% rename from man/spine_ht_loss_man.md rename to modules/man/spine_ht_loss_man.md diff --git a/modules/manual.py b/modules/manual.py index 1b6f915..f54e79c 100644 --- a/modules/manual.py +++ b/modules/manual.py @@ -1,10 +1,32 @@ -man_app={ - "change": f"""- Input current value as number (e.g. 2) -- Input previous value as number (e.g. 1) -- % change = (current - previous) * 100 / previous""", - "spine_ht_loss": f"""- Input height in centimeter (e.g. 10) -- Comma-separated value to calculate mean (e.g. 10, 12)""" -} +# man_app={ +# "change": f"""- Input current value as number (e.g. 2) +# - Input previous value as number (e.g. 1) +# - % change = (current - previous) * 100 / previous""", +# "spine_ht_loss": f"""- Input height in centimeter (e.g. 10) +# - Comma-separated value to calculate mean (e.g. 10, 12)""" +# } +def read_man(man_name): + """Read Markdown from `man/`""" + # Technique from this https://py-pkgs.org/04-package-structure#including-non-code-files-in-a-package + from importlib import resources + with resources.path("modules.man", man_name) as file_path: + markdown_content = read_markdown_file(file_path) + return markdown_content + + +def read_markdown_file(file_path): + from pathlib import Path + file_path = Path(file_path) + try: + # Open the markdown file in read mode + with open(file_path, 'r', encoding='utf-8') as file: + # Read the entire file content into a string + markdown_content = file.read() + return markdown_content + except FileNotFoundError: + print(f"The file {file_path} was not found.") + except Exception as e: + print(f"An error occurred: {e}")