├── .eslintignore ├── .eslintrc.js ├── .github ├── dependabot.yml └── workflows │ ├── binder-badge.yaml │ └── build.yml ├── .gitignore ├── .gitpod.Dockerfile ├── .gitpod.yml ├── .pre-commit-config.yaml ├── .prettierignore ├── .prettierrc ├── .yarnrc.yml ├── CHANGELOG.md ├── LICENSE ├── LICENSE.dexie ├── MANIFEST.in ├── README.md ├── RELEASE.md ├── binder ├── environment.yml └── postBuild ├── dev-requirements-jl3.old ├── dev-requirements-jl4.txt ├── dev-requirements.txt ├── example.ipynb ├── jupyter_offlinenotebook ├── __init__.py ├── description.yaml ├── etc │ ├── offlinenotebook_jpserverextension.json │ ├── offlinenotebook_nbextension.json │ └── offlinenotebook_nbserverextension.json └── static │ └── main.js ├── offline-notebook-buttons.png ├── package.json ├── pyproject.toml ├── setup.cfg ├── setup.py ├── src ├── index.ts └── jslib │ └── offlinenotebook.ts ├── style └── index.css ├── tests ├── start.sh └── test_offlinenotebook.py ├── tsconfig.json ├── webpack.config.js └── yarn.lock /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | coverage 4 | **/*.d.ts 5 | tests 6 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: [ 3 | 'eslint:recommended', 4 | 'plugin:@typescript-eslint/eslint-recommended', 5 | 'plugin:@typescript-eslint/recommended', 6 | 'plugin:prettier/recommended', 7 | ], 8 | parser: '@typescript-eslint/parser', 9 | parserOptions: { 10 | project: 'tsconfig.json', 11 | sourceType: 'module', 12 | }, 13 | plugins: ['@typescript-eslint'], 14 | rules: { 15 | '@typescript-eslint/no-unused-vars': ['warn', { args: 'none' }], 16 | '@typescript-eslint/no-explicit-any': 'off', 17 | '@typescript-eslint/no-namespace': 'off', 18 | '@typescript-eslint/no-use-before-define': 'off', 19 | '@typescript-eslint/quotes': [ 20 | 'error', 21 | 'single', 22 | { avoidEscape: true, allowTemplateLiterals: false }, 23 | ], 24 | curly: ['error', 'all'], 25 | eqeqeq: 'error', 26 | 'prefer-arrow-callback': 'error', 27 | }, 28 | }; 29 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # https://docs.github.com/en/github/administering-a-repository/configuration-options-for-dependency-updates 2 | version: 2 3 | updates: 4 | - package-ecosystem: pip 5 | directory: / 6 | schedule: 7 | interval: weekly 8 | 9 | - package-ecosystem: github-actions 10 | directory: / 11 | schedule: 12 | interval: monthly 13 | -------------------------------------------------------------------------------- /.github/workflows/binder-badge.yaml: -------------------------------------------------------------------------------- 1 | #./.github/workflows/binder-badge.yaml 2 | name: binder-badge 3 | on: 4 | pull_request_target: 5 | 6 | jobs: 7 | badge: 8 | runs-on: ubuntu-latest 9 | steps: 10 | - uses: manics/action-binderbadge@main 11 | with: 12 | githubToken: ${{ secrets.GITHUB_TOKEN }} 13 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | # https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions 2 | name: Build 3 | on: 4 | push: 5 | pull_request: 6 | 7 | jobs: 8 | # https://github.com/pre-commit/action 9 | pre-commit: 10 | name: Lint 11 | runs-on: ubuntu-22.04 12 | steps: 13 | - uses: actions/checkout@v4 14 | - uses: actions/setup-python@v5 15 | with: 16 | python-version: '3.12' 17 | - uses: pre-commit/action@v3.0.1 18 | 19 | # Due to complications in the build process when trying to support both JupyterLab 20 | # 2 and 3 we build the pypi packages with JupyterLab 3, but test on 2 and 3 21 | 22 | build: 23 | name: Build dist 24 | runs-on: ubuntu-22.04 25 | steps: 26 | - uses: actions/checkout@v4 27 | 28 | - uses: actions/setup-python@v5 29 | with: 30 | python-version: '3.10' 31 | cache: pip 32 | cache-dependency-path: 'dev-requirements*' 33 | 34 | - uses: actions/setup-node@v4 35 | with: 36 | node-version: '20.x' 37 | cache: yarn 38 | 39 | - name: Install dependencies 40 | run: python -mpip install -r dev-requirements-jl4.txt 41 | 42 | - name: Build dist 43 | run: | 44 | python -mbuild 45 | ls dist/*tar.gz dist/*.whl 46 | 47 | - name: Javascript format 48 | run: | 49 | jlpm install 50 | jlpm run format:check 51 | 52 | - name: Javascript package 53 | run: | 54 | mkdir jsdist 55 | jlpm pack --filename jsdist/jupyter-offlinenotebook-jlpmpack.tgz 56 | 57 | - uses: actions/upload-artifact@v4 58 | with: 59 | name: dist 60 | path: dist 61 | if-no-files-found: error 62 | 63 | - uses: actions/upload-artifact@v4 64 | with: 65 | name: jsdist 66 | path: jsdist 67 | if-no-files-found: error 68 | 69 | test: 70 | name: Pytest 71 | needs: build 72 | strategy: 73 | # Keep running so we can see if other tests pass 74 | fail-fast: false 75 | matrix: 76 | include: 77 | - python-version: '3.7' 78 | jupyterlab-major: '3' 79 | - python-version: '3.10' 80 | jupyterlab-major: '4' 81 | - python-version: '3.12' 82 | jupyterlab-major: '4' 83 | runs-on: ubuntu-22.04 84 | # Includes geckdriver and firefox 85 | # https://github.com/actions/runner-images/blob/main/images/ubuntu/Ubuntu2204-Readme.md 86 | steps: 87 | - uses: actions/checkout@v4 88 | 89 | - name: Set up Python ${{ matrix.python-version }} 90 | uses: actions/setup-python@v5 91 | with: 92 | python-version: ${{ matrix.python-version }} 93 | cache: pip 94 | cache-dependency-path: 'dev-requirements*' 95 | 96 | - uses: actions/setup-node@v4 97 | with: 98 | node-version: '20.x' 99 | cache: yarn 100 | 101 | - name: Download artifacts from build 102 | uses: actions/download-artifact@v4 103 | 104 | - name: Install dependencies 105 | run: python -mpip install -r dev-requirements-jl${{ matrix.jupyterlab-major }}.* 106 | 107 | - name: Install plugin 108 | run: | 109 | python -mpip install dist/*.whl 110 | 111 | - name: Run pytest 112 | run: pytest -vs tests 113 | 114 | # https://packaging.python.org/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/ 115 | publish-pypi: 116 | needs: 117 | # Only publish if other jobs passed 118 | - pre-commit 119 | - test 120 | runs-on: ubuntu-22.04 121 | permissions: 122 | id-token: write 123 | steps: 124 | - name: Download artifacts from build 125 | uses: actions/download-artifact@v4 126 | with: 127 | name: dist 128 | path: dist 129 | 130 | - name: Publish to PyPI 131 | if: startsWith(github.ref, 'refs/tags') 132 | uses: pypa/gh-action-pypi-publish@v1.12.4 133 | 134 | # https://docs.github.com/en/actions/language-and-framework-guides/publishing-nodejs-packages#publishing-packages-to-the-npm-registry 135 | publish-npm: 136 | needs: 137 | # Only publish if other jobs passed 138 | - pre-commit 139 | - test 140 | runs-on: ubuntu-22.04 141 | steps: 142 | # Setup .npmrc file to publish to npm 143 | - uses: actions/setup-node@v4 144 | with: 145 | node-version: '20.x' 146 | registry-url: https://registry.npmjs.org 147 | 148 | - name: Download artifacts from build 149 | uses: actions/download-artifact@v4 150 | with: 151 | name: jsdist 152 | path: jsdist 153 | - run: npm publish --dry-run ./jsdist/jupyter-offlinenotebook-jlpmpack.tgz 154 | - run: npm publish ./jsdist/jupyter-offlinenotebook-jlpmpack.tgz 155 | if: startsWith(github.ref, 'refs/tags') 156 | env: 157 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 158 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.bundle.* 2 | lib/ 3 | node_modules/ 4 | *.egg-info/ 5 | .ipynb_checkpoints 6 | *.tsbuildinfo 7 | 8 | */labextension/*.tgz 9 | 10 | # Byte-compiled / optimized / DLL files 11 | __pycache__/ 12 | *.py[cod] 13 | *$py.class 14 | 15 | # C extensions 16 | *.so 17 | 18 | # Distribution / packaging 19 | .Python 20 | build/ 21 | develop-eggs/ 22 | dist/ 23 | downloads/ 24 | eggs/ 25 | .eggs/ 26 | lib/ 27 | lib64/ 28 | parts/ 29 | sdist/ 30 | var/ 31 | wheels/ 32 | pip-wheel-metadata/ 33 | share/python-wheels/ 34 | *.egg-info/ 35 | .installed.cfg 36 | *.egg 37 | MANIFEST 38 | 39 | # PyInstaller 40 | # Usually these files are written by a python script from a template 41 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 42 | *.manifest 43 | *.spec 44 | 45 | # Installer logs 46 | pip-log.txt 47 | pip-delete-this-directory.txt 48 | 49 | # Unit test / coverage reports 50 | htmlcov/ 51 | .tox/ 52 | .nox/ 53 | .coverage 54 | .coverage.* 55 | .cache 56 | nosetests.xml 57 | coverage.xml 58 | *.cover 59 | *.py,cover 60 | .hypothesis/ 61 | .pytest_cache/ 62 | 63 | # Translations 64 | *.mo 65 | *.pot 66 | 67 | # Django stuff: 68 | *.log 69 | local_settings.py 70 | db.sqlite3 71 | db.sqlite3-journal 72 | 73 | # Flask stuff: 74 | instance/ 75 | .webassets-cache 76 | 77 | # Scrapy stuff: 78 | .scrapy 79 | 80 | # Sphinx documentation 81 | docs/_build/ 82 | 83 | # PyBuilder 84 | target/ 85 | 86 | # Jupyter Notebook 87 | .ipynb_checkpoints 88 | 89 | # IPython 90 | profile_default/ 91 | ipython_config.py 92 | 93 | # pyenv 94 | .python-version 95 | 96 | # pipenv 97 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 98 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 99 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 100 | # install all needed dependencies. 101 | #Pipfile.lock 102 | 103 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 104 | __pypackages__/ 105 | 106 | # Celery stuff 107 | celerybeat-schedule 108 | celerybeat.pid 109 | 110 | # SageMath parsed files 111 | *.sage.py 112 | 113 | # Environments 114 | .env 115 | .venv 116 | env/ 117 | venv/ 118 | ENV/ 119 | env.bak/ 120 | venv.bak/ 121 | 122 | # Spyder project settings 123 | .spyderproject 124 | .spyproject 125 | 126 | # Rope project settings 127 | .ropeproject 128 | 129 | # mkdocs documentation 130 | /site 131 | 132 | # mypy 133 | .mypy_cache/ 134 | .dmypy.json 135 | dmypy.json 136 | 137 | # Pyre type checker 138 | .pyre/ 139 | 140 | 141 | # JavaScript 142 | node_modules 143 | tsconfig.tsbuildinfo 144 | 145 | # Extension output 146 | jupyter_offlinenotebook/static/jslib/ 147 | jupyter_offlinenotebook/static/lab/ 148 | -------------------------------------------------------------------------------- /.gitpod.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM gitpod/workspace-full:latest 2 | 3 | ARG DEBIAN_FRONTEND=noninteractive 4 | 5 | RUN sudo apt-get -q update && sudo apt-get install -yq firefox-geckodriver 6 | -------------------------------------------------------------------------------- /.gitpod.yml: -------------------------------------------------------------------------------- 1 | image: 2 | file: .gitpod.Dockerfile 3 | 4 | tasks: 5 | # https://www.gitpod.io/docs/languages/python#start-tasks 6 | - init: > 7 | pip3 install -r dev-requirements-jl4.txt && 8 | pip3 install . 9 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/psf/black 3 | rev: 24.1.1 4 | hooks: 5 | - id: black 6 | args: [--target-version=py36] 7 | - repo: https://github.com/pycqa/flake8 8 | rev: 7.0.0 9 | hooks: 10 | - id: flake8 11 | # default black line length is 88 12 | args: [--max-line-length=88] 13 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | jupyter_offlinenotebook/static/jslib 2 | jupyter_offlinenotebook/static/lab/static 3 | lib 4 | node_modules 5 | package.json 6 | .pytest_cache 7 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true 3 | } 4 | -------------------------------------------------------------------------------- /.yarnrc.yml: -------------------------------------------------------------------------------- 1 | nodeLinker: node-modules 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 0.3.1 2024-02-12 2 | 3 | ([full changelog](https://github.com/manics/jupyter-offlinenotebook/compare/v0.3.0...v0.3.1)) 4 | 5 | ## Bug fixes 6 | 7 | - Use setuptools-scm to include files in sdist, switch to `python -mbuild` [#568](https://github.com/manics/jupyter-offlinenotebook/pull/568) ([@manics](https://github.com/manics)) 8 | 9 | ## Other 10 | 11 | - Add \_jupyter_server_extension_points [#578](https://github.com/manics/jupyter-offlinenotebook/pull/578) ([@manics](https://github.com/manics)) 12 | - Disable NPM in dependabot, add github actions [#577](https://github.com/manics/jupyter-offlinenotebook/pull/577) ([@manics](https://github.com/manics)) 13 | 14 | # 0.3.0 2024-02-10 15 | 16 | JupyterLab 4 support, require Jupyter Server (for Notebook support use NbClassic), drop support for Python 3.6. 17 | 18 | ([full changelog](https://github.com/manics/jupyter-offlinenotebook/compare/v0.2.2...v0.3.0)) 19 | 20 | ### New features 21 | 22 | - Drop Python 3.6, test 3.12 [#565](https://github.com/manics/jupyter-offlinenotebook/pull/565) ([@manics](https://github.com/manics)) 23 | - Support JupyterLab 4, Require jupyter-server [#564](https://github.com/manics/jupyter-offlinenotebook/pull/564) ([@manics](https://github.com/manics)) 24 | 25 | ### Other 26 | 27 | - Update/fix CI, stop testing lab 2 / python 3.6 [#563](https://github.com/manics/jupyter-offlinenotebook/pull/563) ([@manics](https://github.com/manics)) 28 | - Regenerate yarn.lock [#458](https://github.com/manics/jupyter-offlinenotebook/pull/458) ([@manics](https://github.com/manics)) 29 | - Update .pre-commit-config.yaml versions [#457](https://github.com/manics/jupyter-offlinenotebook/pull/457) ([@manics](https://github.com/manics)) 30 | 31 | [@dependabot updates](https://github.com/manics/jupyter-offlinenotebook/pulls?q=is%3Apr+author%3Aapp%2Fdependabot+) have been omitted from this Changelog. 32 | 33 | # 0.2.2 2022-01-25 34 | 35 | ([full changelog](https://github.com/manics/jupyter-offlinenotebook/compare/v0.2.1...v0.2.2)) 36 | 37 | ### New features 38 | 39 | - Theme toolbar icon color [#389](https://github.com/manics/jupyter-offlinenotebook/pull/389) ([@fcollonval](https://github.com/fcollonval)) 40 | 41 | ### Other 42 | 43 | - Add gitpod config [#373](https://github.com/manics/jupyter-offlinenotebook/pull/373) ([@manics](https://github.com/manics)) 44 | 45 | # 0.2.1 2021-01-16 46 | 47 | ## Bug fixes 48 | 49 | - Set download type to application/x-ipynb+json ([#168](https://github.com/manics/jupyter-offlinenotebook/pull/168)) 50 | 51 | ### Other 52 | 53 | - Replace master with main ([#170](https://github.com/manics/jupyter-offlinenotebook/pull/170)) 54 | 55 | # 0.2.0 2021-01-04 56 | 57 | JupyterLab 3 support. 58 | 59 | ### New features 60 | 61 | - Jupyterlab 3 support ([#54](https://github.com/manics/jupyter-offlinenotebook/pull/54)) 62 | - JupyterLab 3 pip installable ([#108](https://github.com/manics/jupyter-offlinenotebook/pull/108)) 63 | - JupyterLab 3.0.0 ([#145](https://github.com/manics/jupyter-offlinenotebook/pull/145)) 64 | 65 | ### Other 66 | 67 | - Dexie 3 typescript ([#59](https://github.com/manics/jupyter-offlinenotebook/pull/59)) 68 | - Switch to GitHub workflows ([#63](https://github.com/manics/jupyter-offlinenotebook/pull/63)) 69 | - Add compatibility with jupyter-server ([#112](https://github.com/manics/jupyter-offlinenotebook/pull/112)) 70 | 71 | # 0.1.0 2020-03-18 72 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2019, Simon Li 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /LICENSE.dexie: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | # setuptools-scm includes all source controlled files 2 | 3 | graft jupyter_offlinenotebook/static 4 | 5 | # Javascript files 6 | prune **/node_modules 7 | prune lib 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Jupyter Offline Notebook 2 | 3 | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/manics/jupyter-offlinenotebook/main?urlpath=lab%2Ftree%2Fexample.ipynb) 4 | [![PyPI](https://img.shields.io/pypi/v/jupyter-offlinenotebook.svg)](https://pypi.python.org/pypi/jupyter-offlinenotebook) 5 | [![npm](https://img.shields.io/npm/v/jupyter-offlinenotebook)](https://www.npmjs.com/package/jupyter-offlinenotebook) 6 | [![Build Status](https://github.com/manics/jupyter-offlinenotebook/workflows/Build/badge.svg)](https://github.com/manics/jupyter-offlinenotebook/actions) 7 | 8 | Save and load notebooks to browser storage, even if you've lost your connection to the server. 9 | 10 | ## Installation 11 | 12 | pip install jupyter-offlinenotebook 13 | 14 | This should automatically enable the extension on Jupyter Notebook and JupyterLab. 15 | 16 | This extension supports JupyterLab 3 and 4, and NBclassic. 17 | Use [version 0.2.2](https://github.com/manics/jupyter-offlinenotebook/tree/v0.2.2) for JupyterLab 2 and Notebook <7. 18 | 19 | ## Usage 20 | 21 | ![Offline notebook buttons](./offline-notebook-buttons.png) 22 | 23 | You should see up to five new buttons depending on your configuration and where you are running the notebook: 24 | 25 | - download the in-memory (browser) state of the notebook 26 | - save the in-memory state of the notebook to local-storage 27 | - load a notebook from local-storage 28 | - open the permanent URL of the repository containing this notebook 29 | - copy the permanent mybinder URL to share this repository 30 | 31 | Saving and loading uses the repository ID and the path of the current notebook. 32 | 33 | You should always see the `Download` button. 34 | If you are running this on mybinder all buttons should be visible. 35 | See the configuration section below to enable the other buttons on other systems. 36 | 37 | If you don't see the buttons check the Javascript console log. 38 | 39 | See [example.ipynb](./example.ipynb) 40 | 41 | ## Configuration 42 | 43 | This extension can be configured in `jupyter_notebook_config.py` by setting the following properties of `c.OfflineNotebookConfig`: 44 | 45 | - `repository_id`: 46 | A callable that returns the repository ID. 47 | This is used when storing and retrieving notebooks. 48 | Default is the value of the `BINDER_REPO_URL` environment variable. 49 | - `repository_ref_url`: 50 | A callable that returns the repository reference URL. 51 | Default is the value of the `BINDER_REF_URL` environment variable. 52 | - `binder_persistent_url`: 53 | A callable that returns the repository reference URL. 54 | Default is the values of the `BINDER_LAUNCH_HOST` and 55 | `BINDER_PERSISTENT_REQUEST` environment variables. 56 | - `binder_repo_label`: 57 | A callable that returns the label used to link to the repository. 58 | 59 | # Warning 60 | 61 | This extension is still in development. 62 | It is only tested on Firefox. 63 | Breaking changes may occur in future. 64 | 65 | There are [several major limitations](https://github.com/manics/jupyter-offlinenotebook/issues) including: 66 | 67 | - Local-storage is limited by quotas imposed by the browser. 68 | - A repository ID and path of the notebook within Jupyter Notebook are used, joined by a ` `. 69 | This may change in future. 70 | 71 | # Development notes 72 | 73 | This extension stores notebooks in browser storage using the [IndexedDB API](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API), wrapped with [Dexie.js](https://dexie.org/). 74 | 75 | One server API call is made during initialisation to obtain the storage configuration. 76 | Everything else is done client-side so should work even if the server is disconnected. 77 | 78 | The CI pipeline builds the extension with JupyterLab 4, but the build package works with JupyterLab 3 and NBclassic. 79 | Install the development dependencies: 80 | 81 | pip install -r dev-requirements-jl4.txt 82 | 83 | To build and install the development version: 84 | 85 | pip install . 86 | 87 | This automatically runs `jlpm`. 88 | 89 | The notebook and server extensions should be automatically enabled. 90 | 91 | JupyterLab 3+ supports the installation of extensions as a static package so no further steps are required. 92 | 93 | Tagged releases of this repository are automatically published to [PyPI](https://pypi.python.org/pypi/jupyter-offlinenotebook) and [NPM](https://www.npmjs.com/package/jupyter-offlinenotebook). 94 | 95 | To test that the binder and repo buttons work when developing locally set some placeholder environment variables, e.g.: 96 | 97 | ``` 98 | BINDER_LAUNCH_HOST=http://localhost BINDER_REPO_URL=http://localhost BINDER_PERSISTENT_REQUEST=v2/gh/repo BINDER_REF_URL=http://localhost jupyter-lab --debug 99 | ``` 100 | 101 | If you make any changes remember to run all linters and auto-formatters: 102 | 103 | - `pre-commit run -a` 104 | - `jlpm run format` 105 | -------------------------------------------------------------------------------- /RELEASE.md: -------------------------------------------------------------------------------- 1 | # New release 2 | 3 | Checkout the `main` branch. 4 | If you do not have `sign-git-tag` enabled run: 5 | 6 | npm config set sign-git-tag true 7 | 8 | Update the version in `package.json` and create a Git tag by running: 9 | 10 | npm version $VERSION 11 | 12 | where `$VERSION` is the version that will be published on both PyPI and NPM, e.g. `0.2.0-rc.0` or `0.2.0`. 13 | The Git tag will automatically have a `v` prefix: `v$VERSION`. 14 | 15 | Push `main` and the new tag to the git remote: 16 | 17 | git push origin main --follow-tags 18 | 19 | The packages will be published by a [GitHub workflow](./.github/workflows/build.yml). 20 | -------------------------------------------------------------------------------- /binder/environment.yml: -------------------------------------------------------------------------------- 1 | name: offlinenotebook 2 | channels: 3 | - defaults 4 | dependencies: 5 | - jupyterlab>=4,<5 6 | - notebook>=7,<8 7 | - pip 8 | - python 9 | -------------------------------------------------------------------------------- /binder/postBuild: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -eux 3 | 4 | python -m pip install --upgrade . 5 | -------------------------------------------------------------------------------- /dev-requirements-jl3.old: -------------------------------------------------------------------------------- 1 | -r dev-requirements.txt 2 | jupyterlab==3.6.7 3 | # Also test the old version of jupyter_server 4 | jupyter_server==1.24.0 5 | -------------------------------------------------------------------------------- /dev-requirements-jl4.txt: -------------------------------------------------------------------------------- 1 | -r dev-requirements.txt 2 | jupyterlab==4.2.5 3 | jupyter_server==2.13.0 4 | -------------------------------------------------------------------------------- /dev-requirements.txt: -------------------------------------------------------------------------------- 1 | build==1.1.1 2 | jupyter_packaging==0.12.3 3 | flaky==3.8.1 4 | nbclassic==1.0.0 5 | pre-commit==2.21.0 6 | pytest==7.4.4 7 | selenium==4.11.2 8 | traitlets==5.9.0 9 | wheel==0.42.0 10 | # jupyterlab and jupyter_server are installed separately so we can test multiple versions 11 | -------------------------------------------------------------------------------- /example.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Offline notebook example\n", 8 | "\n", 9 | "You should see three new buttons:\n", 10 | "![Offline notebook buttons](./offline-notebook-buttons.png)" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "1. Make some changes to this notebook (or run it to update the output).\n", 18 | "2. Do not save the notebook. You can even disconnect from the Jupyter server or your network.\n", 19 | "3. Click the first button (`Download`). This should prompt you to download the notebook.\n", 20 | "4. Click the second button (`cloud download`). This should save the current notebook into your browser's [local-storage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage).\n", 21 | "5. Start a new instance of Jupyter, and open the original version of this notebook.\n", 22 | "6. Click the third button (`cloud upload`). This should restore the copy of the notebook from your browser's local-storage." 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "from datetime import datetime\n", 32 | "print(datetime.now())" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": null, 38 | "metadata": {}, 39 | "outputs": [], 40 | "source": [ 41 | "import os\n", 42 | "for (k, v) in sorted(os.environ.items()):\n", 43 | " print(f'{k}\\t{v}')" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "Limitations: see [README.md](https://github.com/manics/jupyter-offlinenotebook/blob/main/README.md)" 51 | ] 52 | } 53 | ], 54 | "metadata": { 55 | "kernelspec": { 56 | "display_name": "Python 3", 57 | "language": "python", 58 | "name": "python3" 59 | }, 60 | "language_info": { 61 | "codemirror_mode": { 62 | "name": "ipython", 63 | "version": 3 64 | }, 65 | "file_extension": ".py", 66 | "mimetype": "text/x-python", 67 | "name": "python", 68 | "nbconvert_exporter": "python", 69 | "pygments_lexer": "ipython3", 70 | "version": "3.7.5" 71 | } 72 | }, 73 | "nbformat": 4, 74 | "nbformat_minor": 2 75 | } 76 | -------------------------------------------------------------------------------- /jupyter_offlinenotebook/__init__.py: -------------------------------------------------------------------------------- 1 | import json 2 | from jupyter_server.utils import url_path_join 3 | from jupyter_server.base.handlers import JupyterHandler 4 | import os 5 | from tornado import web 6 | from traitlets import TraitType 7 | from traitlets.config import Configurable 8 | 9 | 10 | class Callable(TraitType): 11 | """ 12 | A trait which is callable. 13 | Classes are callable, as are instances 14 | with a __call__() method. 15 | """ 16 | 17 | info_text = "a callable" 18 | 19 | def validate(self, obj, value): 20 | if callable(value): 21 | return value 22 | else: 23 | self.error(obj, value) 24 | 25 | 26 | class OfflineNotebookHandler(JupyterHandler): 27 | @web.authenticated 28 | async def get(self): 29 | """ 30 | Return the BinderHub repository information 31 | 32 | This should be called once at the start, since the extension is meant 33 | to work if the user subsequently goes offline 34 | """ 35 | config = self.settings["offline_notebook_config"] 36 | jcfg = json.dumps( 37 | { 38 | "repoid": config.repository_id(), 39 | "binder_repo_label": config.repository_label(), 40 | "binder_ref_url": config.repository_ref_url(), 41 | "binder_persistent_url": config.binder_persistent_url(), 42 | } 43 | ) 44 | self.log.debug("OfflineNotebook config:%s ", jcfg) 45 | self.set_header("Content-Type", "application/json") 46 | self.write(jcfg) 47 | 48 | 49 | def _repo_label_from_binder_request(): 50 | try: 51 | repotype = os.getenv("BINDER_PERSISTENT_REQUEST", "").split("/")[1] 52 | except IndexError: 53 | return "" 54 | if repotype == "gh": 55 | return "GitHub" 56 | if repotype == "gl": 57 | return "GitLab" 58 | return repotype.capitalize() 59 | 60 | 61 | class OfflineNotebookConfig(Configurable): 62 | """ 63 | Holds server-side configuration 64 | """ 65 | 66 | repository_id = Callable( 67 | default_value=lambda: os.getenv("BINDER_REPO_URL", ""), 68 | help=""" 69 | A callable that returns the repository ID. 70 | This is used when storing and retrieving notebooks. 71 | Default is the value of the `BINDER_REPO_URL` environment variable. 72 | """, 73 | ).tag(config=True) 74 | 75 | repository_label = Callable( 76 | default_value=_repo_label_from_binder_request, 77 | help=""" 78 | A callable that returns the repository label. 79 | Default is to parse the `BINDER_PERSISTENT_REQUEST` environment 80 | variable. 81 | """, 82 | ).tag(config=True) 83 | 84 | repository_ref_url = Callable( 85 | default_value=lambda: os.getenv("BINDER_REF_URL", ""), 86 | help=""" 87 | A callable that returns the persistent Binder URL. 88 | Default is the value of the `BINDER_REF_URL` environment variable. 89 | """, 90 | ).tag(config=True) 91 | 92 | binder_persistent_url = Callable( 93 | default_value=lambda: ( 94 | os.getenv("BINDER_LAUNCH_HOST", "") 95 | + os.getenv("BINDER_PERSISTENT_REQUEST", "") 96 | ), 97 | help=""" 98 | A callable that returns the repository reference URL. 99 | Default is the values of the `BINDER_LAUNCH_HOST` and 100 | `BINDER_PERSISTENT_REQUEST` environment variables. 101 | """, 102 | ).tag(config=True) 103 | 104 | 105 | def _jupyter_server_extension_paths(): 106 | """ 107 | Jupyter server extension 108 | """ 109 | return [{"module": "jupyter_offlinenotebook"}] 110 | 111 | 112 | def _jupyter_nbextension_paths(): 113 | """ 114 | Jupyter notebook extension 115 | """ 116 | return [ 117 | dict( 118 | section="notebook", 119 | src="./static", 120 | dest="jupyter-offlinenotebook", 121 | require="jupyter-offlinenotebook/main", 122 | ) 123 | ] 124 | 125 | 126 | def load_jupyter_server_extension(nbapp): 127 | """ 128 | Called during notebook start 129 | """ 130 | nbapp.web_app.settings["offline_notebook_config"] = OfflineNotebookConfig( 131 | parent=nbapp 132 | ) 133 | route_pattern = url_path_join( 134 | nbapp.web_app.settings["base_url"], "/offlinenotebook/config" 135 | ) 136 | nbapp.web_app.add_handlers(".*", [(route_pattern, OfflineNotebookHandler)]) 137 | 138 | 139 | # Add compatibility with jupyter-server 140 | # https://jupyter-server.readthedocs.io/en/latest/developers/extensions.html#migrating-an-extension-to-use-jupyter-serverjupyter_server 141 | _load_jupyter_server_extension = load_jupyter_server_extension 142 | _jupyter_server_extension_points = _jupyter_server_extension_paths 143 | -------------------------------------------------------------------------------- /jupyter_offlinenotebook/description.yaml: -------------------------------------------------------------------------------- 1 | Type: Jupyter Notebook Extension 2 | Compatibility: 6.x 3 | Name: Offline Notebook 4 | Main: main.js 5 | Link: README.md 6 | Description: | 7 | Save, load and download notebooks to local-storage in your browser. 8 | Parameters: 9 | - none 10 | -------------------------------------------------------------------------------- /jupyter_offlinenotebook/etc/offlinenotebook_jpserverextension.json: -------------------------------------------------------------------------------- 1 | { 2 | "ServerApp": { 3 | "jpserver_extensions": { 4 | "jupyter_offlinenotebook": true 5 | } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /jupyter_offlinenotebook/etc/offlinenotebook_nbextension.json: -------------------------------------------------------------------------------- 1 | { 2 | "load_extensions": { 3 | "jupyter-offlinenotebook/main": true 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /jupyter_offlinenotebook/etc/offlinenotebook_nbserverextension.json: -------------------------------------------------------------------------------- 1 | { 2 | "NotebookApp": { 3 | "nbserver_extensions": { 4 | "jupyter_offlinenotebook": true 5 | } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /jupyter_offlinenotebook/static/main.js: -------------------------------------------------------------------------------- 1 | // TODO: Convert to typescript 2 | define([ 3 | 'base/js/namespace', 4 | 'base/js/events', 5 | 'base/js/utils', 6 | 'base/js/dialog', 7 | 'jquery', 8 | './jslib/offlinenotebook', 9 | ], function (Jupyter, events, utils, dialog, $, offline) { 10 | var initialise = function () { 11 | $.getJSON( 12 | utils.get_body_data('baseUrl') + 'offlinenotebook/config', 13 | function (data) { 14 | offline.initialise(data); 15 | addButtons(); 16 | }, 17 | ); 18 | }; 19 | 20 | var addButtons = function () { 21 | Jupyter.actions.register( 22 | { 23 | help: 'Download visible', 24 | icon: 'fa-download', 25 | handler: downloadNotebookFromBrowser, 26 | }, 27 | 'offline-notebook-download', 28 | 'offlinenotebook', 29 | ); 30 | Jupyter.actions.register( 31 | { 32 | help: 'Save to browser storage', 33 | icon: 'fa-cloud-download', 34 | handler: localstoreSaveNotebook, 35 | }, 36 | 'offline-notebook-save', 37 | 'offlinenotebook', 38 | ); 39 | Jupyter.actions.register( 40 | { 41 | help: 'Restore from browser storage', 42 | icon: 'fa-cloud-upload', 43 | handler: localstoreLoadNotebook, 44 | }, 45 | 'offline-notebook-load', 46 | 'offlinenotebook', 47 | ); 48 | 49 | var repoIcons = { 50 | GitHub: 'fa-github', 51 | GitLab: 'fa-gitlab', 52 | Git: 'fa-git', 53 | }; 54 | Jupyter.actions.register( 55 | { 56 | help: 'Visit Binder repository', 57 | icon: repoIcons[offline.repoLabel()] || 'fa-external-link', 58 | handler: offline.openBinderRepo, 59 | }, 60 | 'offline-notebook-binderrepo', 61 | 'offlinenotebook', 62 | ); 63 | Jupyter.actions.register( 64 | { 65 | help: 'Link to this Binder', 66 | icon: 'fa-link', 67 | handler: showBinderLink, 68 | }, 69 | 'offline-notebook-binderlink', 70 | 'offlinenotebook', 71 | ); 72 | 73 | var buttons = [ 74 | { 75 | action: 'offlinenotebook:offline-notebook-download', 76 | label: 'Download', 77 | }, 78 | ]; 79 | if (offline.repoid()) { 80 | buttons.push('offlinenotebook:offline-notebook-save'); 81 | buttons.push('offlinenotebook:offline-notebook-load'); 82 | } 83 | Jupyter.toolbar.add_buttons_group(buttons); 84 | 85 | var binderButtons = []; 86 | if (offline.binderRefUrl()) { 87 | binderButtons.push({ 88 | action: 'offlinenotebook:offline-notebook-binderrepo', 89 | label: offline.repoLabel(), 90 | }); 91 | } 92 | if (offline.binderPersistentUrl()) { 93 | binderButtons.push({ 94 | action: 'offlinenotebook:offline-notebook-binderlink', 95 | label: 'Binder', 96 | }); 97 | } 98 | if (binderButtons) { 99 | Jupyter.toolbar.add_buttons_group(binderButtons); 100 | } 101 | }; 102 | 103 | function modalDialog(title, body, displayclass, buttons) { 104 | if (displayclass) { 105 | body.addClass(displayclass); 106 | } 107 | if (!buttons) { 108 | buttons = { 109 | OK: { class: 'btn-primary' }, 110 | }; 111 | } 112 | dialog.modal({ 113 | title: title, 114 | body: body, 115 | buttons: buttons, 116 | }); 117 | } 118 | 119 | function formatRepoPathforDialog(path) { 120 | var displayRepoid = $('
').append( 121 | $('', { 122 | text: 'repoid: ', 123 | }).append( 124 | $('', { 125 | text: offline.repoid(), 126 | }), 127 | ), 128 | ); 129 | var displayPath = $('
').append( 130 | $('', { 131 | text: 'path: ', 132 | }).append( 133 | $('', { 134 | text: path, 135 | }), 136 | ), 137 | ); 138 | return displayRepoid.append(displayPath); 139 | } 140 | 141 | function getNotebookFromBrowser() { 142 | return Jupyter.notebook.toJSON(); 143 | } 144 | 145 | function localstoreSaveNotebook() { 146 | var path = Jupyter.notebook.notebook_path; 147 | var nb = getNotebookFromBrowser(); 148 | var repopathDisplay = formatRepoPathforDialog(path); 149 | offline.saveNotebook( 150 | path, 151 | nb, 152 | function (key) { 153 | console.log('offline-notebook saved: ', key); 154 | modalDialog('Notebook saved to browser storage', repopathDisplay); 155 | }, 156 | function (e) { 157 | var body = repopathDisplay.append( 158 | $('
', { 159 | text: e, 160 | }), 161 | ); 162 | modalDialog( 163 | 'Local storage IndexedDB error', 164 | body, 165 | 'alert alert-danger', 166 | ); 167 | throw e; 168 | }, 169 | ); 170 | } 171 | 172 | function localstoreLoadNotebook() { 173 | var path = Jupyter.notebook.notebook_path; 174 | var key = 'repoid:' + offline.repoid() + ' path:' + path; 175 | offline.loadNotebook( 176 | path, 177 | function (nb) { 178 | var repopathDisplay = formatRepoPathforDialog(path); 179 | if (nb) { 180 | console.log('offline-notebook found ' + key); 181 | modalDialog( 182 | 'This will replace your current notebook with', 183 | repopathDisplay, 184 | null, 185 | { 186 | OK: { 187 | class: 'btn-primary', 188 | click: function () { 189 | Jupyter.notebook.fromJSON(nb); 190 | console.log('offline-notebook loaded ' + key); 191 | }, 192 | }, 193 | Cancel: {}, 194 | }, 195 | ); 196 | } else { 197 | console.log('offline-notebook not found ' + key); 198 | modalDialog( 199 | 'Notebook not found in browser storage', 200 | repopathDisplay, 201 | 'alert alert-danger', 202 | ); 203 | } 204 | }, 205 | function (e) { 206 | var body = $('
') 207 | .append( 208 | $('
', { 209 | text: key, 210 | }), 211 | ) 212 | .append( 213 | $('
', { 214 | text: e, 215 | }), 216 | ); 217 | modalDialog( 218 | 'Local storage IndexedDB error', 219 | body, 220 | 'alert alert-danger', 221 | ); 222 | throw e; 223 | }, 224 | ); 225 | } 226 | 227 | function downloadNotebookFromBrowser() { 228 | var name = Jupyter.notebook.notebook_name; 229 | var nb = getNotebookFromBrowser(); 230 | offline.downloadNotebookFromBrowser(name, nb); 231 | } 232 | 233 | // https://github.com/jupyterhub/binderhub/blob/b32ad4425be3319f7a2c59cf8253e979512b955d/examples/appendix/static/custom.js#L1-L7 234 | function copy_link_into_clipboard(b) { 235 | var $temp = $(''); 236 | $(b).parent().append($temp); 237 | $temp.val($(b).data('url')).select(); 238 | document.execCommand('copy'); 239 | $temp.remove(); 240 | } 241 | 242 | function showBinderLink() { 243 | var binderUrl = 244 | offline.binderPersistentUrl() + 245 | '?filepath=' + 246 | encodeURIComponent(Jupyter.notebook.notebook_path); 247 | var body = $('
', { 248 | style: 'display: flex;', 249 | }).append( 250 | $('
', {
251 |         text: binderUrl,
252 |         style: 'flex-grow: 1; margin: 0;',
253 |       }),
254 |     );
255 |     var button = $('