Răsfoiți Sursa

integrate spike into main library and demo

Falko Schindler 4 ani în urmă
părinte
comite
ec337fd902
7 a modificat fișierele cu 15 adăugiri și 79 ștergeri
  1. 1 1
      Dockerfile
  2. 5 10
      README.md
  3. 1 5
      main.py
  4. 1 1
      nice_gui/__init__.py
  5. 7 5
      nice_gui/nice_gui.py
  6. 0 56
      nicegui
  7. 0 1
      pyproject.toml

+ 1 - 1
Dockerfile

@@ -13,4 +13,4 @@ WORKDIR /app
 COPY ./pyproject.toml ./poetry.lock* main.py ./
 RUN poetry install --no-root
 
-CMD ./nicegui main.py
+CMD ./main.py

+ 5 - 10
README.md

@@ -1,23 +1,18 @@
 # Nice GUI
 
-# Usage
+## Usage
 
 Write your nice GUI in a file `main.py`:
 
-    import nice_gui
+    from nice_gui import ui
 
-    ui = nice_gui.Ui()
     ui.label('Hello Nice GUI!')
-    ui.Button('BUTTON', on_click: lambda: print('button was pressed'))
+    ui.button('BUTTON', on_click: lambda: print('button was pressed'))
 
-Launch it with
+Launch it with:
 
     python3 main.py
 
-Or use with autoreloading by calling
-
-    nicegui main.py
-
-# Full Example
+## Full Example
 
 See [main.py](https://github.com/zauberzeug/nice_gui/blob/main/main.py)

+ 1 - 5
main.py

@@ -1,11 +1,9 @@
 #!/usr/bin/env python3
-import nice_gui
+from nice_gui import ui
 from datetime import datetime
 from matplotlib import pyplot as plt
 import numpy as np
 
-ui = nice_gui.Ui()
-
 with ui.row():
     with ui.card():
         ui.label('Interactive elements', 'h5')
@@ -63,5 +61,3 @@ with ui.row():
             [np.sin(datetime.now().timestamp()) + 0.02 * np.random.randn()],
             [np.cos(datetime.now().timestamp()) + 0.02 * np.random.randn()],
         ]))
-
-ui.run()

+ 1 - 1
nice_gui/__init__.py

@@ -1 +1 @@
-from nice_gui.nice_gui import Ui
+from nice_gui.nice_gui import ui

+ 7 - 5
nice_gui/nice_gui.py

@@ -6,10 +6,14 @@ import uvicorn
 import inspect
 import time
 import asyncio
+import os.path
 from contextlib import contextmanager
 from matplotlib import pyplot as plt
 from .utils import handle_exceptions, provide_arguments
-from multiprocessing import Process
+
+if not inspect.stack()[-2].filename.endswith('spawn.py'):
+    module = os.path.splitext(os.path.basename(inspect.stack()[-1].filename))[0]
+    uvicorn.run(f'{module}:ui', host='0.0.0.0', port=80, lifespan='on', reload=True)
 
 wp = jp.QuasarPage(delete_flag=False, title='Nice GUI', favicon='favicon.png')
 wp.head_html = '<script>confirm = () => true;</script>'  # HACK: avoid confirmation dialog for reload
@@ -102,10 +106,6 @@ class Ui(Starlette):
         def startup():
             [jp.run_task(t) for t in self.tasks]
 
-    def run(self):
-
-        uvicorn.run(self, host='0.0.0.0', port=80, lifespan='on', reload=False)
-
     def label(self, text='', typography=[]):
 
         if isinstance(typography, str):
@@ -237,3 +237,5 @@ class Ui(Starlette):
                     await asyncio.sleep(interval)
 
         self.tasks.append((timeout() if once else loop()))
+
+ui = Ui()

+ 0 - 56
nicegui

@@ -1,56 +0,0 @@
-#!/usr/bin/env python
-"""modified copy of https://github.com/stevekrenzel/autoreload/blob/master/autoreload"""
-
-import os
-import sys
-import subprocess
-import time
-import psutil
-
-def file_filter(name):
-    return (not name.startswith(".")) and (not name.endswith(".swp"))
-
-
-def file_times(path):
-    for top_level in filter(file_filter, os.listdir(path)):
-        for root, dirs, files in os.walk(top_level):
-            for file in filter(file_filter, files):
-                yield os.stat(os.path.join(root, file)).st_mtime
-
-
-def print_stdout(process):
-    stdout = process.stdout
-    if stdout != None:
-        print(stdout)
-
-
-# We concatenate all of the arguments together, and treat that as the command to run
-command = " ".join(sys.argv[1:])
-command = 'python3 ' + command
-
-# The path to watch
-path = "."
-
-# How often we check the filesystem for changes (in seconds)
-wait = 1
-
-# The process to autoreload
-process = subprocess.Popen(command, shell=True)
-
-# The current maximum file modified time under the watched directory
-last_mtime = max(file_times(path))
-
-
-while True:
-    max_mtime = max(file_times(path))
-    print_stdout(process)
-    if max_mtime > last_mtime:
-        last_mtime = max_mtime
-        print(f'Restarting {process}.')
-        new = subprocess.Popen(command, shell=True)
-        time.sleep(0.5)
-        for child in psutil.Process(process.pid).children(recursive=True):
-            child.kill()
-        process.kill()
-        process = new
-    time.sleep(wait)

+ 0 - 1
pyproject.toml

@@ -11,7 +11,6 @@ justpy = "0.1.5"
 icecream = "^2.1.0"
 matplotlib = "^3.4.1"
 autopep8 = "^1.5.7"
-psutil = "^5.8.0"
 
 [tool.poetry.dev-dependencies]
 debugpy = "^1.3.0"