Skip to content

Commit

Permalink
Squashed 'nbviewer.js/' content from commit 6f48d7c
Browse files Browse the repository at this point in the history
git-subtree-dir: nbviewer.js
git-subtree-split: 6f48d7c308a3d6547814b54bc14d29efe62d2dac
  • Loading branch information
tuxu committed Jan 27, 2017
0 parents commit 2fecfe4
Show file tree
Hide file tree
Showing 9 changed files with 639 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
archive/*
sample-notebooks/*
*.swp
vendor/
TODO.txt
wip/*
.DS_Store
dist/*
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2016 Ondrej Kokes

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 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.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
### client side rendering of jupyter notebooks

*tl;dr: Render Jupyter notebooks straight in the browser, without a back end converter. Can be used as a library.*

I often want to read through my Jupyter notebooks, but I rarely have my Jupyter instances running in the right folders. I can't quite use the [online nbviewer](http://nbviewer.jupyter.org/), because I don't have a public URL for these, so I resort to running dummy Jupyter instances or uploading my file as a one-time gist on Github (one I have to delete thereafter). One last possibility is `nbconvert` in the command line.

I thought it could be easier and more lightweight. So I hacked together this client side rendering of Jupyter notebooks. All you need is a browser that renders an HTML file (dependent on some JavaScript and CSS, both of which can be inlined). You simply drag and drop an `.ipynb` file and it renders. It's rather fast and it supports most notebook features. I tried supporting the previous version (v3), since there are tons of examples in this version out there.

[**Try a live demo**](https://kokes.github.io/nbviewer.js/viewer.html)

#### Usage

There are two ways one can use this. You can use the library itself, there is just a single public method, you call `nbv.render(data, target)`, where `data` is the JSON representation of your Jupyter notebook and `target` is the node where the notebook is to be rendered.

Or you can use [the demo](https://kokes.github.io/nbviewer.js/viewer.html) (or a local copy), which is just a simple wrapper of the library, with dropzones and other basic features. There is no data being transferred anywhere, so feel free to bookmark it and use it.

Two immediate use cases come to mind:

1. In Mac OS, you can preview files and this system, [Quick Look](https://support.apple.com/kb/PH21920?locale=en_US), supports plugins. It would be rather handy to have a notebook preview one keystroke away.
2. Ever since GitHub introduced notebook rendering last year, Gitlab users have been [requesting the same](https://gitlab.com/gitlab-org/gitlab-ce/issues/2508). Gitlab itself recently dropped Python as a dependency, so its reintroduction just for converting notebooks is rather unlikely. A browser-based solution like this could be a good substitute.

### Tech details
It's rather simple at this point, all the DOM manipulation is written in vanilla JavaScript, Markdown rendering goes through [marked.js](https://github.com/chjj/marked), syntax highlighting is administered by [Prism.js](http://prismjs.com/). The example implementation leverages a few goodies from modern web design, like File API or drag&drops, so a fairly modern browser is necessary.

#### Showcase

![screencast](https://dl.dropboxusercontent.com/u/5758323/nbviewer-js/preview.gif)


#### Contact

Drop me an email ([email protected]) or tweet at me ([@pndrej](https://twitter.com/pndrej)) if you have any questions or suggestions. Contributions welcome.
7 changes: 7 additions & 0 deletions cmd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Render a notebook upon a double click. **Currently extremely experimental.**

You'll need `bash` and a working `Go` environment. Then just launch the build script, `./build.sh`, and you're good to go. You may want to comment out some of the cross compilation lines.

TODO:
- figure out a way to launch it as default on a mac/unix
- add link to repo at the end of the HTML, so people know where to complain (also that this is a pre-alpha thing)
13 changes: 13 additions & 0 deletions cmd/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
js=`cat ../lib/nbv.js | base64 -w 0`
html=`cat ../lib/scaffold.html | base64 -w 0`

cat stub.go | sed "s|%js%|$js|g" | sed "s|%html%|$html|g" > nbview.go

mkdir ../dist

GOOS=windows GOARCH=amd64 go build -ldflags -w -o ../dist/nbview.exe nbview.go
GOOS=darwin GOARCH=amd64 go build -ldflags -w -o ../dist/nbview_darwin nbview.go
GOOS=linux GOARCH=amd64 go build -ldflags -w -o ../dist/nbview_linux nbview.go

rm nbview.go
63 changes: 63 additions & 0 deletions cmd/stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"encoding/base64"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
)

func main() {
if len(os.Args) != 2 {
log.Fatal("Not supplied a filename")
}

// read input notebook
fn := os.Args[1]
cn, err := ioutil.ReadFile(fn)
if err != nil {
log.Fatal(err)
}

tdir, err := ioutil.TempDir("", "nb_")
if err != nil {
log.Fatal(err)
}

dt := map[string][]byte{
"index.html": []byte(`%html%`),
"nbv.js": []byte(`%js%`),
}

for fn, vl := range dt {
pt, _ := base64.StdEncoding.DecodeString(string(vl))
if err := ioutil.WriteFile(filepath.Join(tdir, fn), pt, 0666); err != nil {
log.Fatal(err)
}
}
if err := ioutil.WriteFile(filepath.Join(tdir, "notebook.js"), append([]byte("var nb = "), cn...), 0666); err != nil {
log.Fatal(err)
}

// launch browser
tfn := filepath.Join(tdir, "index.html")

var eerr error
switch runtime.GOOS {
case "windows":
eerr = exec.Command("cmd", "/c", "start", tfn).Start()
case "darwin":
eerr = exec.Command("open", tfn).Start()
case "linux", "freebsd", "netbsd", "openbsd":
eerr = exec.Command("xdg-open", tfn).Start()
default:
log.Fatalf("%s not supported", runtime.GOOS)
}
if eerr != nil {
log.Fatal(eerr)
}

}
Loading

0 comments on commit 2fecfe4

Please sign in to comment.