jupyterhub / jupyter-server-proxy

Jupyter notebook server extension to proxy web services.
https://jupyter-server-proxy.readthedocs.io
BSD 3-Clause "New" or "Revised" License
346 stars 147 forks source link

Url handler order different dependent on whether config is by traitlets vs entry_points #326

Open metafeather opened 2 years ago

metafeather commented 2 years ago

Docs: https://jupyter-server-proxy.readthedocs.io/en/latest/server-process.html#specifying-config-via-traitlets

When using traitlets (in a jupyter_lab_config.py file) the declared order of the urls to proxy is used when being turned in to url handlers inside the Tornado server 1.

But when using entry_points (in a python package) pkg_resources.iter_entry_points('jupyter_serverproxy_servers') is used to get the list of paths to proxy and this is sorted alphabetically by setuptools first 2.

This means that url handlers can mask each other and be unreachable, e.g. if the order declared is:

with entry_points the order returned will be:

and so the proxy to a/b/some/path/ cannot be reached.

As the list of entry_points is global to all installed packages this can be hard to track down.

A quick fix for this would be to reverse the results of pkg_resources.iter_entry_points('jupyter_serverproxy_servers') before using them, as these would be in an appropriate order for url routing.

In my particular case I wanted to proxy to my wiki server under my application root so that links from the app worked as expected, and was forced to use traitlets, with my users copy and pasting configs rather than being able to install a module.

welcome[bot] commented 2 years ago

Thank you for opening your first issue in this project! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out Jupyter's Code of Conduct. Also, please try to follow the issue template as it helps other other community members to contribute more effectively. welcome You can meet the other Jovyans by joining our Discourse forum. There is also an intro thread there where you can stop by and say Hi! :wave:
Welcome to the Jupyter community! :tada:

metafeather commented 2 years ago

Somewhat related, but not so well documented is that you can declare multiple entry_points in a setup.py to aid code reuse, e.g.

entry_points={
        "jupyter_serverproxy_servers": [
            # name = packagename:function_name
            "research/wiki = wiki_server_proxies:setup_research_wiki_service",
            "research = wiki_server_proxies:setup_research_docs_service",
            "platform/wiki = wiki_server_proxies:setup_platform_wiki_service",
            "platform= wiki_server_proxies:setup_platform_docs_service",
        ]
    }

However these still get sorted globally.

takluyver commented 1 year ago

Does importlib.metadata give you the names in the order you specified them?

from importlib.metadata import entry_points
entry_points(group='jupyter_serverproxy_servers')

This is the modern replacement for pkg_resources, so if it fixes this problem, it should be easy for JSP to switch over.