Skip to content

Fix nbextensions loader timeout on large notebooks #455

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 4 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Starting with v1.31.6, this file will contain a record of major features and upd
## Upcoming
- Added support for Python 3.10 ([Link to PR](https://github.com/aws/graph-notebook/pull/476))
- Deprecated Python 3.7 support ([PR #1](https://github.com/aws/graph-notebook/pull/453)) ([PR #2](https://github.com/aws/graph-notebook/pull/473))
- Patched nbextensions loader timeouts for large notebooks ([PR #1](https://github.com/aws/graph-notebook/pull/455))
- Fixed Dockerfile builds breaking with AL2023 ([Link to PR](https://github.com/aws/graph-notebook/pull/466))
- Fixed `--store-to` option for several magics ([Link to PR](https://github.com/aws/graph-notebook/pull/463))
- Fixed broken documentation links in Neptune ML notebooks ([PR #1](https://github.com/aws/graph-notebook/pull/467)) ([PR #2](https://github.com/aws/graph-notebook/pull/468))
Expand All @@ -24,6 +25,7 @@ Starting with v1.31.6, this file will contain a record of major features and upd
- Added `--profile-misc-args` option to `%%gremlin` ([Link to PR](https://github.com/aws/graph-notebook/pull/443))
- Added error messaging for incompatible host-specific `%%graph_notebok_config` parameters ([Link to PR](https://github.com/aws/graph-notebook/pull/456))
- Ensure default assignments for all Gremlin nodes when using grouping ([Link to PR](https://github.com/aws/graph-notebook/pull/448))
- Fixed nbextensions loader timeout on large notebooks ([Link to PR](https://github.com/aws/graph-notebook/pull/455))

## Release 3.7.1 (January 25, 2023)
- Added ECR auto-publish workflow ([Link to PR](https://github.com/aws/graph-notebook/pull/405))
Expand Down
31 changes: 29 additions & 2 deletions src/graph_notebook/start_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

HOME_PATH = os.path.expanduser("~")
NBCONFIG_DIR_TREE = HOME_PATH + '/.jupyter/nbconfig'
CFG_FILE_NAME = 'notebook.json'
NOTEBOOK_CFG_PATH = NBCONFIG_DIR_TREE + '/' + CFG_FILE_NAME
NOTEBOOK_CFG_PATH = NBCONFIG_DIR_TREE + '/notebook.json'

CUSTOM_DIR_TREE = HOME_PATH + '/.jupyter/custom'
NOTEBOOK_CUSTOMJS_PATH = CUSTOM_DIR_TREE + '/custom.js'


def patch_cm_cypher_config():
Expand All @@ -33,13 +35,38 @@ def patch_cm_cypher_config():
json.dump(notebook_cfg, file, indent=2)


def patch_customjs():
# Increases time allotted to load nbextensions on large notebooks. Limit is set to 60s and can be increased further.
# Reference: https://github.com/ipython-contrib/jupyter_contrib_nbextensions/blob/master/docs/source/troubleshooting.md#extensions-not-loading-for-large-notebooks
limit = "60"
increase_requirejs_timeout_prefix = "window.requirejs.config({waitSeconds:"
increase_requirejs_timeout_suffix = "});"
requirejs_timeout_full = increase_requirejs_timeout_prefix + limit + increase_requirejs_timeout_suffix

try:
os.makedirs(CUSTOM_DIR_TREE, exist_ok=True)
with open(NOTEBOOK_CUSTOMJS_PATH, 'r') as file:
customjs_content = file.read()
except (json.decoder.JSONDecodeError, FileNotFoundError) as e:
customjs_content = ""

if increase_requirejs_timeout_prefix not in customjs_content:
if customjs_content:
customjs_content += "\n"
customjs_content += requirejs_timeout_full
with open(NOTEBOOK_CUSTOMJS_PATH, 'w') as file:
file.write(customjs_content)
print(f"Modified nbextensions loader timeout limit to {limit} seconds")


def main():
parser = argparse.ArgumentParser()
parser.add_argument('--notebooks-dir', default='', type=str, help='The directory to start Jupyter from.')

args = parser.parse_args()

patch_cm_cypher_config()
patch_customjs()

kernel_manager_option = "--NotebookApp.kernel_manager_class=notebook.services.kernels.kernelmanager.AsyncMappingKernelManager"
notebooks_dir = '~/notebook/destination/dir' if args.notebooks_dir == '' else args.notebooks_dir
Expand Down