Skip to content

Commit

Permalink
Feature/playground (#26)
Browse files Browse the repository at this point in the history
* add blitz ui read only

* create read only mode for ui

* rename env
  • Loading branch information
pbrochar authored Mar 9, 2024
1 parent 72a7987 commit ecb5d6d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
BLITZ_PORT=8100
DEFAULT_FILE="blitz.json"
BLITZ_DB_TYPE="MEMORY"
BLITZ_OPENAI_API_KEY=""
BLITZ_OPENAI_API_KEY=""
BLITZ_READ_ONLY=False
1 change: 1 addition & 0 deletions blitz/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Config:
DEFAULT_FILE: str = "blitz.json"
BLITZ_DB_TYPE: DBTypes = DBTypes.SQLITE
BLITZ_OPENAI_API_KEY: str = ""
BLITZ_READ_ONLY: bool = False


@lru_cache()
Expand Down
5 changes: 1 addition & 4 deletions blitz/ui/blitz_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
from blitz.settings import Settings, get_settings
from blitz.tools.erd import generate_mermaid_erd

# @lru_cache
# def get_erd(app: BlitzApp) -> str:
# return generate_mermaid_erd(app._base_resource_model.metadata)


class BlitzUI:
def __init__(self, settings: Settings = get_settings()) -> None:
self.read_only = settings.BLITZ_READ_ONLY
self.blitz_app: BlitzCore = BlitzCore()
self.apps = self.blitz_app.apps
self.preprompt = self._get_preprompt()
Expand Down
13 changes: 9 additions & 4 deletions blitz/ui/components/json_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def __init__(
else:
self.content = content
self.mode = mode
self._read_only = True
self.read_only = blitz_ui.read_only
self._editor_read_only = True

async def get_data(self) -> None:
raw_content: dict[str, str] = await self.editor.run_editor_method("get")
Expand All @@ -56,8 +57,8 @@ async def get_data(self) -> None:
app.storage.user["blitz_file_content"] = self.content

def enable_editor(self) -> None:
self._read_only = not self._read_only
self.editor.run_editor_method("updateProps", {"readOnly": self._read_only})
self._editor_read_only = not self._editor_read_only
self.editor.run_editor_method("updateProps", {"readOnly": self._editor_read_only})

def reset_content(self) -> None:
self.content = self._original_content
Expand All @@ -75,6 +76,9 @@ def validate(self) -> None:
notify.success("Valid Blitz File")

def save(self) -> None:
if self.read_only:
notify.error("Read Only Mode")
return
try:
BlitzFile.from_dict(self.content)
except ValidationError:
Expand Down Expand Up @@ -104,12 +108,13 @@ def render(self) -> None:
FlatButton("Reset", on_click=self.reset_content, icon="restart_alt")
with JustifyBetweenRow():
FlatButton("Validate", on_click=self.validate, icon="verified")

FlatButton("Save", on_click=self.save, icon="save")
self.editor = (
ui.json_editor(
{
"content": {"json": self.content},
"readOnly": self._read_only,
"readOnly": self._editor_read_only,
"mode": self.mode,
},
on_change=self.get_data,
Expand Down
12 changes: 9 additions & 3 deletions blitz/ui/components/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,29 @@

class LogComponent(BaseComponent[ui.log]):
class LogHandler(InterceptHandler):
ANONYMISED_MESSAGE = "[ANONYMISED] *****"

"""A logging handler that emits messages to a log element."""

def __init__(self, log: ui.log, level: int = logging.NOTSET) -> None:
def __init__(self, log: ui.log, level: int = logging.NOTSET, is_anonymised: bool = False) -> None:
self.log = log
self._is_anonymised = is_anonymised
super().__init__(level)

def emit(self, record: logging.LogRecord) -> None:
try:
if record.name != "uvicorn.access.ui":
self.log.push(record.getMessage())
message = record.getMessage() if not self._is_anonymised else self.ANONYMISED_MESSAGE
self.log.push(message)
except Exception:
self.handleError(record)

def __init__(self) -> None:
self._logger = logging.getLogger("uvicorn.access")
self._anonymize_log = self.blitz_ui.read_only
super().__init__()


def render(self) -> None:
self.ng = ui.log(max_lines=None).classes("w-full h-64 text-sm")
self._logger.addHandler(self.LogHandler(self.ng))
self._logger.addHandler(self.LogHandler(self.ng, is_anonymised=self._anonymize_log))

0 comments on commit ecb5d6d

Please sign in to comment.