Skip to content

Commit

Permalink
Fix hot reloading for demo when using bazel CLI (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwillchen authored May 16, 2024
1 parent 9650cdf commit 45ed529
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 76 deletions.
2 changes: 2 additions & 0 deletions demo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ class Section:
),
]

ALL_SECTIONS = FIRST_SECTIONS + COMPONENTS_SECTIONS

BORDER_SIDE = me.BorderSide(
style="solid",
width=1,
Expand Down
26 changes: 26 additions & 0 deletions mesop/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,28 @@
"prod", False, "set to true for prod mode; otherwise editor mode."
)
flags.DEFINE_bool("verbose", False, "set to true for verbose logging.")
flags.DEFINE_bool(
"reload_demo_modules", False, "set to true to reload demo modules."
)


def execute_main_module():
module_name = get_module_name_from_runfile_path(FLAGS.path)
clear_app_modules(module_name=module_name)
if FLAGS.reload_demo_modules:
clear_demo_modules()
execute_module(
module_path=get_runfile_location(FLAGS.path),
module_name=module_name,
)


def clear_demo_modules():
for module in demo_modules:
if module in sys.modules:
del sys.modules[module]


def monitor_stdin():
while True:
line = sys.stdin.readline().strip()
Expand All @@ -63,6 +74,9 @@ def monitor_stdin():
hot_reload_finished()


demo_modules = ["demo", "demo.main"]


def main(argv: Sequence[str]):
flask_app = configure_flask_app(prod_mode=FLAGS.prod)
if len(FLAGS.path) < 1:
Expand All @@ -71,6 +85,18 @@ def main(argv: Sequence[str]):
if not FLAGS.prod:
enable_debug_mode()

if FLAGS.reload_demo_modules:
# If we need to reload demo modules, collect all the modules
# reference demo/main.py.
#
# Because the imports in demo/ do not follow the standard import
# convention used by the rest of the Bazel workspace, we use
# a list of the modules stored as a constant in main.py.
from demo import main

for section in main.ALL_SECTIONS:
for example in section.examples:
demo_modules.append(example.name)
try:
execute_main_module()
except Exception as e:
Expand Down
3 changes: 0 additions & 3 deletions mesop/examples/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from demo import main as main
from mesop.examples import buttons as buttons
from mesop.examples import chat as chat
from mesop.examples import checkbox_and_radio as checkbox_and_radio
from mesop.examples import composite as composite
from mesop.examples import docs as docs
Expand All @@ -20,7 +19,5 @@
from mesop.examples import readme_app as readme_app
from mesop.examples import sxs as sxs
from mesop.examples import testing as testing
from mesop.examples import text_to_image as text_to_image
from mesop.examples import text_to_text as text_to_text

# Do not import error_state_missing_init_prop because it cause all examples to fail.
40 changes: 0 additions & 40 deletions mesop/examples/chat.py

This file was deleted.

14 changes: 0 additions & 14 deletions mesop/examples/text_to_image.py

This file was deleted.

18 changes: 0 additions & 18 deletions mesop/examples/text_to_text.py

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/cli.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
(lsof -t -i:32123 | xargs kill) || true && \
ibazel run //mesop/cli -- --path="mesop/mesop/example_index.py"
ibazel run //mesop/cli -- --path="mesop/mesop/example_index.py" --reload_demo_modules

0 comments on commit 45ed529

Please sign in to comment.