Refactor NO_RELOAD implementation (#10088)

* Refactor `NO_RELOAD` implementation

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Muspi Merol 2024-12-06 23:54:05 +08:00 committed by GitHub
parent 0248c6d9d8
commit cb5b89108e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 16 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": minor
---
feat:Refactor `NO_RELOAD` implementation

View File

@ -172,10 +172,21 @@ class SourceFileReloader(BaseReloader):
self.alert_change("reload")
NO_RELOAD = True
class DynamicBoolean(int):
def __init__(self, value: int):
self.value = bool(value)
def __bool__(self):
return self.value
def set(self, value: int):
self.value = bool(value)
def _remove_no_reload_codeblocks(file_path: str):
NO_RELOAD = DynamicBoolean(True)
def _remove_if_name_main_codeblock(file_path: str):
"""Parse the file, remove the gr.no_reload code blocks, and write the file back to disk.
Parameters:
@ -187,16 +198,6 @@ def _remove_no_reload_codeblocks(file_path: str):
tree = ast.parse(code)
def _is_gr_no_reload(expr: ast.AST) -> bool:
"""Find with gr.no_reload context managers."""
return (
isinstance(expr, ast.If)
and isinstance(expr.test, ast.Attribute)
and isinstance(expr.test.value, ast.Name)
and expr.test.value.id == "gr"
and expr.test.attr == "NO_RELOAD"
)
def _is_if_name_main(expr: ast.AST) -> bool:
"""Find the if __name__ == '__main__': block."""
return (
@ -212,7 +213,7 @@ def _remove_no_reload_codeblocks(file_path: str):
# Find the positions of the code blocks to load
for node in ast.walk(tree):
if _is_gr_no_reload(node) or _is_if_name_main(node):
if _is_if_name_main(node):
assert isinstance(node, ast.If) # noqa: S101
node.body = [ast.Pass(lineno=node.lineno, col_offset=node.col_offset)]
@ -236,6 +237,8 @@ def watchfn(reloader: SourceFileReloader):
get_changes is taken from uvicorn's default file watcher.
"""
NO_RELOAD.set(False)
# The thread running watchfn will be the thread reloading
# the app. So we need to modify this thread_data attr here
# so that subsequent calls to reload don't launch the app
@ -275,7 +278,7 @@ def watchfn(reloader: SourceFileReloader):
# Need to import the module in this thread so that the
# module is available in the namespace of this thread
module = reloader.watch_module
no_reload_source_code = _remove_no_reload_codeblocks(str(reloader.demo_file))
no_reload_source_code = _remove_if_name_main_codeblock(str(reloader.demo_file))
exec(no_reload_source_code, module.__dict__)
sys.modules[reloader.watch_module_name] = module
@ -294,7 +297,7 @@ def watchfn(reloader: SourceFileReloader):
# changes to be reflected in the main demo file.
if changed.suffix == ".py":
changed_in_copy = _remove_no_reload_codeblocks(str(changed))
changed_in_copy = _remove_if_name_main_codeblock(str(changed))
if changed != reloader.demo_file:
changed_module = _find_module(changed)
if changed_module:
@ -305,7 +308,7 @@ def watchfn(reloader: SourceFileReloader):
if top_level_parent != changed_module:
importlib.reload(top_level_parent)
changed_demo_file = _remove_no_reload_codeblocks(
changed_demo_file = _remove_if_name_main_codeblock(
str(reloader.demo_file)
)
exec(changed_demo_file, module.__dict__)