Skip to content

Add option to disable TryExamples without rebuilding docs #118

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

Merged
merged 16 commits into from
Jan 24, 2024

Conversation

steppi
Copy link
Collaborator

@steppi steppi commented Jan 10, 2024

This PR makes it so if a file entitled .disable_try_examples is added to the root level of the deployed documentation's build directory, "try examples" buttons will be hidden at page load time, effectively disabling interactive examples without requiring a documentation rebuild.

A Javascript function checks at page load for the presence of the file, and if so, hides the buttons.

In scipy/scipy#19729 (comment), @rgommers noted that it would be good to be able to disable the interactive examples without requiring a rebuild of the documentation. This is particularly helpful for SciPy with its long documentation build and deployment times.

image

@martinRenou martinRenou added the enhancement New feature or request label Jan 10, 2024
@Carreau
Copy link
Collaborator

Carreau commented Jan 10, 2024

To make thinks a bit more automatic, I would take a peak at the URL and look at wether it contains /latest|/stable or similar, or at least something the file you look at could be a json with a list of url pattern where this should be enabled/disabled.

At least the latest/stable would allow to have automatic enabling/disabling automatically when new version is pushed on RTD

}
}
});
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also you have a mix of tabs and spaces :-)

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ha. I use Emacs and for languages I use regularly, I have it set up to handle the indentation automatically. I guess the out of the box Javascript mode isn’t doing that properly. I’ll look into installing proper tooling.

@steppi
Copy link
Collaborator Author

steppi commented Jan 10, 2024

To make thinks a bit more automatic, I would take a peak at the URL and look at wether it contains /latest|/stable or similar, or at least something the file you look at could be a json with a list of url pattern where this should be enabled/disabled.

At least the latest/stable would allow to have automatic enabling/disabling automatically when new version is pushed on RTD

Having it be a json with a list of url patterns sounds like a good solution.

@steppi
Copy link
Collaborator Author

steppi commented Jan 10, 2024

Thanks @Carreau! I've updated it to check for a json file .try_examples_ignore.json containing a list of regex patterns for files where interactive documentation should be disabled. I've been testing this locally, but can work on getting it set up for my experimental deployment at https://steppi.github.io/scipy/. A hiccup there is that I accidentally left circle ci enabled in my SciPy fork after building and deploying the docs the last time and ran out of credits for the month, so it's not as simple as just pushing a new commit where jupyterlite-sphinx is pinned to this PR branch.

Another thought I had is that it would be useful to be able to specify a default .try_examples_ignore.json in Sphinx's conf.py.

@steppi
Copy link
Collaborator Author

steppi commented Jan 11, 2024

I've deployed the docs at https://steppi.github.io/scipy with jupyterlite-sphinx pinned to this PR branch and added an ignore file https://github.com/steppi/scipy/blob/gh-pages/.try_examples_ignore.json, which disables all linalg examples.

An example of a linalg example which is disabled: https://steppi.github.io/scipy/reference/generated/scipy.linalg.solve.html#scipy.linalg.solve

An example of a non-linalg example which is still live: https://steppi.github.io/scipy/reference/generated/scipy.cluster.hierarchy.linkage.html#scipy.cluster.hierarchy.linkage.

@steppi
Copy link
Collaborator Author

steppi commented Jan 11, 2024

I've changed .try_examples_ignore.json to just a generic config file .try_examples.json and documented it as such, since we might want to add more runtime changeable options in the future. I've also made it possible to specify a default version of this file in the sphinx conf.py.

@Carreau
Copy link
Collaborator

Carreau commented Jan 15, 2024

From a discussion, maybe we should have a "force-enable" function in the browser javascript console to force-enable.

Copy link
Collaborator

@Carreau Carreau left a comment

Choose a reason for hiding this comment

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

Miscelaneous comments.

Comment on lines 183 to 184
`TryExamples` buttons will be hidden in files matching at least one of these patterns,
effectively disabling the interactive documentation. In the provided example:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we want those to match files, or final URLs ?

Comment on lines 87 to 117
.then(response => {
if (!response.ok) {
if (response.status === 404) {
// try examples ignore file is not present.
return null;
}
throw new Error(`Error fetching ${ignoreFilePath}`);
}
return response.json();
})
.then(data => {
if (!data) {
return;
}
// Disable interactive examples if file matches one of the ignore patterns
// by hiding try_examples_buttons.
const regexPatterns = data.ignore_patterns;
for (let pattern of regexPatterns) {
let regex = new RegExp(pattern);
if (regex.test(currentPagePath)) {
var buttons = document.getElementsByClassName('try_examples_button');
for (var i = 0; i < buttons.length; i++) {
buttons[i].classList.add('hidden');
}
break;
}
}
})
.catch(error => {
console.error(error);
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

I talk to some local colleagues, and they told that most recent browser support top level await, so I think we canlater refactor into clearer code and avoid promises.

Comment on lines 536 to 540
"<script>"
'document.addEventListener("DOMContentLoaded", function() {'
f'window.loadTryExamplesConfig("{config_path}","{docname}");'
"});"
"</script>"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we want the regex to match urls, that way if say on readthedocs we can have only /stable/ and /latest/ to show JupyterLight buttons, but not older version.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, true. Mix up on my part. I didn't consider that we'd have two sets of deployed docs with different doc roots one at base_url + '/stable' and the other at base_url + '/latest'. I was thinking of the case where / is the doc root, and it has subfolders /stable and /latest, which isn't how things would typically work.

@steppi
Copy link
Collaborator Author

steppi commented Jan 16, 2024

@Carreau, I've updated this to match url pathnames instead of sphinx docnames; use async/await instead of promises, and have added a function to toggle the visibility of the buttons for use from the console.

@Carreau
Copy link
Collaborator

Carreau commented Jan 24, 2024

Thanks, I think we should just merge and iterate, to see how it goes.

@Carreau Carreau merged commit 308034d into jupyterlite:main Jan 24, 2024
@martinRenou
Copy link
Member

Thank you both!

@steppi
Copy link
Collaborator Author

steppi commented Jan 24, 2024

Thanks @Carreau! I'll work on implementing your other suggestions today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants