Refactor hot reload logic into separate module#1365
Open
richard-to wants to merge 2 commits intomainfrom
Open
Conversation
…ervers
Extract file-watcher logic from bin.py into a new mesop/server/hot_reload.py
module so it can be shared by both the mesop CLI and the WSGI integration.
Add a `watch_path` parameter to `create_wsgi_app()`: when provided alongside
`debug_mode=True`, a background watchdog thread is started immediately so that
hot reloading works when Mesop is mounted inside FastAPI, Starlette, Gunicorn,
or any other WSGI-compatible server.
Usage:
mesop_app = create_wsgi_app(debug_mode=True, watch_path=__file__)
Also add THIRD_PARTY_PY_WATCHDOG to build_defs/defaults.bzl and update the
server/BUILD and bin/BUILD targets accordingly.
https://claude.ai/code/session_01U3KhAZacaA9FpVT1jzGeYn
Dict keys that are Python tuples (e.g. (1, 2)) were not handled correctly in two places: 1. diff_state: DeepDiff incorrectly expanded the tuple into multiple path segments, producing ["val1", 1, 2] instead of the correct single element ["val1", "(1, 2)"]. The fix normalises both state objects with a new _normalize_state_for_diff helper before passing them to DeepDiff. The helper converts any non-JSON-scalar dict key (tuple, frozenset, etc.) to its str() representation so DeepDiff emits an unambiguous, single path element like "(1, 2)". 2. serialize_dataclass: json.dumps raised TypeError for dicts with tuple keys because JSON only supports string keys. The same normalisation is now applied to the asdict() output before encoding. The TypeScript applyStateDiff already handles arbitrary string path elements correctly, so no frontend changes are required beyond a new test that exercises the "(1, 2)" string-key path. https://claude.ai/code/session_01U3KhAZacaA9FpVT1jzGeYn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Extracted hot reload functionality from
mesop/bin/bin.pyinto a new dedicated modulemesop/server/hot_reload.pyto improve code organization and enable hot reload support in WSGI server environments.Key Changes
New module: Created
mesop/server/hot_reload.pycontaining:execute_main_module()- executes the main app module with error handlingstart_file_watcher()- starts a background daemon thread for file watchingfs_watcher()- filesystem watcher implementation using watchdogReloadEventHandler- handles file system events (modified, created, deleted)clear_app_modules,add_app_module,remove_app_module)Updated
mesop/bin/bin.py:mesop.server.hot_reload)start_file_watcher()instead of managing threads directlythreading,time,watchdog, etc.)execute_main_module()call to passprod_modeparameterEnhanced
mesop/server/wsgi_app.py:watch_pathparameter tocreate_wsgi_app()functiondebug_mode=Trueandwatch_pathis providedBuild configuration updates:
THIRD_PARTY_PY_WATCHDOGdependency definition inbuild_defs/defaults.bzlmesop/server/BUILDto include watchdog dependency and necessary importsmesop/bin/BUILDto depend on//mesop/serverinstead of individual modulesImplementation Details
prod_modeparameter is now threaded through the hot reload pipeline to control error reporting behaviorapp_modules.remove()toapp_modules.discard()for safer module removalhttps://claude.ai/code/session_01U3KhAZacaA9FpVT1jzGeYn