Skip to content

Commit

Permalink
Fixes Blocks exit issue (#3600)
Browse files Browse the repository at this point in the history
* fix

* changelog

* blocks

* formatting
  • Loading branch information
abidlabs authored Mar 24, 2023
1 parent 9d3f58b commit 7a4e22b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- Fixed bug where text for altair plots was not legible in dark mode by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 3555](https://github.com/gradio-app/gradio/pull/3555)
- Fixes `Chatbot` and `Image` components so that files passed during processing are added to a directory where they can be served from, by [@abidlabs](https://github.com/abidlabs) in [PR 3523](https://github.com/gradio-app/gradio/pull/3523)
- Fixes an an issue where if the Blocks scope was not exited, then State could be shared across sessions, by [@abidlabs](https://github.com/abidlabs) in [PR 3600](https://github.com/gradio-app/gradio/pull/3600)
- Fixed bug where "or" was not being localized in file upload text by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 3599](https://github.com/gradio-app/gradio/pull/3599)

## Documentation Changes:
Expand Down
5 changes: 5 additions & 0 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,7 @@ def __enter__(self):
Context.root_block = self
self.parent = Context.block
Context.block = self
self.exited = False
return self

def __exit__(self, *args):
Expand All @@ -1163,6 +1164,7 @@ def __exit__(self, *args):
self.config = self.get_config_file()
self.app = routes.App.create_app(self)
self.progress_tracking = any(block_fn.tracks_progress for block_fn in self.fns)
self.exited = True

@class_or_instancemethod
def load(
Expand Down Expand Up @@ -1384,6 +1386,9 @@ def reverse(text):
demo = gr.Interface(reverse, "text", "text")
demo.launch(share=True, auth=("username", "password"))
"""
if not self.exited:
self.__exit__()

self.dev_mode = False
if (
auth
Expand Down
7 changes: 7 additions & 0 deletions test/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys
import time
import unittest.mock as mock
import uuid
import warnings
from contextlib import contextmanager
from functools import partial
Expand Down Expand Up @@ -393,6 +394,12 @@ def test_use_default_theme_as_fallback(self, mock_from_hub):
with gr.Blocks(theme="freddyaboulton/this-theme-does-not-exist") as demo:
assert demo.theme.to_dict() == gr.themes.Default().to_dict()

def test_exit_called_at_launch(self):
with gr.Blocks() as demo:
gr.Textbox(uuid.uuid4)
demo.launch(prevent_thread_lock=True)
assert len(demo.get_config_file()["dependencies"]) == 1


class TestComponentsInBlocks:
def test_slider_random_value_config(self):
Expand Down

0 comments on commit 7a4e22b

Please sign in to comment.