executablebooks / sphinx-thebe

A Sphinx extension to convert static code into interactive code cells with Jupyter, Thebe, and Binder.
https://sphinx-thebe.readthedocs.io/en/latest/
MIT License
28 stars 15 forks source link

Using thebe with a local server #27

Open psychemedia opened 3 years ago

psychemedia commented 3 years ago

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

akhmerov commented 3 years ago

Would #25 address this need?

psychemedia commented 3 years ago

@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 .

bethac07 commented 2 years ago

+1 to this being useful!