22
33from __future__ import annotations
44
5- import ast
65import asyncio
76import copy
87import functools
@@ -156,6 +155,17 @@ class BaseReloader(ABC):
156155 def running_app (self ) -> App :
157156 pass
158157
158+ def get_attribute (self , attr : str , demo ) -> Any :
159+ if (
160+ hasattr (demo , f"_deprecated_{ attr } " )
161+ and getattr (demo , f"_deprecated_{ attr } " ) is not None
162+ ):
163+ return getattr (demo , f"_deprecated_{ attr } " )
164+ elif hasattr (demo , attr ) and getattr (demo , attr ) is not None :
165+ return getattr (demo , attr )
166+ else :
167+ return getattr (self .running_app .blocks , attr )
168+
159169 def swap_blocks (self , demo : Blocks ):
160170 assert self .running_app .blocks # noqa: S101
161171 # Copy over the blocks to get new components and events but
@@ -166,11 +176,13 @@ def swap_blocks(self, demo: Blocks):
166176 demo .is_running = True
167177 demo .allowed_paths = self .running_app .blocks .allowed_paths
168178 demo .blocked_paths = self .running_app .blocks .blocked_paths
169- demo .theme = self .running_app .blocks .theme
170- demo .head_paths = self .running_app .blocks .head_paths
171- demo .css = self .running_app .blocks .css
172- demo .head = self .running_app .blocks .head
173- demo .css_paths = self .running_app .blocks .css_paths
179+
180+ demo .theme = self .get_attribute ("theme" , demo )
181+ demo .css = self .get_attribute ("css" , demo )
182+ demo .css_paths = self .get_attribute ("css_paths" , demo )
183+ demo .js = self .get_attribute ("js" , demo )
184+ demo .head = self .get_attribute ("head" , demo )
185+ demo .head_paths = self .get_attribute ("head_paths" , demo )
174186 demo ._set_html_css_theme_variables ()
175187 self .running_app .state_holder .set_blocks (demo )
176188 for session in self .running_app .state_holder .session_data .values ():
@@ -295,51 +307,6 @@ def swap_blocks(self, demo: Blocks):
295307 self .alert_change ("reload" )
296308
297309
298- def _remove_if_name_main_codeblock (file_path : str , encoding : str = "utf-8" ):
299- """Parse the file, remove the gr.no_reload code blocks, and write the file back to disk.
300-
301- Parameters:
302- file_path (str): The path to the file to remove the no_reload code blocks from.
303- """
304-
305- with open (file_path , encoding = encoding ) as file :
306- code = file .read ()
307-
308- tree = ast .parse (code )
309-
310- def _is_if_name_main (expr : ast .AST ) -> bool :
311- """Find the if __name__ == '__main__': block."""
312- return (
313- isinstance (expr , ast .If )
314- and isinstance (expr .test , ast .Compare )
315- and isinstance (expr .test .left , ast .Name )
316- and expr .test .left .id == "__name__"
317- and len (expr .test .ops ) == 1
318- and isinstance (expr .test .ops [0 ], ast .Eq )
319- and isinstance (expr .test .comparators [0 ], ast .Constant )
320- and expr .test .comparators [0 ].value == "__main__"
321- )
322-
323- # Find the positions of the code blocks to load
324- for node in ast .walk (tree ):
325- if _is_if_name_main (node ):
326- assert isinstance (node , ast .If ) # noqa: S101
327- node .body = [ast .Pass (lineno = node .lineno , col_offset = node .col_offset )]
328-
329- # convert tree to string
330- code_removed = compile (tree , filename = file_path , mode = "exec" )
331- return code_removed
332-
333-
334- def _find_module (source_file : Path ) -> ModuleType | None :
335- for s , v in sys .modules .items ():
336- if s not in {"__main__" , "__mp_main__" } and getattr (v , "__file__" , None ) == str (
337- source_file
338- ):
339- return v
340- return None
341-
342-
343310def watchfn_spaces (reloader : SpacesReloader ):
344311 try :
345312 spaces_version = importlib .metadata .version ("spaces" )
@@ -423,8 +390,8 @@ def is_in_watch_dirs_and_not_sitepackages(file_path):
423390 # Need to import the module in this thread so that the
424391 # module is available in the namespace of this thread
425392 module = reloader .watch_module
426- no_reload_source_code = _remove_if_name_main_codeblock (
427- str ( reloader . demo_file ), encoding = reloader .encoding
393+ no_reload_source_code = Path ( str ( reloader . demo_file )). read_text (
394+ encoding = reloader .encoding
428395 )
429396 # Reset the context to id 0 so that the loaded module is the same as the original
430397 # See https://github.com/gradio-app/gradio/issues/10253
@@ -457,8 +424,8 @@ def is_in_watch_dirs_and_not_sitepackages(file_path):
457424
458425 NO_RELOAD .set (False )
459426 # Remove the gr.no_reload code blocks and exec in the new module's dict
460- no_reload_source_code = _remove_if_name_main_codeblock (
461- str ( reloader . demo_file ), encoding = reloader .encoding
427+ no_reload_source_code = Path ( str ( reloader . demo_file )). read_text (
428+ encoding = reloader .encoding
462429 )
463430 exec (no_reload_source_code , module .__dict__ )
464431
0 commit comments