Repository: gradio-app/gradio

Issue:
Show "view API" button with a helpful message even with no named API routes
It has been pretty confusing for our users when they see some Gradio demos with the "view API" link and others without it. The reason this happens is that when there are no named API routes, we do not show the "view API" link. This is the case in Blocks (by default if there is no `api_name` attached to any event listener), while in Interface, there is always at least one named API.

To avoid this confusion, I would propose that we always show the "view API" link, but if there is no named API then clicking on the "view API" link includes a message that says "this demo has no named API routes" and link to the documentation for named API routes.

Related discussion: https://huggingface.slack.com/archives/C02QNREU24S/p1662068541775709


Discussion:

Resolving patch:
diff --git a/demo/reverse_audio/run.py b/demo/reverse_audio/run.py
--- a/demo/reverse_audio/run.py
+++ b/demo/reverse_audio/run.py
@@ -14,7 +14,7 @@ def reverse_audio(audio):
                     inputs="microphone", 
                     outputs="audio", 
                     examples=[
-                    "https://file-examples.com/storage/fe6d784fb46320d949c245e/2017/11/file_example_MP3_700KB.mp3",
+                    "https://samplelib.com/lib/preview/mp3/sample-3s.mp3",
                     os.path.join(os.path.dirname(__file__), "audio/recording1.wav")
         ], cache_examples=True)
 
diff --git a/gradio/blocks.py b/gradio/blocks.py
--- a/gradio/blocks.py
+++ b/gradio/blocks.py
@@ -413,6 +413,7 @@ def __init__(
         self.app_id = random.getrandbits(64)
         self.temp_dirs = set()
         self.title = title
+        self.show_api = True
 
         data = {
             "mode": self.mode,
@@ -785,6 +786,7 @@ def get_config_file(self):
             "is_space": self.is_space,
             "enable_queue": getattr(self, "enable_queue", False),  # launch attributes
             "show_error": getattr(self, "show_error", False),
+            "show_api": self.show_api,
         }
 
         def getLayout(block):
@@ -946,6 +948,7 @@ def launch(
         ssl_certfile: Optional[str] = None,
         ssl_keyfile_password: Optional[str] = None,
         quiet: bool = False,
+        show_api: bool = True,
         _frontend: bool = True,
     ) -> Tuple[FastAPI, str, str]:
         """
@@ -974,6 +977,7 @@ def launch(
             ssl_certfile: If a path to a file is provided, will use this as the signed certificate for https. Needs to be provided if ssl_keyfile is provided.
             ssl_keyfile_password: If a password is provided, will use this with the ssl certificate for https.
             quiet: If True, suppresses most print statements.
+            show_api: If True, shows the api docs in the footer of the app. Default True.
         Returns:
             app: FastAPI app object that is running the demo
             local_url: Locally accessible link to the demo
@@ -1000,6 +1004,7 @@ def reverse(text):
         self.height = height
         self.width = width
         self.favicon_path = favicon_path
+        self.show_api = show_api
         if enable_queue is not None:
             self.enable_queue = enable_queue
             warnings.warn(


Test patch:
diff --git a/test/test_blocks.py b/test/test_blocks.py
--- a/test/test_blocks.py
+++ b/test/test_blocks.py
@@ -108,6 +108,9 @@ def fake_func():
 
         config = demo.get_config_file()
         self.assertTrue(assert_configs_are_equivalent_besides_ids(XRAY_CONFIG, config))
+        assert config["show_api"] is True
+        _ = demo.launch(prevent_thread_lock=True, show_api=False)
+        assert demo.config["show_api"] is False
 
     def test_load_from_config(self):
         def update(name):