Skip to content

Commit

Permalink
Refactor NO_RELOAD implementation (#10088)
Browse files Browse the repository at this point in the history
* Refactor `NO_RELOAD` implementation

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
CNSeniorious000 and gradio-pr-bot authored Dec 6, 2024
1 parent 0248c6d commit cb5b891
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/legal-geese-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": minor
---

feat:Refactor `NO_RELOAD` implementation
35 changes: 19 additions & 16 deletions gradio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,21 @@ def swap_blocks(self, demo: Blocks):
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 _remove_no_reload_codeblocks(file_path: str):
def set(self, value: int):
self.value = bool(value)


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:
Expand All @@ -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 (
Expand All @@ -212,7 +213,7 @@ def _is_if_name_main(expr: ast.AST) -> bool:

# 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)]

Expand All @@ -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
Expand Down Expand Up @@ -275,7 +278,7 @@ def iter_py_files() -> Iterator[Path]:
# 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

Expand All @@ -294,7 +297,7 @@ def iter_py_files() -> Iterator[Path]:
# 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:
Expand All @@ -305,7 +308,7 @@ def iter_py_files() -> Iterator[Path]:
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__)
Expand Down

0 comments on commit cb5b891

Please sign in to comment.