Skip to content

Commit

Permalink
Fix ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrochar committed Feb 19, 2024
1 parent d619758 commit 8d50a9a
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 186 deletions.
13 changes: 3 additions & 10 deletions blitz/api/blitz_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ def _create_crud_router(self, resource: BlitzResource) -> CRUDGenerator:
# Rebuild the model to include forward ref types that was not available at the time of the model creation
# We need to use the model AFTER the rebuild because if not, all the relationship and cie will not be set
# correctly.
types_namespace = {
resource.model.__name__: resource.model
for resource in self.blitz_app.resources
}
types_namespace = {resource.model.__name__: resource.model for resource in self.blitz_app.resources}
read_model.model_rebuild(_types_namespace=types_namespace)
create_model.model_rebuild(_types_namespace=types_namespace)
update_model.model_rebuild(_types_namespace=types_namespace)
Expand Down Expand Up @@ -189,12 +186,8 @@ def create_blitz_api(
warnings.simplefilter("ignore", category=SAWarning)
BlitzAdmin(blitz_app).mount_to(blitz_api)

print(
"\n[bold yellow]This is still an alpha. Please do not use in production.[/bold yellow]"
)
print(
"[bold yellow]Please report any issues on https://github.com/Paperz-org/blitz[/bold yellow]"
)
print("\n[bold yellow]This is still an alpha. Please do not use in production.[/bold yellow]")
print("[bold yellow]Please report any issues on https://github.com/Paperz-org/blitz[/bold yellow]")
print(
"\n".join(
(
Expand Down
29 changes: 7 additions & 22 deletions blitz/cli/commands/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,12 @@


def start_blitz(
blitz_app_name: Annotated[
Optional[str], typer.Argument(help="Blitz app name")
] = None,
blitz_app_name: Annotated[Optional[str], typer.Argument(help="Blitz app name")] = None,
admin: Annotated[bool, typer.Option(help="Don't create admin.")] = True,
port: Annotated[
int, typer.Option(help="Define the port of the server")
] = get_settings().BLITZ_PORT,
config_route: Annotated[
bool, typer.Option(help="Enable the blitz config route.")
] = True,
port: Annotated[int, typer.Option(help="Define the port of the server")] = get_settings().BLITZ_PORT,
config_route: Annotated[bool, typer.Option(help="Enable the blitz config route.")] = True,
hot_reload: Annotated[bool, typer.Option(help="Enable the hot reload.")] = True,
version: Annotated[
Optional[str], typer.Option(help="Define the version of the app.")
] = None,
version: Annotated[Optional[str], typer.Option(help="Define the version of the app.")] = None,
) -> None:
blitz = BlitzCore()

Expand Down Expand Up @@ -87,14 +79,7 @@ def start_blitz(
log_level="info",
)
server = uvicorn.Server(server_config)
ChangeReload(
server_config, target=server.run, sockets=[server_config.bind_socket()]
).run()
ChangeReload(server_config, target=server.run, sockets=[server_config.bind_socket()]).run()
else:

blitz_api = create_blitz_api(
blitz_app, enable_config_route=config_route, admin=admin
)
uvicorn.run(
blitz_api, host="localhost", port=port, log_config=None, log_level="warning"
)
blitz_api = create_blitz_api(blitz_app, enable_config_route=config_route, admin=admin)
uvicorn.run(blitz_api, host="localhost", port=port, log_config=None, log_level="warning")
28 changes: 7 additions & 21 deletions blitz/models/blitz/file.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Any, ClassVar, NoReturn
from pydantic import BaseModel, Field, ValidationError, field_serializer
from pydantic import BaseModel, Field, field_serializer
from blitz.models.blitz.config import BlitzAppConfig
from blitz.models.blitz.resource import BlitzResourceConfig
from pathlib import Path
Expand Down Expand Up @@ -35,9 +35,7 @@ class FileType(StrEnum):
RESOURCES_FIELD_NAME: ClassVar[str] = "resources"

config: BlitzAppConfig
resources_configs: list[BlitzResourceConfig] = Field(
default=[], serialization_alias=RESOURCES_FIELD_NAME
)
resources_configs: list[BlitzResourceConfig] = Field(default=[], serialization_alias=RESOURCES_FIELD_NAME)
raw_file: dict[str, Any] = Field(exclude=True)
path: Path | None = Field(default=None, exclude=True)
file_type: FileType | None = Field(default=None, exclude=True)
Expand All @@ -47,14 +45,10 @@ class FileType(StrEnum):
# blitz_file.write(self.model_dump_json)

@field_serializer("resources_configs")
def _serialize_resources_configs(
self, resources_configs: list[BlitzResourceConfig], _info: Any
) -> dict[str, Any]:
def _serialize_resources_configs(self, resources_configs: list[BlitzResourceConfig], _info: Any) -> dict[str, Any]:
serialized_resources_configs = {}
for resource_config in resources_configs:
serialized_resources_configs[resource_config.name] = (
resource_config.model_dump()
)
serialized_resources_configs[resource_config.name] = resource_config.model_dump()

return serialized_resources_configs

Expand Down Expand Up @@ -82,23 +76,15 @@ def from_dict(
resource_name: str
resource_config: dict[str, Any]

for resource_name, resource_config in blitz_file.get(
cls.RESOURCES_FIELD_NAME, {}
).items():
for resource_name, resource_config in blitz_file.get(cls.RESOURCES_FIELD_NAME, {}).items():
settings_fields = {}
fields = {}
for field_name, field_value in resource_config.items():
if field_name.startswith(BlitzResourceConfig.Settings.FIELD_PREFIX):
settings_fields[
field_name[len(BlitzResourceConfig.Settings.FIELD_PREFIX) :]
] = field_value
settings_fields[field_name[len(BlitzResourceConfig.Settings.FIELD_PREFIX) :]] = field_value
else:
fields[field_name] = field_value
resources_configs.append(
BlitzResourceConfig(
name=resource_name, fields=fields, settings=settings_fields
)
)
resources_configs.append(BlitzResourceConfig(name=resource_name, fields=fields, settings=settings_fields))

return cls(
config=blitz_file.get(cls.CONFIG_FIELD_NAME),
Expand Down
2 changes: 0 additions & 2 deletions blitz/ui/blitz_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ def current_project(self, project: str) -> None:
print(project)
self._current_project = project



@property
def current_app(self) -> BlitzApp | None:
return self._current_app
Expand Down
18 changes: 6 additions & 12 deletions blitz/ui/components/gpt_chat_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def action_buttons(self) -> None:
if self._dialog is None:
# TODO: handle error
raise Exception
ui.button(
icon="file_download", color="transparent", on_click=self._dialog.open
).props("dense flat size=xm color=grey")
ui.button(icon="file_download", color="transparent", on_click=self._dialog.open).props(
"dense flat size=xm color=grey"
)

def download_dialog(self) -> None:
with ui.dialog() as self._dialog, ui.card().classes("w-full px-4"):
Expand All @@ -95,9 +95,7 @@ def _download_json(self) -> None:
)

def _download_yaml(self) -> None:
ui.download(
str.encode(yaml.dump(self.json)), filename=self._get_filename("yaml")
)
ui.download(str.encode(yaml.dump(self.json)), filename=self._get_filename("yaml"))

def _get_filename(self, extension: str) -> str:
return f"{self.blitz_app_title.replace(' ', '_').replace('.', '_').lower()}.{extension}"
Expand Down Expand Up @@ -188,9 +186,7 @@ class UserQuestion(GPTChatComponent):
AVATAR_COLOR = "#a72bff"

def __init__(self, text: str = "") -> None:
super().__init__(
label=self.LABEL, text=text, icon=self.ICON, avatar_color=self.AVATAR_COLOR
)
super().__init__(label=self.LABEL, text=text, icon=self.ICON, avatar_color=self.AVATAR_COLOR)

def as_gpt_dict(self) -> ChatCompletionMessageParam:
return {
Expand All @@ -213,9 +209,7 @@ class GPTResponse(GPTChatComponent):
AVATAR_COLOR = "#74aa9c"

def __init__(self, text: str = "", text_is_finished: bool = False) -> None:
super().__init__(
label=self.LABEL, text=text, icon=self.ICON, avatar_color=self.AVATAR_COLOR
)
super().__init__(label=self.LABEL, text=text, icon=self.ICON, avatar_color=self.AVATAR_COLOR)
self._text_is_finished = text_is_finished
self.text_is_finished = text_is_finished

Expand Down
56 changes: 16 additions & 40 deletions blitz/ui/components/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,9 @@ def __init__(
self.blitz_ui = blitz_ui

self.dark_mode = ui.dark_mode(value=True)
self.home_link = (
f"dashboard/projects/{blitz_ui.current_project}"
if blitz_ui.current_project
else "projects"
)
self.home_link = f"dashboard/projects/{blitz_ui.current_project}" if blitz_ui.current_project else "projects"
self.drawer = drawer
ui.add_head_html(
f"<style>{(Path(__file__).parent.parent / 'static' / 'style.css').read_text()}</style>"
)
ui.add_head_html(f"<style>{(Path(__file__).parent.parent / 'static' / 'style.css').read_text()}</style>")

ui.add_head_html(
f"<style>{(Path(__file__).parent.parent / 'static' / 'jse_theme_dark.css').read_text()}</style>"
Expand All @@ -51,30 +45,22 @@ def __init__(
)

def render(self) -> None:
with ui.header(bordered=True).classes(
"pl-1 pr-8 justify-between content-center h-16 backdrop-blur-sm"
):
with ui.header(bordered=True).classes("pl-1 pr-8 justify-between content-center h-16 backdrop-blur-sm"):
with ui.row().classes("items-center space-x-20 content-center my-auto"):
with ui.row().classes("items-center space-x-0 content-center "):
if self.drawer is not None:
ui.button(icon="menu", on_click=self.drawer.toggle).props(
"flat"
)
ui.button(icon="menu", on_click=self.drawer.toggle).props("flat")
ui.icon(name="bolt", color=DARK_PINK, size="32px")
with ui.link(target=f"/projects/{self.blitz_ui.current_project}"):
ui.label("Blitz Dashboard")

with ui.row().classes("items-center justify-between content-center"):
with ui.link(
target=f"{self.blitz_ui.localhost_url}/projects"
).classes("disabled"):
with ui.link(target=f"{self.blitz_ui.localhost_url}/projects").classes("disabled"):
ui.tooltip("Multiple App management is coming soon")
ui.label("Projects")
with ui.link(target="/gpt"):
ui.label("GPT Builder")
with ui.link(
target="https://paperz-org.github.io/blitz/", new_tab=True
):
with ui.link(target="https://paperz-org.github.io/blitz/", new_tab=True):
ui.label("Documentation")
with ui.row().classes("items-center content-center my-auto"):
with ui.element():
Expand All @@ -91,12 +77,8 @@ def render(self) -> None:
self.dark_mode, "value", value=True
)
ui.tooltip("White mode is coming soon")
with ui.link(
target="https://github.com/Paperz-org/blitz", new_tab=True
).classes(" w-8"):
ui.image(
Path(__file__).parent.parent / "./assets/github_white.png"
).classes("w-8 ")
with ui.link(target="https://github.com/Paperz-org/blitz", new_tab=True).classes(" w-8"):
ui.image(Path(__file__).parent.parent / "./assets/github_white.png").classes("w-8 ")


class MenuLink:
Expand All @@ -106,11 +88,9 @@ def __init__(self, label: str, link: str, icon: str) -> None:
self.icon = icon

def render(self) -> None:
with ui.link(target=self.link).classes("w-full"), ui.button(
on_click=self.go_to
).props("flat align=left").classes(
"px-4 hover:bg-slate-700 rounded-sm w-full"
) as self.button:
with ui.link(target=self.link).classes("w-full"), ui.button(on_click=self.go_to).props(
"flat align=left"
).classes("px-4 hover:bg-slate-700 rounded-sm w-full") as self.button:
ui.icon(name=self.icon, size="sm").props("flat").classes("pr-4")
ui.label(self.label)

Expand All @@ -133,20 +113,16 @@ def __init__(
self.drawer: LeftDrawer | None = None

def left_drawer(self) -> None:
with ui.left_drawer(
value=self.drawer_open, fixed=True, bottom_corner=True
).props("width=200").classes("px-0 bg-[#14151a]") as self.drawer:
MenuLink(
"Dashboard", f"/projects/{self.current_project}", "dashboard"
).render()
with ui.left_drawer(value=self.drawer_open, fixed=True, bottom_corner=True).props("width=200").classes(
"px-0 bg-[#14151a]"
) as self.drawer:
MenuLink("Dashboard", f"/projects/{self.current_project}", "dashboard").render()
MenuLink(
"Admin",
f"{self.blitz_ui.localhost_url}/admin/",
"table_chart",
).render()
MenuLink(
"Swagger", f"/projects/{self.current_project}/swagger", "api"
).render()
MenuLink("Swagger", f"/projects/{self.current_project}/swagger", "api").render()
MenuLink(
"Blitz File",
f"/projects/{self.current_project}/blitz-file",
Expand Down
33 changes: 8 additions & 25 deletions blitz/ui/pages/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,16 @@


class ProjectDetailComponent(BaseComponent):

def render(self) -> None:
with ui.row().classes("w-full justify-between items-center"):
if self.current_app is None:
# TODO handle error
raise Exception
ui.label(f"{self.current_app.file.config.name}").classes(
"text-xl font-bold"
)
ui.label(f"Version: {self.current_app.file.config.version}").classes(
"font-bold text-sm"
)
ui.label(f"{self.current_app.file.config.name}").classes("text-xl font-bold")
ui.label(f"Version: {self.current_app.file.config.version}").classes("font-bold text-sm")
ui.separator()
ui.label(f"Project Path: {self.current_app.path}").classes("text-sm")
ui.label(f"Description: {self.current_app.file.config.description}").classes(
"text-sm font-normal"
)
ui.label(f"Description: {self.current_app.file.config.description}").classes("text-sm font-normal")


class DashboardPage(BasePage):
Expand All @@ -36,22 +29,12 @@ def setup(self) -> None:
def render(self) -> None:
with ui.element("div").classes("w-full h-full flex flex-row justify-center"):
with ui.column().classes("w-2/3 h-full border rounded-lg border-gray-300"):
with ui.expansion("Project", value=True, icon="info").classes(
"w-full text-bold text-2xl "
):
with ui.expansion("Project", value=True, icon="info").classes("w-full text-bold text-2xl "):
ProjectDetailComponent().render()
with ui.expansion("Resources", value=True, icon="help_outline").classes(
"w-full text-bold text-2xl"
):
ui.table(
columns=self.columns, rows=self.rows, row_key="name"
).classes("w-full no-shadow")
with ui.expansion(
"Status", value=True, icon="health_and_safety"
).classes("w-full text-bold text-2xl"):
with ui.expansion("Resources", value=True, icon="help_outline").classes("w-full text-bold text-2xl"):
ui.table(columns=self.columns, rows=self.rows, row_key="name").classes("w-full no-shadow")
with ui.expansion("Status", value=True, icon="health_and_safety").classes("w-full text-bold text-2xl"):
# See https://github.com/zauberzeug/nicegui/issues/2174
StatusComponent().render() # type: ignore
with ui.expansion("Logs", value=False, icon="list").classes(
"w-full text-bold text-2xl"
):
with ui.expansion("Logs", value=False, icon="list").classes("w-full text-bold text-2xl"):
LogComponent().render()
9 changes: 2 additions & 7 deletions blitz/ui/pages/diagram.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from nicegui import ui
from blitz.ui.components.header import FrameComponent
from blitz.ui.pages.base import BasePage


Expand Down Expand Up @@ -46,9 +45,5 @@ def render(self) -> None:
raise Exception
ui.mermaid(self.blitz_ui.erd)
with ui.footer().classes("w-full justify-start "):
ui.button(icon="zoom_in", on_click=self.zoom_svg).classes(
"borderrounded-sm"
).props("flat")
ui.button(icon="zoom_out", on_click=self.unzoom_svg).classes(
"border rounded-sm"
).props("flat")
ui.button(icon="zoom_in", on_click=self.zoom_svg).classes("borderrounded-sm").props("flat")
ui.button(icon="zoom_out", on_click=self.unzoom_svg).classes("border rounded-sm").props("flat")
Loading

0 comments on commit 8d50a9a

Please sign in to comment.