Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement passthrough configuration #25

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 24 additions & 65 deletions sphinx_thebe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,22 @@ def st_static_path(app):
app.config.html_static_path.append(static_path)


def init_thebe_default_config(app, env, docnames):
thebe_config = app.config.thebe_config
defaults = {
"selector": ".thebe",
"selector_input": "pre",
"selector_output": ".output",
}
for key, val in defaults.items():
if key not in thebe_config:
thebe_config[key] = val


def init_thebe_core(app, env):
config_thebe = app.config["thebe_config"]
if not config_thebe:
if not app.config["thebe_config"]:
logger.warning("Didn't find `thebe_config` in conf.py, add to use thebe")
return

# Add core libraries
opts = {"async": "async"}
app.add_js_file(filename="https://unpkg.com/thebelab@latest/lib/index.js", **opts)
app.add_js_file(filename=app.config.thebe_url, **opts)

# Add configuration variables
thebe_config = f"""
const thebe_selector = "{ app.config.thebe_config['selector'] }"
const thebe_selector_input = "{ app.config.thebe_config['selector_input'] }"
const thebe_selector_output = "{ app.config.thebe_config['selector_output'] }"
selector_config = f"""
const thebe_selector = "{ app.config.thebe_selector['cell'] }"
const thebe_selector_input = "{ app.config.thebe_selector['input'] }"
const thebe_selector_output = "{ app.config.thebe_selector['output'] }"
"""
app.add_js_file(None, body=thebe_config)
app.add_js_file(None, body=selector_config)
app.add_js_file(filename="sphinx-thebe.js", **opts)


Expand All @@ -63,7 +50,6 @@ def update_thebe_context(app, doctree, docname):
raise ValueError(
"thebe configuration must be `True` or a dictionary for configuration."
)
codemirror_theme = config_thebe.get("codemirror-theme", "abcdef")

# Thebe configuration
# Choose the kernel we'll use
Expand All @@ -75,41 +61,10 @@ def update_thebe_context(app, doctree, docname):
else:
kernel_name = "python3"

# Codemirror syntax
cm_language = kernel_name
if "python" in cm_language:
cm_language = "python"
elif cm_language == "ir":
cm_language = "r"
Comment on lines -79 to -83
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codemirror language is in principle kernel-dependent, however I would like to remove this bit. I think that thebe should configure the codemirror language by talking to the kernel instead. @choldgraf do you agree?

Also currently jupyter-book/thebe#241 seems to render this irrelevant.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the main challenge here is that kernels can have all kinds of wacky names (eg, conda-python-3) , while codemirror only uses "canonical" names for languages (eg, "python") so this logic will need to.be somewhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once thebe is talking with the kernel and it's running, thebe can read from codemirror_mode kernel metadata key, without any config. This would avoid flaky heuristics, and would therefore be a more reliable approach. Or am I missing something?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would require thebe to wait for the kernel to be ready and then update the DOM once that finishes, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, this is poor UX. I would still prefer to make the logic more reliable. In principle, notebook metadata should already have the codemirror mode specified directly. Any idea how we could get it?

An alternative approach is to assume that the necessary kernel is reachable locally in the environment we use to build the docs, and we could inspect that kernel for metadata. Would that be OK?


# Create the URL for the kernel request
repo_url = config_thebe.get(
"repository_url",
"https://github.com/binder-examples/jupyter-stacks-datascience",
)
branch = config_thebe.get("repository_branch", "master")
path_to_docs = config_thebe.get("path_to_docs", ".").strip("/") + "/"
org, repo = _split_repo_url(repo_url)

# 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
}}
{ json.dumps(thebe_config) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason we'd wanna do any data validation or anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think validation is a great idea, however the best approach to implement validation would be jupyter-book/thebe#398.

</script>
"""

Expand All @@ -119,17 +74,6 @@ def update_thebe_context(app, doctree, docname):
)


def _split_repo_url(url):
"""Split a repository URL into an org / repo combination."""
if "github.com/" in url:
end = url.split("github.com/")[-1]
org, repo = end.split("/")[:2]
else:
logger.warning(f"Currently Thebe repositories must be on GitHub, got {url}")
org = repo = None
return org, repo


class ThebeButtonNode(nodes.Element):
"""Appended to the doctree by the ThebeButton directive

Expand Down Expand Up @@ -196,7 +140,22 @@ def setup(app):
app.connect("env-updated", init_thebe_core)

# configuration for this tool
app.add_config_value("thebe_config", {}, "html")
app.add_config_value("thebe_config", None, "html")
app.add_config_value(
"thebe_selector",
{
"cell": ".thebe",
"input": "pre",
"output": ".output",
},
"html",
)
app.add_config_value(
"thebe_url",
"https://unpkg.com/[email protected]/lib/index.js",
"html"
)

# override=True in case Jupyter Sphinx has already been loaded
app.add_directive("thebe-button", ThebeButton, override=True)

Expand Down