Skip to content
14 changes: 11 additions & 3 deletions examples/ace_editor.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import uvicorn
from contextlib import asynccontextmanager
from pydantic import BaseModel

from uiwiz import UiwizApp, ui
from uiwiz.server import run

app = UiwizApp()


@asynccontextmanager
async def lifespan(app):
print("")
yield
print("")

app = UiwizApp(lifespan=lifespan)

class DataInput(BaseModel):
ace_data: str

Expand Down Expand Up @@ -35,4 +43,4 @@ async def home_page():


if __name__ == "__main__":
uvicorn.run("ace_editor:app", reload=True)
run("ace_editor:app")
4 changes: 2 additions & 2 deletions examples/dict_example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import uvicorn
from uiwiz import server

from uiwiz import UiwizApp, ui

Expand Down Expand Up @@ -42,4 +42,4 @@ async def test():


if __name__ == "__main__":
uvicorn.run("dict_example:app", reload=True)
server.run("dict_example:app")
4 changes: 2 additions & 2 deletions examples/drawer_example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

import uvicorn
from uiwiz import server

import uiwiz.ui as ui
from uiwiz.app import UiwizApp
Expand Down Expand Up @@ -57,4 +57,4 @@ async def test():


if __name__ == "__main__":
uvicorn.run("drawer_example:app", reload=True)
server.run("drawer_example:app")
4 changes: 2 additions & 2 deletions examples/echart_example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from random import randint

import uvicorn
from uiwiz import server

from uiwiz import UiwizApp, ui

Expand Down Expand Up @@ -90,4 +90,4 @@ async def test():


if __name__ == "__main__":
uvicorn.run("echart_example:app", reload=True)
server.run("echart_example:app")
4 changes: 2 additions & 2 deletions examples/edit_table.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Annotated

import uvicorn
from uiwiz import server
from fastapi import Request
from pydantic import BaseModel, Field

Expand Down Expand Up @@ -79,4 +79,4 @@ async def test(request: Request):


if __name__ == "__main__":
uvicorn.run("edit_table:app", reload=True)
server.run("edit_table:app")
10 changes: 4 additions & 6 deletions examples/input_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from typing import Annotated, Optional

import pandas as pd
import uvicorn
from fastapi import Depends
from uiwiz import server
from fastapi import Depends, Request
from pydantic import BaseModel

from uiwiz import Element, PageDefinition, PageRouter, UiwizApp, ui
Expand Down Expand Up @@ -59,12 +59,10 @@ def footer(self, content):

route = PageRouter(page_definition_class=MyDefinition)

PageRouter()


# set static page title
@route.page("/", title="Input Example")
async def test(page: Annotated[MyDefinition, Depends()]):
async def test(page: Annotated[MyDefinition, Depends()], req: Request):
# set dynamic page title
page.title = "Dynamic title"
# set dynamic page lang
Expand Down Expand Up @@ -152,4 +150,4 @@ async def test(page: Annotated[MyDefinition, Depends()]):


if __name__ == "__main__":
uvicorn.run("input_example:app", reload=True)
server.run("input_example:app")
14 changes: 7 additions & 7 deletions examples/multipage/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import uvicorn
from uiwiz import server

from examples.multipage.second_page import router
from multipage.second_page import router
from uiwiz import UiwizApp, ui
from uiwiz.shared import page_map

app = UiwizApp()
app.include_router(router)
Expand All @@ -13,10 +12,11 @@ async def test():
with ui.element().classes("col lg:px-80"):
with ui.element().classes("w-full"):
ui.element(content="Hello, world!")
for route in page_map.values():
with ui.col():
ui.link(route, route)
with ui.col():
ui.link("Home", "/")
with ui.col():
ui.link("Second page", "/second_page")


if __name__ == "__main__":
uvicorn.run("examples.multipage.main:app", reload=True, port=8000)
server.run("multipage.main:app")
4 changes: 2 additions & 2 deletions examples/run_simple.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from io import BytesIO

import pandas as pd
import uvicorn
from fastapi import Request, UploadFile

import uiwiz.ui as ui
from uiwiz.app import UiwizApp
from uiwiz import server

app = UiwizApp(theme="aqua")

Expand Down Expand Up @@ -62,4 +62,4 @@ async def test(request: Request):


