Repository: holoviz/panel

Issue:
Automatic `comms` detection is sensitive to import order
First noticed here https://github.com/holoviz/holoviews/pull/5398

When `hv.extension()` (or a import of `hvplot`) is done before `pn.extension()` it does not automatic set the current `comms` configuration.

| | Colab | vscode |
|-|---------|-----------|
|`holoviews` / `hvplot` import first | ![ Screenshot 2022-08-22 10 49 59](https://user-images.githubusercontent.com/19758978/185882561-7a47c693-759c-4c08-80f5-49279d8178ec.png) | ![ Screenshot 2022-08-22 10 55 41](https://user-images.githubusercontent.com/19758978/185882841-ad86a5b0-afc4-43ad-8956-8cdb1de5c699.png) |
|`panel` import first |  ![ Screenshot 2022-08-22 10 50 49](https://user-images.githubusercontent.com/19758978/185882671-6b9254df-4479-4c98-a14a-b28e0801f25a.png) | ![ Screenshot 2022-08-22 10 55 13](https://user-images.githubusercontent.com/19758978/185882901-1357e426-3166-43dd-be39-0eb14af618ef.png) |  

The offending code seems to be here:

https://github.com/holoviz/panel/blob/4f8ab43dac9ca5971f5bd84c35e7882ca676e541/panel/config.py#L597-L599

This could be easy to fix, but I'm not sure if this will break some other underlying logic. 





Discussion:
Would it be possible to set up some tests? I.e. configure some `fixture` environment with the right environment variables and then create some tests from your examples above?

Resolving patch:
diff --git a/panel/config.py b/panel/config.py
--- a/panel/config.py
+++ b/panel/config.py
@@ -166,7 +166,7 @@ class _config(_base_config):
     _admin = param.Boolean(default=False, doc="Whether the admin panel was enabled.")
 
     _comms = param.ObjectSelector(
-        default='default', objects=['default', 'ipywidgets', 'vscode', 'colab'], doc="""
+        default=None, objects=[None, 'default', 'ipywidgets', 'vscode', 'colab'], doc="""
         Whether to render output in Jupyter with the default Jupyter
         extension or use the jupyter_bokeh ipywidget model.""")
 
@@ -601,6 +601,8 @@ def __call__(self, *args, **params):
 
         from .io.notebook import load_notebook
 
+        self._detect_comms(params)
+
         newly_loaded = [arg for arg in args if arg not in panel_extension._loaded_extensions]
         if loaded and newly_loaded:
             self.param.warning(
@@ -637,7 +639,18 @@ def __call__(self, *args, **params):
             load_notebook(config.inline)
         panel_extension._loaded = True
 
+        if config.notifications:
+            display(state.notifications) # noqa
+
+        if config.load_entry_points:
+            self._load_entry_points()
+
+    def _detect_comms(self, params):
         if 'comms' in params:
+            config.comms = params.pop("comms")
+            return
+
+        if config.comms is not None:
             return
 
         # Try to detect environment so that we can enable comms
@@ -648,18 +661,15 @@ def __call__(self, *args, **params):
         except ImportError:
             pass
 
-        # Check if we're running in VSCode
         if "VSCODE_PID" in os.environ:
             config.comms = "vscode"
+            return
 
         if "pyodide" in sys.modules:
             config.comms = "ipywidgets"
+            return
 
-        if config.notifications:
-            display(state.notifications) # noqa
-
-        if config.load_entry_points:
-            self._load_entry_points()
+        config.comms = "default"
 
     def _apply_signatures(self):
         from inspect import Parameter, Signature


Test patch: