浏览代码

matplotlib plots

Falko Schindler 4 年之前
父节点
当前提交
8093e2c632
共有 2 个文件被更改,包括 15 次插入3 次删除
  1. 7 2
      main.py
  2. 8 1
      nice_gui.py

+ 7 - 2
main.py

@@ -1,6 +1,7 @@
 #!/usr/bin/env python3
 #!/usr/bin/env python3
 from nice_gui import ui
 from nice_gui import ui
 from datetime import datetime
 from datetime import datetime
+from matplotlib import pyplot as plt
 
 
 ui.label('Hello world!')
 ui.label('Hello world!')
 
 
@@ -15,9 +16,13 @@ with ui.card():
             ui.label('C2')
             ui.label('C2')
             ui.label('C3')
             ui.label('C3')
 
 
-ui.run()
-
 with ui.row():
 with ui.row():
     ui.label('Time:')
     ui.label('Time:')
     time = ui.label()
     time = ui.label()
     ui.timer(0.1, lambda: time.set_text(datetime.now().strftime("%X")))
     ui.timer(0.1, lambda: time.set_text(datetime.now().strftime("%X")))
+
+with ui.plot():
+    plt.title('Some plot')
+    plt.plot(range(10), [x**2 for x in range(10)])
+
+ui.run()

+ 8 - 1
nice_gui.py

@@ -5,6 +5,7 @@ import uvicorn
 import inspect
 import inspect
 import time
 import time
 import asyncio
 import asyncio
+from contextlib import contextmanager
 from utils import handle_exceptions, provide_arguments
 from utils import handle_exceptions, provide_arguments
 
 
 wp = jp.WebPage(delete_flag=False, title='Nice GUI', favicon='favicon.png')
 wp = jp.WebPage(delete_flag=False, title='Nice GUI', favicon='favicon.png')
@@ -53,6 +54,12 @@ class Ui(Starlette):
             view.on('click', handle_exceptions(provide_arguments(on_click)))
             view.on('click', handle_exceptions(provide_arguments(on_click)))
         return Element(view)
         return Element(view)
 
 
+    @contextmanager
+    def plot(self):
+
+        yield
+        jp.Matplotlib(a=view_stack[-1])
+
     def row(self):
     def row(self):
 
 
         view = jp.Div(classes='flex flex-row gap-4 items-start')
         view = jp.Div(classes='flex flex-row gap-4 items-start')
@@ -83,7 +90,7 @@ class Ui(Starlette):
 
 
     def run(self):
     def run(self):
 
 
-        # NOTE: prevent reloader to restart uvicorn
+        # NOTE: prevent reloader from restarting uvicorn
         if inspect.stack()[-2].filename.endswith('spawn.py'):
         if inspect.stack()[-2].filename.endswith('spawn.py'):
             return
             return