From 42ad0cbe45e4814a8ce9b4278778e575b4073d22 Mon Sep 17 00:00:00 2001 From: Freddy Boulton Date: Wed, 1 Feb 2023 11:53:53 -0500 Subject: [PATCH] Enable gradio to work on kaggle (#3101) * enable share kaggle * Add test * CHANGELOG * Add image to changelog --- CHANGELOG.md | 8 ++++++++ gradio/blocks.py | 3 ++- gradio/utils.py | 6 ++++++ test/test_utils.py | 26 ++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0191271edf..39f49bb458 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,14 @@ Previously photos uploaded via iOS would be rotated after processing. This has b #### After ![image](https://user-images.githubusercontent.com/41651716/215846554-e41773ed-70f0-491a-9952-6a18babf91ef.png) +### Run on Kaggle kernels 🧪 + +A share link will automatically be created when running on Kaggle kernels (notebooks) so that +the front-end is properly displayed. + +![image](https://user-images.githubusercontent.com/41651716/216104254-2cf55599-449c-436c-b57e-40f6a83f9eee.png) + +By [@freddyaboulton](https://github.com/freddyaboulton) in [PR 3101](https://github.com/gradio-app/gradio/pull/3101) ## Bug Fixes: - Fix change event listener for JSON, HighlightedText, Chatbot by [@aliabid94](https://github.com/aliabid94) in [PR 3095](https://github.com/gradio-app/gradio/pull/3095) diff --git a/gradio/blocks.py b/gradio/blocks.py index de88893269..f9813f3609 100644 --- a/gradio/blocks.py +++ b/gradio/blocks.py @@ -1383,6 +1383,7 @@ class Blocks(BlockContext): self.server = server self.is_running = True self.is_colab = utils.colab_check() + self.is_kaggle = utils.kaggle_check() self.protocol = ( "https" if self.local_url.startswith("https") or self.is_colab @@ -1405,7 +1406,7 @@ class Blocks(BlockContext): share if share is not None else True - if self.is_colab and self.enable_queue + if (self.is_colab and self.enable_queue) or self.is_kaggle else False ) diff --git a/gradio/utils.py b/gradio/utils.py index 674154f4c3..9ac48152e9 100644 --- a/gradio/utils.py +++ b/gradio/utils.py @@ -189,6 +189,12 @@ def colab_check() -> bool: return is_colab +def kaggle_check() -> bool: + return bool( + os.environ.get("KAGGLE_KERNEL_RUN_TYPE") or os.environ.get("GFOOTBALL_DATA_DIR") + ) + + def ipython_check() -> bool: """ Check if interface is launching from iPython (not colab) diff --git a/test/test_utils.py b/test/test_utils.py index 9b4de02672..cdbe453a5c 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -29,6 +29,7 @@ from gradio.utils import ( format_ner_list, get_local_ip_address, ipython_check, + kaggle_check, launch_analytics, readme_to_html, sanitize_list_for_csv, @@ -103,6 +104,31 @@ class TestUtils: def test_readme_to_html_correct_parse(self): readme_to_html("https://github.com/gradio-app/gradio/blob/master/README.md") + def test_kaggle_check_false(self): + assert not kaggle_check() + + def test_kaggle_check_true_when_run_type_set(self): + with mock.patch.dict( + os.environ, {"KAGGLE_KERNEL_RUN_TYPE": "Interactive"}, clear=True + ): + assert kaggle_check() + + def test_kaggle_check_true_when_both_set(self): + with mock.patch.dict( + os.environ, + {"KAGGLE_KERNEL_RUN_TYPE": "Interactive", "GFOOTBALL_DATA_DIR": "./"}, + clear=True, + ): + assert kaggle_check() + + def test_kaggle_check_false_when_neither_set(self): + with mock.patch.dict( + os.environ, + {"KAGGLE_KERNEL_RUN_TYPE": "", "GFOOTBALL_DATA_DIR": ""}, + clear=True, + ): + assert not kaggle_check() + class TestIPAddress: @pytest.mark.flaky