Skip to content

Commit

Permalink
fix conflict from main
Browse files Browse the repository at this point in the history
  • Loading branch information
mde-pach committed Feb 20, 2024
2 parents d355e7b + 8f9d885 commit beb4c38
Show file tree
Hide file tree
Showing 18 changed files with 180 additions and 165 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
19 changes: 14 additions & 5 deletions blitz/cli/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ class BlitzProjectCreator:
DEMO_BLITZ_APP_DESCRIPTION = "This is a demo blitz app"
DEMO_BLITZ_APP_NAME = "Demo Blitz App"

def __init__(self, name: str, description: str, file_format: str, demo: bool = False) -> None:
def __init__(
self, name: str, description: str, file_format: str, demo: bool = False
) -> None:
self.name = name
self.description = description
self.file_format = file_format
Expand Down Expand Up @@ -93,7 +95,9 @@ def create_file_or_exit(self) -> None:
raise typer.Exit(code=1)

def print_success_message(self) -> None:
print(f"\n[medium_purple1 bold]{self.name}[/medium_purple1 bold] created successfully !")
print(
f"\n[medium_purple1 bold]{self.name}[/medium_purple1 bold] created successfully !"
)
print("To start your app, you can use:")
print(f" [bold medium_purple1]blitz start {self.path}[/bold medium_purple1]")

