Skip to content

Commit

Permalink
Merge pull request #264 from jitseniesen/jupyterlab
Browse files Browse the repository at this point in the history
PR: Use JupyterLab to render notebooks
  • Loading branch information
jitseniesen authored May 13, 2020
2 parents f330ea8 + 371a682 commit 024de76
Show file tree
Hide file tree
Showing 27 changed files with 1,184 additions and 93 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/linux-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
strategy:
fail-fast: false
matrix:
PYTHON_VERSION: ['2.7', '3.6', '3.7', '3.8']
PYTHON_VERSION: ['3.6', '3.7', '3.8']
steps:
- name: Checkout branch
uses: actions/checkout@v2
Expand All @@ -42,14 +42,15 @@ jobs:
run: |
conda install nomkl -y -q
conda install -c spyder-ide --file requirements/tests.txt -y -q
- name: Install pytest-mock v1
# For some reason, pytest-mock=2 does not work with Python 2.7
if: matrix.PYTHON_VERSION == '2.7'
shell: bash -l {0}
run: conda install pytest-mock=1 -y -q
- name: Install Package
shell: bash -l {0}
run: pip install --no-deps -e .
- name: Set up notebook server
shell: bash -l {0}
run: |
cd spyder_notebook/server
jlpm install
jlpm build
- name: Show environment information
shell: bash -l {0}
run: |
Expand Down
13 changes: 7 additions & 6 deletions .github/workflows/macos-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
strategy:
fail-fast: false
matrix:
PYTHON_VERSION: ['2.7', '3.6', '3.7', '3.8']
PYTHON_VERSION: ['3.6', '3.7', '3.8']
steps:
- name: Checkout branch
uses: actions/checkout@v2
Expand All @@ -38,14 +38,15 @@ jobs:
run: |
conda install nomkl -y -q
conda install -c spyder-ide --file requirements/tests.txt -y -q
- name: Install pytest-mock v1
# For some reason, pytest-mock=2 does not work with Python 2.7
if: matrix.PYTHON_VERSION == '2.7'
shell: bash -l {0}
run: conda install pytest-mock=1 -y -q
- name: Install Package
shell: bash -l {0}
run: pip install --no-deps -e .
- name: Set up notebook server
shell: bash -l {0}
run: |
cd spyder_notebook/server
jlpm install
jlpm build
- name: Show environment information
shell: bash -l {0}
run: |
Expand Down
17 changes: 6 additions & 11 deletions .github/workflows/windows-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ jobs:
fail-fast: false
matrix:
PYTHON_VERSION: ['3.6', '3.7', '3.8']
# We skip Python 2.7 because pytest segfaults at the very end
# under Windows with Python 2.7
steps:
- name: Checkout branch
uses: actions/checkout@v2
Expand All @@ -38,23 +36,20 @@ jobs:
- name: Install test dependencies
shell: bash -l {0}
run: conda install -c spyder-ide --file requirements/tests.txt -y -q
- name: Install pytest-mock v1
# For some reason, pytest-mock=2 does not work with Python 2.7
if: matrix.PYTHON_VERSION == '2.7'
shell: bash -l {0}
run: conda install pytest-mock=1 -y -q
- name: Install Package
shell: bash -l {0}
run: pip install --no-deps -e .
- name: Set up notebook server
shell: bash -l {0}
run: |
cd spyder_notebook/server
jlpm install
jlpm build
- name: Show environment information
shell: bash -l {0}
run: |
conda info
conda list
- name: Set correct enconding
shell: bash -l {0}
if: matrix.PYTHON_VERSION == '2.7'
run: echo "::set-env name=PYTHONIOENCODING::cp1252"
- name: Run tests
shell: bash -l {0}
run: pytest spyder_notebook --cov=spyder_notebook -x -vv
Expand Down
99 changes: 99 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Contributing to the Spyder notebook plugin

:+1::tada:
First off, thanks for taking the time to contribute to the Spyder notebook
plugin!
:tada::+1:

## General guidelines for contributing

The Spyder notebook plugin is developed as part of the wider Spyder project.
In general, the guidelines for contributing to Spyder also apply here.
Specifically, all contributors are expected to abide by
[Spyder's Code of Conduct](https://github.com/spyder-ide/spyder/blob/master/CODE_OF_CONDUCT.md).

There are many ways to contribute and all are valued and welcome.
You can help other users, write documentation, spread the word, submit
helpful issues on the
[issue tracker](https://github.com/spyder-ide/spyder-notebook/issues)
with problems you encounter or ways to improve the plugin, test the development
version, or submit a pull request on GitHub.

The rest of this document explains how to set up a development environment.

## Setting up a development environment

This section explains how to set up a conda environment to run and work on the
development version of the Spyder notebook plugin.

### Creating a conda environment

This creates a new conda environment with the name `spydernb-dev`.

```bash
$ conda create -n spydernb-dev python=3.7
$ conda activate spydernb-dev
```

### Cloning the repository

This creates a new directory `spyder-notebook` with the source code of the
Spyder notebook plugin.

```bash
$ git clone https://github.com/spyder-ide/spyder-notebook.git
$ cd spyder-notebook
```

### Installing dependencies

This installs Spyder, JupyterLab and all other dependencies of the plugin into
the conda environment.

```bash
$ conda install --file requirements/conda.txt
```

### Building the notebook server

The Spyder notebook plugin includes a server which serves notebooks as HTML
pages. The following commands install the JavaScript dependencies of the
notebook server and build the server.

```bash
$ cd spyder_notebook/server
$ jlpm install
$ jlpm build
$ cd ../..
```

### Installing the plugin

This installs the Spyder notebook plugin so that Spyder will use it.

```bash
$ pip install --no-deps -e .
```

### Running Spyder

You are done! You can run Spyder as normal and it should load the notebook
plugin.

```bash
$ spyder
```

### Running Tests

This command installs the test dependencies in the conda environment.

```bash
$ conda install -c spyder-ide --file requirements/tests.txt
```

You can now run the tests with a simple

```bash
$ pytest
```
8 changes: 8 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


Server License
==============

The files in the directory spyder_notebook/server are adapted from an example
of the Jupyter Project and licensed under the revised BSD license. See the
file spyder_notebook/server/LICENSE for details.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ This project depends on:
Visit our [CHANGELOG](CHANGELOG.md) file to know more about our new features and improvements.

## Development and contribution
To start contributing to this project you can execute ``python setup.py install`` to test your changes on Spyder. We follow PEP8 and PEP257 style guidelines.

See the
[Contributing Guide](CONTRIBUTING.md)
for information on how to contribute to the Spyder notebook plugin, including
instructions for setting up a development environment.

## Contact

Expand Down
2 changes: 1 addition & 1 deletion requirements/conda.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
notebook
jupyterlab
spyder >=4.0.0b5
5 changes: 4 additions & 1 deletion spyder_notebook/notebookplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,10 @@ def create_new_client(self, filename=None, give_focus=True):
os.makedirs(NOTEBOOK_TMPDIR)
nb_name = 'untitled' + str(self.untitled_num) + '.ipynb'
filename = osp.join(NOTEBOOK_TMPDIR, nb_name)
nb_contents = nbformat.v4.new_notebook()
kernelspec = dict(display_name='Python 3 (Spyder)',
name='python3')
metadata = dict(kernelspec=kernelspec)
nb_contents = nbformat.v4.new_notebook(metadata=metadata)
nbformat.write(nb_contents, filename)
self.untitled_num += 1

Expand Down
33 changes: 33 additions & 0 deletions spyder_notebook/server/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
The files in this directory and its subdirectories are licensed under the
modified BSD license, as detailed below. They are adapted from the directory
examples/notebook in the JupyterLab repository at
https://github.com/jupyterlab/jupyterlab


Copyright (c) 2015 Project Jupyter Contributors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97 changes: 97 additions & 0 deletions spyder_notebook/server/data/data.json

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions spyder_notebook/server/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/

body {
background: white;
margin: 0;
padding: 0;
}

#main {
position: absolute;
top: 4px;
left: 4px;
right: 4px;
bottom: 4px;
}

.jp-NotebookPanel {
border-bottom: 1px solid #e0e0e0;
}

.notebookCommandPalette {
min-width: 225px;
}
24 changes: 24 additions & 0 deletions spyder_notebook/server/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!doctype html>
<html lang="en">
<head>
<title>Notebook Demo</title>
</head>
<body>
<script id='jupyter-config-data' type="application/json">
{{ config_data|tojson }}
</script>
<script src="{{config_data['frontendUrl'] | e}}bundle.js"></script>

<script type="text/javascript">
/* Remove token from URL. */
(function () {
var parsedUrl = new URL(window.location.href);
if (parsedUrl.searchParams.get('token')) {
parsedUrl.searchParams.delete('token');
window.history.replaceState({ }, '', parsedUrl.href);
}
})();
</script>

</body>
</html>
Binary file added spyder_notebook/server/jupyter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 71 additions & 0 deletions spyder_notebook/server/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
An example demonstrating a stand-alone "notebook".
Copyright (c) Jupyter Development Team.
Distributed under the terms of the Modified BSD License.
Example
-------
To run the example, see the instructions in the README to build it. Then
run ``python main.py``.
"""
import os
from jinja2 import FileSystemLoader
from notebook.base.handlers import IPythonHandler, FileFindHandler
from notebook.notebookapp import NotebookApp
from notebook.utils import url_path_join as ujoin

HERE = os.path.dirname(__file__)


class NotebookHandler(IPythonHandler):
"""
Serve a notebook file from the filesystem in the notebook interface
"""

def get(self, filename):
"""Get the main page for the application's interface."""
# Options set here can be read with PageConfig.getOption
config_data = {
# Use camelCase here, since that's what the lab components expect
'baseUrl': self.base_url,
'token': self.settings['token'],
'notebookPath': filename,
'frontendUrl': ujoin(self.base_url, 'static/'),
# FIXME: Don't use a CDN here
'mathjaxUrl': 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/'
'2.7.5/MathJax.js',
'mathjaxConfig': "TeX-AMS_CHTML-full,Safe"
}
return self.write(
self.render_template(
'index.html',
static=self.static_url,
base_url=self.base_url,
config_data=config_data
)
)

def get_template(self, name):
loader = FileSystemLoader(HERE)
return loader.load(self.settings['jinja2_env'], name)


class SpyderNotebookServer(NotebookApp):
def init_webapp(self):
"""initialize tornado webapp and httpserver.
"""
super(SpyderNotebookServer, self).init_webapp()

default_handlers = [
(ujoin(self.base_url, r'/notebook/(.*)'), NotebookHandler),
(ujoin(self.base_url, r"/static/(.*)"), FileFindHandler,
{'path': os.path.join(HERE, 'build')})
]
self.web_app.add_handlers('.*$', default_handlers)


if __name__ == '__main__':
SpyderNotebookServer.launch_instance()
Loading

0 comments on commit 024de76

Please sign in to comment.