if __name__ == "__main__":
uvicorn.run("run_simple:app", reload=True)
server.run("run_simple:app")
6 changes: 2 additions & 4 deletions examples/run_tabs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import uvicorn

import uiwiz.ui as ui
from uiwiz.app import UiwizApp
from uiwiz import UiwizApp, server

app = UiwizApp()

Expand Down Expand Up @@ -35,4 +33,4 @@ async def test():


if __name__ == "__main__":
uvicorn.run("run_tabs:app", reload=True)
server.run("run_tabs:app")
7 changes: 2 additions & 5 deletions examples/sample.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import uvicorn

from uiwiz import ui
from uiwiz.app import UiwizApp
from uiwiz import ui, UiwizApp, server

app = UiwizApp()

Expand All @@ -12,4 +9,4 @@ async def home_page():


if __name__ == "__main__":
uvicorn.run(app)
server.run(app)
5 changes: 2 additions & 3 deletions examples/validate_form_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
from datetime import date
from typing import Annotated, Literal

import uvicorn
from pydantic import BaseModel, Field

import uiwiz.ui as ui
from uiwiz.app import UiwizApp
from uiwiz import UiwizApp, server
from uiwiz.models.model_handler import UiAnno

app = UiwizApp(auto_close_toast_error=False)
Expand Down Expand Up @@ -90,4 +89,4 @@ async def test():

if __name__ == "__main__":
app_name = os.path.basename(__file__).replace(".py", "")
uvicorn.run(app=f"{app_name}:app", host="0.0.0.0", port=8080, workers=1, reload=True)
server.run(app=f"{app_name}:app")
8 changes: 5 additions & 3 deletions src/uiwiz/page_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from uiwiz.version import __version__



class PageDefinition:
html_ele: Element
header_ele: Element
Expand Down Expand Up @@ -51,6 +52,7 @@ def footer(self, content: Element) -> None:

"""
self._lang: str = "en"
self._title_ele: Optional[Element] = None

@property
def lang(self) -> str:
Expand All @@ -63,11 +65,11 @@ def lang(self, value: str) -> None:

@property
def title(self) -> str:
return self.title_ele.content
return self._title_ele.content

@title.setter
def title(self, value: str) -> None:
self.title_ele.content = value
self._title_ele.content = value

async def render(
self,
Expand Down Expand Up @@ -102,7 +104,7 @@ def render(self):
Element("meta", charset="utf-8")
Element("meta", description=frame.meta_description_content)

self.title_ele = Element("title", content=page_title)
self._title_ele = Element("title", content=page_title)

Element("link", href=f"/_static/{__version__}/libs/output.css", rel="stylesheet", type="text/css")
Element("link", href=f"/_static/{__version__}/libs/daisyui.css", rel="stylesheet", type="text/css")
Expand Down
11 changes: 8 additions & 3 deletions src/uiwiz/page_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,13 @@ async def decorated(*dec_args, **dec_kwargs: DecKwargs) -> Response:
Frame.get_stack().del_stack()
# Create frame before function is called

request = dec_kwargs["request"]
response = dec_kwargs["response"]
request = None
response = None
for value in dec_kwargs.values():
if isinstance(value, Request):
request = value
if isinstance(value, Response):
response = value

if self.page_definition_class is None:
self.page_definition_class = request.app.page_definition_class
Expand All @@ -185,7 +190,7 @@ async def decorated(*dec_args, **dec_kwargs: DecKwargs) -> Response:

page = page_class()

dec_kwargs = {k: v for k, v in dec_kwargs.items() if k in parameters_of_decorated_func}
dec_kwargs = {k: v if not isinstance(v, PageDefinition) else page for k, v in dec_kwargs.items() if k in parameters_of_decorated_func}
user_method = partial(func, *dec_args, **dec_kwargs)
result = await page.render(user_method=user_method, request=request, title=cap_title)
if isinstance(result, Response):
Expand Down
7 changes: 7 additions & 0 deletions src/uiwiz/server/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from uiwiz.server._server import Config, Server

def run(app: str, host: str = "localhost", port:int = 8080):
config = Config(host=host, port=port, app=app, root_path="")

server = Server(config)
server.run()
Loading