Skip to content

Commit

Permalink
Fix isssue with running with nginx gradio-app#4133 (@abidlabs work re…
Browse files Browse the repository at this point in the history
…based to v3.23.0).
  • Loading branch information
zuazo committed May 10, 2023
1 parent dbd4f75 commit e2e12c3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
7 changes: 5 additions & 2 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def __init__(
self._skip_init_processing = _skip_init_processing
self._style = {}
self.parent: BlockContext | None = None
self.root = ""

if render:
self.render()
Expand Down Expand Up @@ -560,6 +559,8 @@ def __init__(

self.file_directories = []

self.root_path = ""

if self.analytics_enabled:
data = {
"mode": self.mode,
Expand Down Expand Up @@ -1114,7 +1115,6 @@ def get_config_file(self):
"show_api": self.show_api,
"is_colab": utils.colab_check(),
"stylesheets": self.stylesheets,
"root": self.root,
}

def getLayout(block):
Expand Down Expand Up @@ -1328,6 +1328,7 @@ def launch(
quiet: bool = False,
show_api: bool = True,
file_directories: List[str] | None = None,
root_path: str = "",
_frontend: bool = True,
) -> Tuple[FastAPI, str, str]:
"""
Expand Down Expand Up @@ -1358,6 +1359,7 @@ def launch(
quiet: If True, suppresses most print statements.
show_api: If True, shows the api docs in the footer of the app. Default True. If the queue is enabled, then api_open parameter of .queue() will determine if the api docs are shown, independent of the value of show_api.
file_directories: List of directories that gradio is allowed to serve files from (in addition to the directory containing the gradio python file). Must be absolute paths. Warning: any files in these directories or its children are potentially accessible to all users of your app.
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".
Returns:
app: FastAPI app object that is running the demo
local_url: Locally accessible link to the demo
Expand Down Expand Up @@ -1393,6 +1395,7 @@ def reverse(text):
self.height = height
self.width = width
self.favicon_path = favicon_path
self.root_path = root_path

if enable_queue is not None:
self.enable_queue = enable_queue
Expand Down
14 changes: 10 additions & 4 deletions gradio/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def configure_app(self, blocks: gradio.Blocks) -> None:
self.cwd = os.getcwd()
self.favicon_path = blocks.favicon_path
self.tokens = {}
self.root_path = blocks.root_path

def get_blocks(self) -> gradio.Blocks:
if self.blocks is None:
Expand Down Expand Up @@ -219,15 +220,17 @@ def login(form_data: OAuth2PasswordRequestForm = Depends()):
def main(request: fastapi.Request, user: str = Depends(get_current_user)):
mimetypes.add_type("application/javascript", ".js")
blocks = app.get_blocks()
root_path = request.scope.get("root_path")

if app.auth is None or not (user is None):
config = app.get_blocks().config
config["root"] = root_path
else:
config = {
"auth_required": True,
"auth_message": blocks.auth_message,
"is_space": app.get_blocks().is_space,
"root": app.get_blocks().root,
"root": root_path,
}

try:
Expand All @@ -252,8 +255,12 @@ def main(request: fastapi.Request, user: str = Depends(get_current_user)):

@app.get("/config/", dependencies=[Depends(login_check)])
@app.get("/config", dependencies=[Depends(login_check)])
def get_config():
return app.get_blocks().config
def get_config(request: fastapi.Request):
root_path = request.scope.get("root_path")
config = app.get_blocks().config
config["root"] = root_path
return config


@app.get("/static/{path:path}")
def static_resource(path: str):
Expand Down Expand Up @@ -756,7 +763,6 @@ def read_main():
# Then run `uvicorn run:app` from the terminal and navigate to http://localhost:8000/gradio.
"""
blocks.dev_mode = False
blocks.root = path[:-1] if path.endswith("/") else path
blocks.config = blocks.get_config_file()
gradio_app = App.create_app(blocks)

Expand Down

0 comments on commit e2e12c3

Please sign in to comment.