Expand Down Expand Up @@ -123,7 +127,9 @@ def _write_blitz_file(self) -> Path:
raise Exception()
match self.file_format:
case "json":
blitz_file_data = self.blitz_file.model_dump_json(indent=4, by_alias=True, exclude_unset=True)
blitz_file_data = self.blitz_file.model_dump_json(
indent=4, by_alias=True, exclude_unset=True
)
case "yaml":
blitz_file_data = yaml.dump(
self.blitz_file.model_dump(by_alias=True, exclude_unset=True),
Expand All @@ -145,7 +151,9 @@ def _print_file_error(error: Exception) -> None:

@staticmethod
def _print_directory_error(error: Exception) -> None:
print(f"[red bold]Error[/red bold] while creating the blitz app in the file system: {error}")
print(
f"[red bold]Error[/red bold] while creating the blitz app in the file system: {error}"
)


def create_blitz_app(
Expand All @@ -155,12 +163,13 @@ def create_blitz_app(
] = None,
demo: Annotated[bool, typer.Option(help="Create a demo blitz app")] = False,
) -> None:
name = blitz_app_name
if demo:
name = BlitzProjectCreator.DEMO_BLITZ_APP_NAME
description = BlitzProjectCreator.DEMO_BLITZ_APP_DESCRIPTION
file_format = BlitzProjectCreator.DEFAULT_BLITZ_FILE_FORMAT
else:
if not blitz_app_name:
if not name:
# Interactive prompt to create a new blitz app
name = prompt.Prompt.ask(
"Enter the name of your blitz app",
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")
1 change: 1 addition & 0 deletions blitz/models/blitz/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def from_dict(
resources_configs: list[BlitzResourceConfig] = []
resource_name: str
resource_config: dict[str, Any]

for resource_name, resource_config in blitz_file.get(cls.RESOURCES_FIELD_NAME, {}).items():
settings_fields = {}
fields = {}
Expand Down
5 changes: 5 additions & 0 deletions blitz/ui/blitz_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def __init__(self, settings: Settings = get_settings()) -> None:
def current_project(self) -> str | None:
return self._current_project

@current_project.setter
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
13 changes: 8 additions & 5 deletions blitz/ui/components/base.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from typing import Any, Self, Tuple, TypeVar
from blitz.ui.blitz_ui import get_blitz_ui

from typing import Any, Self, TypeVar
from blitz.ui.blitz_ui import BlitzUI, get_blitz_ui

T = TypeVar("T", bound="BaseComponent")


class BaseComponent:
def __init__(self, *args: Any, props: str = "", classes: str = "", **kwargs: Any) -> None:
def __init__(self, *args: Any, props: str = "", classes: str = "", blitz_ui: BlitzUI = get_blitz_ui(), **kwargs: Any) -> None:
self.props: str
self.classes: str
self.blitz_ui = blitz_ui
self.current_project = blitz_ui.current_project
self.current_app = blitz_ui.current_app

if hasattr(self, "props"):
self.props = f"{self.props} {props}"
Expand All @@ -19,7 +23,6 @@ def __init__(self, *args: Any, props: str = "", classes: str = "", **kwargs: Any
self.classes = classes

self.blitz_ui = get_blitz_ui()
self.render()

def render(self) -> None:
raise NotImplementedError
Expand All @@ -46,4 +49,4 @@ def variant(cls, name: str, *, props: str = "", classes: str = "", **kwargs: Any
"props": props,
"classes": classes,
},
)
)
8 changes: 5 additions & 3 deletions blitz/ui/components/gpt_chat_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ class GPTResponse(GPTChatComponent):

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)
self._text_is_finished = False
self.text_is_finished = text_is_finished
self._text_is_finished = text_is_finished

def add(self, text: str) -> None:
self.text += text
Expand Down Expand Up @@ -252,4 +251,7 @@ def split_response(text: str) -> list[Any]:

@classmethod
def from_gpt_dict(cls, gpt_dict: dict[str, Any]) -> "GPTChatComponent":
return cls(text=gpt_dict.get("content", ""), text_is_finished=gpt_dict.get("text_is_finished", True))
return cls(
text=gpt_dict.get("content", ""),
text_is_finished=gpt_dict.get("text_is_finished", False),
)
16 changes: 10 additions & 6 deletions blitz/ui/components/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from nicegui.page_layout import LeftDrawer
from blitz.ui.blitz_ui import BlitzUI, get_blitz_ui
from blitz.ui.components.buttons import FlatButton
from blitz.ui.components.base import BaseComponent

MAIN_PINK = "#cd87ff"
DARK_PINK = "#a72bff"
Expand All @@ -18,7 +19,12 @@ def render(self) -> None:


class HeaderComponent:
def __init__(self, title: str = "", blitz_ui: BlitzUI = get_blitz_ui(), drawer: LeftDrawer | None = None) -> None:
def __init__(
self,
title: str = "",
blitz_ui: BlitzUI = get_blitz_ui(),
drawer: LeftDrawer | None = None,
) -> None:
self.title = title
self.blitz_ui = blitz_ui

Expand Down Expand Up @@ -93,20 +99,18 @@ def go_to(self) -> None:
ui.open(self.link)


class FrameComponent:
class FrameComponent(BaseComponent):
def __init__(
self,
blitz_ui: BlitzUI = get_blitz_ui(),
show_drawer: bool = True,
drawer_open: bool = True,
) -> None:
self.blitz_ui = blitz_ui
self.current_project = blitz_ui.current_project
self.show_drawer = show_drawer
self.drawer_open = drawer_open

# Only for declarative
self.drawer: LeftDrawer | None = None
super().__init__()

def left_drawer(self) -> None:
with ui.left_drawer(value=self.drawer_open, fixed=True, bottom_corner=True).props("width=200").classes(
Expand All @@ -132,6 +136,6 @@ def left_drawer(self) -> None:
MenuLink("Logs", f"/projects/{self.current_project}/logs", "list").render()

def render(self) -> None:
if self.show_drawer and self.current_project is not None:
if self.show_drawer and self.blitz_ui.current_project is not None:
self.left_drawer()
HeaderComponent(drawer=self.drawer).render()
85 changes: 21 additions & 64 deletions blitz/ui/main.py
Original file line number Diff line number Diff line change
@@ -1,76 +1,32 @@
import logging
from typing import TYPE_CHECKING, Annotated

from blitz.ui.blitz_ui import BlitzUI, get_blitz_ui
from blitz.ui.pages.blitz_file import BlitzFilePage
from blitz.ui.pages.dashboard import DashboardPage
from blitz.ui.pages.diagram import MermaidPage
from blitz.ui.pages.gpt_builder import AskGPTPage
from blitz.ui.pages.log import LogPage
from blitz.ui.pages.swagger import SwaggerPage
from typing import TYPE_CHECKING
from blitz.ui.blitz_ui import get_blitz_ui
from blitz.ui.pages import (
BlitzFilePage,
DashboardPage,
MermaidPage,
AskGPTPage,
LogPage,
SwaggerPage,
)
from pathlib import Path
from blitz.ui.router import BlitzRouter
from nicegui import ui

from nicegui import app

from blitz.ui.components.header import FrameComponent


from fastapi import Depends, Request


if TYPE_CHECKING:
from blitz.api.blitz_api import BlitzAPI


async def _post_dark_mode(request: Request) -> None:
print("dark mode")
app.storage.browser["dark_mode"] = (await request.json()).get("value")


@ui.page("/projects/{uuid}", title="Dashboard")
def dashboard_page(uuid: str, blitz_ui: Annotated[BlitzUI, Depends(get_blitz_ui)]) -> None:
DashboardPage().render_page()
FrameComponent().render()


@ui.page("/projects/{uuid}/diagram", title="Diagram")
def diagram_page(uuid: str, blitz_ui: Annotated[BlitzUI, Depends(get_blitz_ui)]) -> None:
MermaidPage().render_page()
FrameComponent().render()


@ui.page("/projects/{uuid}/blitz-file", title="Blitz File")
def blitz_file_page(uuid: str, blitz_ui: Annotated[BlitzUI, Depends(get_blitz_ui)]) -> None:
BlitzFilePage().render_page()
FrameComponent().render()


@ui.page("/projects/{uuid}/swagger", title="Swagger")
def swagger_page(uuid: str, blitz_ui: Annotated[BlitzUI, Depends(get_blitz_ui)]) -> None:
SwaggerPage().render_page()
FrameComponent().render()


@ui.page("/projects/{uuid}/logs")
def log_page(uuid: str, blitz_ui: Annotated[BlitzUI, Depends(get_blitz_ui)]) -> None:
ui.page_title("Logs")
LogPage().render_page()
FrameComponent().render()


# @ui.page("/projects/{uuid}/admin")
# def log_page(uuid: str, blitz_ui: Annotated[BlitzUI, Depends(get_blitz_ui)]) -> None:
# ui.page_title("Admin")
# AdminPage().render_page()
# FrameComponent(drawer_open=False).render()

def init_routers() -> None:
router = BlitzRouter("/")
router.add_page("gpt", AskGPTPage)

@ui.page("/gpt")
def ask_gpt_page(blitz_ui: Annotated[BlitzUI, Depends(get_blitz_ui)]) -> None:
ui.page_title("GPT Builder")
AskGPTPage().render_page()
FrameComponent(show_drawer=False).render()
project_router = BlitzRouter("/projects/{uuid}/")
project_router.add_page("logs", LogPage)
project_router.add_page("swagger", SwaggerPage)
project_router.add_page("blitz-file", BlitzFilePage)
project_router.add_page("diagram", MermaidPage)
project_router.add_page("", DashboardPage)


# @ui.page("/projects")
Expand All @@ -89,6 +45,7 @@ def init_ui(
logging.getLogger("niceGUI").setLevel(logging.WARNING)
blitz_ui = get_blitz_ui()
blitz_ui.current_app = blitz_api.blitz_app
init_routers()
ui.run_with(
app=blitz_api,
title=title,
Expand Down
8 changes: 8 additions & 0 deletions blitz/ui/pages/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from .blitz_file import BlitzFilePage
from .dashboard import DashboardPage
from .diagram import MermaidPage
from .gpt_builder import AskGPTPage
from .log import LogPage
from .swagger import SwaggerPage

__all__ = ["BlitzFilePage", "DashboardPage", "MermaidPage", "AskGPTPage", "LogPage", "SwaggerPage"]
Loading

0 comments on commit beb4c38

Please sign in to comment.