sphinx-thebe icon indicating copy to clipboard operation
sphinx-thebe copied to clipboard

Using thebe with a local server

Open psychemedia opened this issue 3 years ago • 3 comments

Currently, thebe is hardwired to connect to a kernel launched from a BinderHub server using the following templated config as per https://github.com/executablebooks/sphinx-thebe/blob/44ae793880eba585120cfea0af87012b6abf21fc/sphinx_thebe/init.py#L94:

    # Update the doctree with some nodes for the thebe configuration
    thebe_html_config = f"""
    <script type="text/x-thebe-config">
    {{
        requestKernel: true,
        binderOptions: {{
            repo: "{org}/{repo}",
            ref: "{branch}",
        }},
        codeMirrorConfig: {{
            theme: "{codemirror_theme}",
            mode: "{cm_language}"
        }},
        kernelOptions: {{
            kernelName: "{kernel_name}",
            path: "{path_to_docs}{str(Path(docname).parent)}"
        }},
        predefinedOutput: true
    }}
    </script>
    """

It would be useful to be able to specify a local connection to an already running server rather than a BinderHub connection, eg as per https://github.com/executablebooks/jupyter-book/issues/199 and https://github.com/executablebooks/jupyter-book/issues/1206.

This would require a template of the form (untested - {} may be messed up...):

thebe_html_config = f"""
    <script type="text/x-thebe-config">
    {{
        bootstrap: true,
        kernelOptions: {{
          name: "{kernel_name}",
          serverSettings: {{
            "baseUrl": "{thebe_server_url}",
            "token": "{thebe_server_token}"
          }}
      }}
    }}
    </script>
    """

to give a config such as the following for a local server run as jupyter notebook --NotebookApp.token=test-secret --NotebookApp.allow_origin='http://localhost:8888'

<script type="text/x-thebe-config">
{
  bootstrap: true,
  kernelOptions: {
    name: "python3",
    serverSettings: {
      "baseUrl": "http://127.0.0.1:8888",
      "token": "test-secret"
    }
  },
}
</script>

As for settings in Jupyter book, maybe add something like the following to jupyter_book/config_schema.json:

"launch_buttons": {
            "type": "object",
            "properties": {

                # ...

                "thebe_server_url": {
                    "type": "string"
                },
                "thebe_server_token": {
                    "type": "string"
                }
            }

with default set via jupyter_book/default_config.yml as per something like:


# Launch button settings
launch_buttons:
 # ...
  thebe_server_url           : "" # Use a specified server URL (must be started with appropriate --NotebookApp.allow_origin setting)
  thebe_server_token      : "" # thebe_local_url server must be started with a corresponding --NotebookApp.token value.
  

In a default setup, as per above, these would then be set as per:

  thebe_server_url           : "http://127.0.0.1:8888"
  thebe_server_token      : "test-secret"

Some logic would then be required to determine which thebe template to use based on relative settings of binderhub_url and thebe_local_url .

The Launching from MyBinder message in sphinx_thebe/_static/sphinx-thebe.js would also need updating as appropriate.

It would possibly be neater to have a Jupyter Book setting thebe: binderhub | server | none rather than true false and then thebe_server_url would work equally for BinderHub. But that could break backwards compatible config files.

I've started trying to explore this route by mnaully editing Jupyter Book HTML pages in the context of a jupyter-server-proxied Jupyter Book. Notes here: https://github.com/ouseful-testing/jupyter-book-server-proxy

psychemedia avatar Jun 18 '21 12:06 psychemedia

Would #25 address this need?

akhmerov avatar Jun 20 '21 22:06 akhmerov

@akhmerov Ah, so is that suggesting just dumping thebe_config into <script type="text/x-thebe-config">?

So to backtrack from that, some way would need to setup the thebe_config structure correctly.

I also note for even more generality, it would be worth considering a case where ThebeLab might also support Jupyterlite (eg https://github.com/executablebooks/thebe/issues/412 .

psychemedia avatar Jun 21 '21 08:06 psychemedia

+1 to this being useful!

bethac07 avatar May 26 '22 19:05 bethac07