Răsfoiți Sursa

improved capabilities of writing demo code
1. commented lines are also shown if they are indented
2. placing "# HIDE" behind a line will hide it from the rendered demo

Rodja Trappe 2 ani în urmă
părinte
comite
c78cc9382d
1 a modificat fișierele cu 5 adăugiri și 3 ștergeri
  1. 5 3
      website/demo.py

+ 5 - 3
website/demo.py

@@ -15,13 +15,15 @@ BROWSER_BGCOLOR = '#00000010'
 BROWSER_COLOR = '#ffffff'
 
 
-def remove_prefix(text: str, prefix: str) -> str:
-    return text[len(prefix):] if text.startswith(prefix) else text
+def remove_comment(text: str) -> str:
+    """non-executed lines should be shown in the code examples"""
+    return text.replace('# ', '')
 
 
 def demo(f: Callable) -> Callable:
     with ui.column().classes('w-full items-stretch gap-8 no-wrap min-[1500px]:flex-row'):
         code = inspect.getsource(f).split('# END OF DEMO')[0].strip().splitlines()
+        code = [line for line in code if not line.endswith("# HIDE")]
         while not code[0].strip().startswith('def') and not code[0].strip().startswith('async def'):
             del code[0]
         del code[0]
@@ -31,7 +33,7 @@ def demo(f: Callable) -> Callable:
             del code[0]
         indentation = len(code[0]) - len(code[0].lstrip())
         code = [line[indentation:] for line in code]
-        code = ['from nicegui import ui'] + [remove_prefix(line, '# ') for line in code]
+        code = ['from nicegui import ui'] + [remove_comment(line) for line in code]
         code = ['' if line == '#' else line for line in code]
         if not code[-1].startswith('ui.run('):
             code.append('')