Răsfoiți Sursa

re-adding routes section to reference

Rodja Trappe 2 ani în urmă
părinte
comite
ab6bc012ae
2 a modificat fișierele cu 37 adăugiri și 1 ștergeri
  1. 3 1
      nicegui/functions/static_files.py
  2. 34 0
      website/reference.py

+ 3 - 1
nicegui/functions/static_files.py

@@ -6,7 +6,9 @@ from .. import globals
 def add_static_files(path: str, directory: str) -> None:
     """Static Files
 
-    Makes a local directory available at the specified endpoint, e.g. `'/static'`.
+    `ui.add_static_files` makes a local directory available at the specified endpoint, e.g. `'/static'`.
+    This is useful for providing local data like images to the frontend.
+    Otherwise the browser would not be able to access the files.
     Do only put non-security-critical files in there, as they are accessible to everyone.
 
     :param path: string that starts with a slash "/"

+ 34 - 0
website/reference.py

@@ -745,6 +745,40 @@ You can also set `respond=False` to send a command without waiting for a respons
         ui.button('fire and forget', on_click=alert)
         ui.button('receive result', on_click=get_date)
 
+    h3('Routes')
+
+    @example(ui.add_static_files)
+    def add_static_files_example():
+        ui.add_static_files('/examples', 'examples')
+        ui.label('Some NiceGUI Examples').classes('text-h5')
+        ui.link('AI interface', '/examples/ai_interface/main.py')
+        ui.link('custom FastAPI app', '/examples/custom_fastapi_app/main.py')
+        ui.link('Authentication', '/examples/authentication/main.py')
+
+    @example('''#### API Responses
+
+NiceGUI is based on [FastAPI](https://fastapi.tiangolo.com/). 
+This means you can use all of FastAPI's features. 
+For example, you can implement an RESTful API in addition to your graphical user interface.
+You simply import the `app` object from `nicegui`. 
+Or you can run NiceGUI on top of your own FastAPI app by using `ui.run_with(app)` instead of starting a server automatically with `ui.run()`.
+
+You can also return any other FastAPI response object inside a page function.
+For example, you can return a `RedirectResponse` to redirect the user to another page if certain conditions are met.
+This is used in our [authentication demo](https://github.com/zauberzeug/nicegui/tree/main/examples/authentication/main.py).
+''')
+    def fastapi_example():
+        import random
+
+        from nicegui import app
+
+        @app.get('/random/{max}')
+        def generate_random_number(max: int):
+            return {'min': 0, 'max': max, 'value': random.randint(0, max)}
+
+        max = ui.number('max', value=100)
+        ui.button('generate random number', on_click=lambda: ui.open(f'/random/{max.value}'))
+
     h3('Configuration')
 
     @example(ui.run, browser_title='My App')