neptune-notebook

1.3.1 • Public • Published

Neptune-notebook.js

Interactive Notebook for Javascript and Node.js

Overview

Neptune allows you to write interactive javascript documents using markdown. A document can contain any markdown features (text, images, links, code blocks, etc), including "special" neptune code blocks that can be executed interactively when the document is open. Neptune automatically generates HTML code from the input markdown source, and manages the interactive code blocks.

Currently the following features are supported:

  1. Covers all markdown features using showdown.
  2. (editable) Javascript, CSS, and HTML interactive code blocks.
  3. Combining related code blocks in a tab layout.
  4. Specifying the scope of each code block, so that code blocks with different scopes are totally independent and do not share any variables, while code blocks with the same scope share state (the effects of running one is visible when running any of the others).
  5. Easy output display using Console.log which mimics the standard console.log.
  6. Manual output display in a dedicated div following a code block for plots or other complex output formats.
  7. Injecting HTML, CSS, and Javascript directly into document for customized styling or behavior.
  8. Can run interactive node.js code via the server.
  9. Dumps the output into a single HTML file for static hosting if desired (interactive code will not run).
  10. Save the current state of an interactive document as it is running: particularly, any console or html/visual outputs.

Interactive Code Blocks

Interactive code blocks are defined in the markdown source using:

```neptune[<options>]
<code>
<code>
...
```

Where options is on the form: :,:,...,:

Note: options cannot include white spaces.

The following options are supported:

  1. language can be one of javascript, css, or html, defaults to javascript.
  2. inject if given any value, then this code block will be injected into the HTML code at the current position, if not provided, the code will be displayed in an interactive block but not injected. If this attribute is provided, neptune will ignore any of the next attributes.
  3. title specifies the title of the code block.
  4. scope specifies the scope of the code block, all code blocks with the same given scope will share state. Blocks without a specified scope share state.
  5. frame all code blocks with the same frame value will be displayed together in a tab layout. The layout is located in the position of the first code block that belongs to the frame. If not given, the code block will displayed stand alone.
  6. env can be browser or server, defaults to browser, specifies where the code will be run.
  7. output if provided, neptune will create an empty div with the given value as an id right after the code block. This div can be used by the code block to display custom HTML output.
  8. offline if set to false, neptune will not allow running this code block in offline mode (i.e. dumped HTML or saved documents).
  9. run if set to false, neptune will not allow this code block to run, and will not display a run icon near it.
  10. dropdown if set to true, neptune will not display all of the tab headers of the blocks in the frame, instead it will show a bars icon that displays a drop down menu from which a tab can be chosen when clicked, if set to false, tab headers will always be displayed and there will be no drop down menu. If this is not set, neptune will automatically determine whether to use a drop down menu or not, depending on the size of the tab headers and the width of the screen.

Important Pitfalls for Interactive Code Blocks

Reservered variables: All interactive code blocks are isolated from most of the neptune client side code.

The following neptune internal variables are visible to code running inside code blocks, these variables are unsafe to modify by interactive code, doing so may cause failures.

$__scopes__$, $__logMiddlewareBrowser__$, $__logMiddlewareServer__$, $__eval__$, $__code__$
Console // contains one attribute: 'log' which mimics console.log but displays output inside the HTML page, this can be used safetly, but not modified.

Server-side require: Server/node.js interactive code blocks run on the server inside a dedicated wrapper module. By default, using require('') in such a situtation causes node.js to look for the module relative to the path of the wrapper (in neptune ./node_modules/neptune-notebook/src/statics/browserify/).

Requiring dependencies installed via npm (both local and global) are uneffected by this behavior. However, requiring user created files may be affected since their path now must be provided relative to our wrapper module. For such cases, the Require wrapper can be used, indicating that the required path should be resolved relative to the path of the main entry point of the application.

/*
 * assume the following was run to execute neptune
 * cd /home/somewhere/project
 * node index.js
 */
 
// resolves to /home/somewhere/project/dep/mylib.js
var mylib = Require('dep/mylib.js');
 
// resolves to /home/somewhere/project/node_modules/neptune-notebook/.../dep/mylib.js
// which does not exist
var mylib = require('dep/mylib.js');

Running Neptune

You can install neptune with:

npm install neptune-notebook

After installation, you can run a neptune server as follows:

var Neptune = require('neptune-notebook'); // require neptune server-side code
 
var neptune = new Neptune(); // create a new server
neptune.addDocument('document-name1', 'path/to/markdown1', [<auto-refresh>=false], [<injected-JS-files>=[]], [<injected-CSS-files>=[]], [<injected-HTML-files>=[]], [decomment=true]);
neptune.addDocument('document-name2', 'path/to/markdown2', [<auto-refresh>=false], [<injected-JS-files>=[]], [<injected-CSS-files>=[]], [<injected-HTML-files>=[]], [decomment=true]);
...
 
// Static serving: Dump document as an HTML file
neptune.writeHTML('document-name1', 'path/to/output/html');
 
// Dynamic serving: supports server-side interactive code block
neptune.start(<port number>); // neptune will log to the console the urls for each document

Where:

  1. <auto-refresh>: an optional boolean parameter, if set to true, it will automatically render any changes to the markdown source whenever the document is accessed with the browser. By default, this is disabled.
  2. <injected-*-files>: an array of (javascript, CSS, or HTML) file paths to inject statically into the page, look at the Injecting Code Into Output Document section below for more details.
  3. <decomment>: an optional boolean parameter, if set to true, neptune will strip out comments from injected javascript files to avoid potential issues with special characters / script termination.

Note: you can choose to either statically or dynamically serve the output document (or both).

Injecting Code Into Output Document

Neptune supports two approaches for injecting code into the output document. Code can be injected statically (prior to the output HTML generation) or dynamically (with client side javascript that executes when the page is loaded).

Static injection is useful for injecting styling rules or external javascript dependencies. It is restricted in that it can only inject code in fixed locations in the code. Namely, HTML and CSS code are injected at the end of body, while Javascript code is injected at the end of inside a

Package Sidebar

Install

npm i neptune-notebook

Weekly Downloads

17

Version

1.3.1

License

MIT

Unpacked Size

590 kB

Total Files

28

Last publish

Collaborators

  • kinanbab