diff --git a/.changeset/big-wolves-grab.md b/.changeset/big-wolves-grab.md new file mode 100644 index 0000000000000..394d804e33701 --- /dev/null +++ b/.changeset/big-wolves-grab.md @@ -0,0 +1,5 @@ +--- +"gradio": minor +--- + +feat:solve how can I config root_path dynamically? #4968 diff --git a/gradio/blocks.py b/gradio/blocks.py index 8386ecf21fd5c..6c7372281104e 100644 --- a/gradio/blocks.py +++ b/gradio/blocks.py @@ -758,7 +758,7 @@ def __init__( self.allowed_paths = [] self.blocked_paths = [] - self.root_path = "" + self.root_path = os.environ.get("GRADIO_ROOT_PATH", "") self.root_urls = set() if not wasm_utils.IS_WASM and self.analytics_enabled: @@ -1690,7 +1690,7 @@ def launch( file_directories: list[str] | None = None, allowed_paths: list[str] | None = None, blocked_paths: list[str] | None = None, - root_path: str = "", + root_path: str | None = None, _frontend: bool = True, app_kwargs: dict[str, Any] | None = None, ) -> tuple[FastAPI, str, str]: @@ -1725,7 +1725,7 @@ def launch( file_directories: This parameter has been renamed to `allowed_paths`. It will be removed in a future version. allowed_paths: List of complete filepaths or parent directories that gradio is allowed to serve (in addition to the directory containing the gradio python file). Must be absolute paths. Warning: if you provide directories, any files in these directories or their subdirectories are accessible to all users of your app. blocked_paths: List of complete filepaths or parent directories that gradio is not allowed to serve (i.e. users of your app are not allowed to access). Must be absolute paths. Warning: takes precedence over `allowed_paths` and all other directories exposed by Gradio by default. - root_path: The root path (or "mount point") of the application, if it's not served from the root ("/") of the domain. Often used when the application is behind a reverse proxy that forwards requests to the application. For example, if the application is served at "https://example.com/myapp", the `root_path` should be set to "/myapp". + root_path: The root path (or "mount point") of the application, if it's not served from the root ("/") of the domain. Often used when the application is behind a reverse proxy that forwards requests to the application. For example, if the application is served at "https://example.com/myapp", the `root_path` should be set to "/myapp". Can be set by environment variable GRADIO_ROOT_PATH. Defaults to "". app_kwargs: Additional keyword arguments to pass to the underlying FastAPI app as a dictionary of parameter keys and argument values. For example, `{"docs_url": "/docs"}` Returns: app: FastAPI app object that is running the demo @@ -1766,7 +1766,10 @@ def reverse(text): self.width = width self.favicon_path = favicon_path self.ssl_verify = ssl_verify - self.root_path = root_path + if root_path is None: + self.root_path = os.environ.get("GRADIO_ROOT_PATH", "") + else: + self.root_path = root_path if enable_queue is not None: self.enable_queue = enable_queue