Skip to content
This repository has been archived by the owner on May 3, 2018. It is now read-only.

Commit

Permalink
Merge pull request #22 from gnestor/0.17
Browse files Browse the repository at this point in the history
0.17.0
  • Loading branch information
ellisonbg authored Mar 2, 2017
2 parents 1b26a94 + 2b0a4e4 commit 582a393
Show file tree
Hide file tree
Showing 20 changed files with 386 additions and 240 deletions.
7 changes: 7 additions & 0 deletions .cookiecutter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
default_context:
author_name: "Grant Nestor"
author_email: "[email protected]"
mime_type: "application/vnd.vega.v2+json"
file_extension: "vg"
mime_short_name: "Vega"
extension_name: "jupyterlab_vega"
18 changes: 14 additions & 4 deletions .gitignore
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
jupyterlab_vega/static/
jupyterlab_vega/__pycache__/
jupyterlab_vega.egg-info
node_modules/
labextension/lib/
nbextension/lib/
nbextension/embed/
*/node_modules/
npm-debug.log
*.egg-info/
build/
dist/

# Compiled javascript
jupyterlab_vega/__pycache__/
jupyterlab_vega/static/

# OS X
.DS_Store
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A JupyterLab and Jupyter Notebook extension for rendering Vega and Vega-lite

## Prerequisites

* JupyterLab ^0.16.0 and/or Notebook >=4.3.0
* JupyterLab ^0.17.0 and/or Notebook >=4.3.0

## Usage

Expand Down
9 changes: 4 additions & 5 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ This document guides an extension maintainer through creating and publishing a r

## Update version number

Update the version number in `setup.py` and in `package.json`.
Update the version number in `setup.py`, `labextension/package.json`, and `nbextension/package.json`.

## Remove generated files

Remove old Javascript bundle builds and delete the `dist/` folder to remove old Python package builds:
Remove old Javascript bundle and Python package builds:

```bash
npm run clean
rm -rf dist/
rm -rf jupyterlab_vega/static
```

## Build the package

Build the Javascript extension bundle, then build the Python package and wheel:

```bash
npm run build
bash build.js
python setup.py sdist
python setup.py bdist_wheel --universal
```
Expand Down
44 changes: 42 additions & 2 deletions jupyterlab_vega/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,48 @@ def _ipython_display_(self):
'application/vnd.vega.v2+json': prepare_vega_spec(self.spec, self.data),
'text/plain': '<jupyterlab_vega.Vega object>'
}
metadata = {
'application/vnd.vega.v2+json': self.metadata
display(bundle, raw=True)


class VegaLite(Vega):
"""VegaLite expects a spec (a JSON-able dict) and data (JSON-able list or pandas DataFrame) argument
not already-serialized JSON strings.
Scalar types (None, number, string) are not allowed, only dict containers.
"""

def __init__(self, spec=None, data=None, url=None, filename=None, metadata=None):
"""Create a VegaLite display object given raw data.
Parameters
----------
spec : dict
VegaLite spec. Not an already-serialized JSON string.
data : dict or list
VegaLite data. Not an already-serialized JSON string.
Scalar types (None, number, string) are not allowed, only dict
or list containers.
url : unicode
A URL to download the data from.
filename : unicode
Path to a local file to load the data from.
metadata: dict
Specify extra metadata to attach to the json display object.
"""

super(VegaLite, self).__init__(spec=spec, data=data, url=url, filename=filename)

def _check_data(self):
if self.spec is not None and not isinstance(self.spec, dict):
raise TypeError("%s expects a JSONable dict, not %r" % (self.__class__.__name__, self.spec))
if self.data is not None and not isinstance(self.data, (list, pd.DataFrame)):
raise TypeError("%s expects a JSONable list or pandas DataFrame, not %r" % (self.__class__.__name__, self.data))

def _ipython_display_(self):
bundle = {
'application/vnd.vegalite.v1+json': prepare_vegalite_spec(self.spec, self.data),
'text/plain': '<jupyterlab_vega.VegaLite object>'
}
display(bundle, metadata=metadata, raw=True)

Expand Down
4 changes: 1 addition & 3 deletions labextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ A JupyterLab extension for rendering Vega

## Prerequisites

* `jupyterlab@^0.16.0`

![file renderer](http://g.recordit.co/cbf0xnQHKn.gif)
* `jupyterlab@^0.17.0`

## Development

Expand Down
41 changes: 30 additions & 11 deletions labextension/build_extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,45 @@ var path = require('path');

buildExtension({
name: 'jupyterlab_vega',
entry: './src/plugin.js',
outputDir: '../jupyterlab_vega/static',
entry: path.join(__dirname, 'src', 'plugin.js'),
outputDir: path.join(
__dirname,
'..',
'jupyterlab_vega',
'static'
),
useDefaultLoaders: false,
config: {
module: {
loaders: [
{ test: /\.html$/, loader: 'file-loader' },
{ test: /\.(jpg|png|gif)$/, loader: 'file-loader' },
{ test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: 'url-loader?limit=10000&mimetype=application/font-woff' },
{ test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: 'url-loader?limit=10000&mimetype=application/font-woff' },
{ test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url-loader?limit=10000&mimetype=application/octet-stream' },
{
test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=application/font-woff'
},
{
test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=application/font-woff'
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=application/octet-stream'
},
{ test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file-loader' },
{ test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url-loader?limit=10000&mimetype=image/svg+xml' },
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=image/svg+xml'
},
{ test: /\.json$/, loader: 'json-loader' },
{ test: /\.js$/,
exclude: /node_modules(?!\/jupyterlab_vega_react)/,
{
test: /\.js$/,
include: [
path.join(__dirname, 'src'),
path.join(__dirname, 'node_modules', 'jupyterlab_vega_react')
],
loader: 'babel-loader',
query: {
presets: ['latest', 'stage-0', 'react']
}
query: { presets: [ 'latest', 'stage-0', 'react' ] }
}
]
}
Expand Down
11 changes: 5 additions & 6 deletions labextension/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "jupyterlab_vega_labextension",
"version": "0.16.0",
"version": "0.17.0",
"description": "A JupyterLab extension for rendering Vega",
"author": "Grant Nestor <[email protected]>",
"main": "lib/plugin.js",
Expand All @@ -12,7 +12,7 @@
],
"scripts": {
"build": "node build_extension.js",
"watch": "watch \"npm run build\" src ../component --wait 10 --ignoreDotFiles",
"watch": "watch \"npm install\" src ../component --wait 10 --ignoreDotFiles",
"preinstall": "npm install ../component",
"prepublish": "npm run build",
"extension:install": "jupyter labextension install --symlink --py --sys-prefix jupyterlab_vega",
Expand All @@ -21,20 +21,19 @@
"extension:disable": "jupyter labextension disable --py --sys-prefix jupyterlab_vega"
},
"dependencies": {
"jupyterlab": "^0.16.0",
"phosphor": "^0.7.0",
"jupyterlab": "^0.17.0",
"@phosphor/algorithm": "^0.1.0",
"@phosphor/widgets": "^0.1.3",
"react": "^15.3.2",
"react-dom": "^15.3.2"
},
"devDependencies": {
"@jupyterlab/extension-builder": ">=0.10.0",
"@jupyterlab/services": ">=0.25.0",
"babel-core": "^6.18.2",
"babel-loader": "^6.2.7",
"babel-preset-latest": "^6.16.0",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",
"rimraf": "^2.5.4",
"watch": "^1.0.1"
}
}
65 changes: 49 additions & 16 deletions labextension/src/doc.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Widget } from 'phosphor/lib/ui/widget';
import { Widget } from '@phosphor/widgets';
import { ABCWidgetFactory } from 'jupyterlab/lib/docregistry';
import { ActivityMonitor } from 'jupyterlab/lib/common/activitymonitor';
import React from 'react';
import ReactDOM from 'react-dom';
import Vega from 'jupyterlab_vega_react';

/**
* The class name added to this DocWidget.
* The class name added to a DocWidget.
*/
const CLASS_NAME = 'jp-DocWidgetVega';

Expand All @@ -18,8 +18,7 @@ const RENDER_TIMEOUT = 1000;
/**
* A widget for rendering jupyterlab_vega files.
*/
class DocWidget extends Widget {

export class DocWidget extends Widget {
constructor(context) {
super();
this._context = context;
Expand Down Expand Up @@ -55,14 +54,51 @@ class DocWidget extends Widget {
onUpdateRequest(msg) {
this.title.label = this._context.path.split('/').pop();
if (this.isAttached) {
let content = this._context.model.toString();
const json = content ? JSON.parse(content) : {};
const path = this._context._path;
const props = {
data: json,
embedMode: path.includes('.vl') ? 'vega-lite' : 'vega'
};
ReactDOM.render(<Vega {...props} />, this.node);
const content = this._context.model.toString();
try {
const data = JSON.parse(content);
const path = this._context._path;
const props = {
data,
embedMode: path.includes('.vl') ? 'vega-lite' : 'vega'
};
ReactDOM.render(<Vega {...props} />, this.node);
} catch (error) {
const ErrorDisplay = props => (
<div
className="jp-RenderedText jp-mod-error"
style={{
width: '100%',
minHeight: '100%',
textAlign: 'center',
padding: 10,
boxSizing: 'border-box'
}}
>
<span
style={{
fontSize: 18,
fontWeight: 500
}}
>{props.message}</span>
<pre
style={{
textAlign: 'left',
padding: 10,
overflow: 'hidden'
}}
>{props.content}</pre>
</div>
);

ReactDOM.render(
<ErrorDisplay
message="Invalid JSON"
content={content}
/>,
this.node
);
}
}
}

Expand All @@ -72,14 +108,12 @@ class DocWidget extends Widget {
onAfterAttach(msg) {
this.update();
}

}

/**
* A widget factory for DocWidget.
*/
export class VegaDoc extends ABCWidgetFactory {

constructor(options) {
super(options);
}
Expand All @@ -88,9 +122,8 @@ export class VegaDoc extends ABCWidgetFactory {
* Create a new widget given a context.
*/
createNewWidget(context, kernel) {
let widget = new DocWidget(context);
const widget = new DocWidget(context);
this.widgetCreated.emit(widget);
return widget;
}

}
6 changes: 3 additions & 3 deletions labextension/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
*/

.jp-OutputWidgetVega, .jp-DocWidgetVega {
padding: 0;
overflow-y: scroll;
padding: 5px 0;
}

.jp-OutputWidgetVega {

}

.jp-DocWidgetVega {

padding: 5px;
overflow: auto;
}
Loading

0 comments on commit 582a393

Please sign in to comment.