Skip to content

Commit

Permalink
Use default factory to in notes for markdown demo (#393)
Browse files Browse the repository at this point in the history
* Use default factory to in notes for markdown demo

* Fix type checking issue
richard-to authored Jun 9, 2024
1 parent 6d63ba0 commit fa17bcd
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions demo/markdown_editor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from dataclasses import dataclass
from dataclasses import dataclass, field

import mesop as me

_INTRO_TEXT = """
# Mesop Markdown Editor Example
This is example shows how to make a simple markdown editor.
""".strip()


@dataclass(kw_only=True)
class Note:
@@ -12,9 +18,9 @@ class Note:

@me.stateclass
class State:
notes: list[Note]
selected_note_index: int
selected_note_content: str
notes: list[Note] = field(default_factory=lambda: [Note(content=_INTRO_TEXT)])
selected_note_index: int = 0
selected_note_content: str = _INTRO_TEXT
show_preview: bool = True


@@ -28,12 +34,6 @@ class State:
def page():
state = me.state(State)

# On load, add a starter note.
if not state.notes:
state.notes.append(Note(content=_INTRO_TEXT))
state.selected_note_content = state.notes[0].content
state.selected_note_index = 0

with me.box(style=_style_container(state.show_preview)):
# Note list column
with me.box(style=_STYLE_NOTES_NAV):
@@ -83,13 +83,6 @@ def _render_note_excerpt(content: str) -> str:
return content[:_EXCERPT_CHAR_LIMIT] + "..."


_INTRO_TEXT = """
# Mesop Markdown Editor Example
This is example shows how to make a simple markdown editor.
""".strip()


# EVENT HANDLERS


0 comments on commit fa17bcd

Please sign in to comment.