• Stars
    star
    262
  • Rank 152,980 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Jupyter javascript plugin for static sites

Thebe takes the Jupyter (formerly ipython) front end, and make it work outside of the notebook context.

What? Why?

In short, this is an easy way to let users on a web page run code examples on a server.

Four things are required:

  1. A server, either a tmpnb server, for lots of users, or simply an ipython notebook server.
  2. A web page with some code examples
  3. A script tag in the page that includes the compiled javascript of Thebe, which is in this repo at static/main-built.js
  4. node-js and bower to install external javascript dependencies.

To install the javascript dependencies, simply run bower install in the root of the repository.

Here is a very simple example/demo. There are more complicated examples in the examples directory but you'll need to run a notebook server with the appropriate dependencies installed for them to work.

Also, Thebe is a moon of Jupiter in case you were wondering. Naming things is hard.

Tips and Default Shortcuts

shift+return executes the example that is currently focused shift+space moves the focus to the next example shift+clicking a run button will execute all preceding code examples as well as the current one ctrl+c works like a keyboard interrupt (in case of infinite loops and such)

You can change the first two by passing options to Thebe when you instantiate it (see below)

Front End Use

Currently Thebe is imported via an HTML script tag. Since Thebe is an alternative Jupyter notebook front-end, the notebook server version and ipywidgets version of the tmpnb instance are important and must be specified.
The following example will load Thebe for the notebook version 4.1.0 and ipywidgets 4.0.3:

<script src="http://cdn.jupyter.org/thebe/notebook4.1.0/ipywidgets4.0.3/static/main-built.js" type="text/javascript" charset="utf-8"></script>

or for the notebook version 3.x:

<script src="http://cdn.jupyter.org/thebe/notebook3.0.0/static/main-built.js" type="text/javascript" charset="utf-8"></script>

and then, to instantiate Thebe:

<script>
    $(function(){
        new Thebe({url:"http://some.tmpnb.server.com:8000"});
    });
</script>

Any pre tags with the data-executable attribute will be turned into editable, executable examples, and a run button will be added for each. Once run is clicked, Thebe will try to connect to the notebook server url you supplied, start the kernel, and execute the code in that example.

If append_kernel_controls_to is set to a dom selector, clicking run will also add kernel controls to that element, which will allow users to interrupt and restart the kernel. (Interrupting is equivalent to a keyboard interrupt, whereas restarting will lose all the user's variables and data.)

Opt-in auto instantiation: When loaded, the script will automatically start Thebe with the default options if the body has a data-runnable attribute that is set to true.

Options

You can override the below default options when you instantiate Thebe: Thebe(options)

    default_options =
      # jquery selector for elements we want to make runnable 
      selector: 'pre[data-executable]'
      # the url of either a tmnb server or a notebook server
      # (default url assumes user is running tmpnb via boot2docker)
      url: '//192.168.59.103:8000/'
      # is the url for tmpnb or for a notebook
      tmpnb_mode: true
      # the kernel name to use, must exist on notebook server
      kernel_name: "python2"
      # set to false to prevent kernel_controls from being added
      append_kernel_controls_to: false
      # Automatically inject basic default css we need, no highlighting
      inject_css: true
      # Automatically load other necessary css (jquery ui)
      load_css: true
      # Automatically load mathjax js
      load_mathjax: true
      # Default keyboard shortcut for focusing next cell, shift+ this keycode, default (32) is spacebar
      # Set to false to disable
      next_cell_shortcut: 32
      # Default keyboard shortcut for executing cell, shift+ this keycode, default (13) is return
      # Set to false to disable
      run_cell_shortcut: 13
      # For when you want a pre to become a CM instance, but not be runnable
      not_executable_selector: "pre[data-not-executable]"
      # For when you want a pre to become a CM instance, but not be writable
      read_only_selector: "pre[data-read-only]"
      # if set to false, no addendum added, if a string, use that instead
      error_addendum: true
      # adds interrupt to every cell control, when it's running
      add_interrupt_button: false
      # hack to set the codemirror mode correctly
      codemirror_mode_name: "ipython"
      # hack to set the codemirror theme
      codemirror_theme_name: "default"
      # totally different mode for running a terminal instead of a notebook
      terminal_mode: false
      # where are our cell elements (that are created from the selector option above)
      container_selector: "body"
      # for setting what docker image you want to run on the back end
      image_name: "jupyter/notebook"
      # should we remember the url that we connect to
      set_url_cookie: true
      # show messages from @log()
      debug: false

For example,

$(function(){
    var thebe = new Thebe({
      selector:"pre.cool",
      url: 'http://localhost:8888/'
    });
});

will make each pre tag with class cool runnable, and will try to connect with an ipython notebook server running locally at the ipython notebook default address and port.

Override with Query String

Because the url of your notebook server or tmpnb server may change quite often during development, you can override the url and tmpnb_mode options that are set when thebe is instantiated.

Do this by adding url and/or tmpnb_mode params as a query string to the page location. For instance, if you are serving a page at localhost:8000, accessing localhost:8000?url=https://tmp38.tmpnb.org in your browser instead will cause thebe to use https://tmp38.tmpnb.org rather than the options passed when thebe was instantiated. This is only for debugging and should not be used for production.

Terminal Mode

Thebe now includes terminal functionality, via termjs. To run a terminal in the browser, make sure you only have one instance of the selector (which defaults to pre[data-executable]) and set terminal_mode to true. Now, clicking on the element will connect and launch a terminal.

Terminal mode only works with the most up to date version of jupyter notebook. For now, you can run this docker image and it should work.

CodeMirror Language Modes

For simplicity, several codemirror language modes (i.e. syntax highlighting) are bundled with Thebe. Here's a list of them.

Run Locally (Simple)

The easiest way to get this running locally requires setting a few options in your Thebe declaration.

$(function(){
    var thebe = new Thebe({
        tmpnb_mode: false,
        url: 'http://localhost:8888/'
    });
});

to point to your server's address url option to the url of a running ipython notebook server, as above.

After installing ipython, run it like so:

ipython notebook  --NotebookApp.allow_origin='*' --no-browser

Which defaults to running at http://localhost:8888/, and should tell you that.

Developing the Front End

Most of the actual development takes place in static/main.coffee. I've tried to make as few changes to the rest of the jupyter front end as possible, but there were some cases where it was unavoidable (see #2 for more info on this).

After making a change to the javascript in static/, you'll need to recompile it to see your changes in built.html or to use it in production. index.html will reflect your changes that last r.js step.

npm install -g requirejs
npm install -g coffee-script
bower install
coffee -cbm .
cd static
r.js -o build.js baseUrl=. name=almond include=main out=main-built.js 

Run tmpnb Locally

First, you need docker (and boot2docker if you're on a OS X) installed.

Pull the images. This version is a slightly different fork of the main repo, which adds a API route for /spawn ( /api/spawn).

docker pull jupyter/configurable-http-proxy
docker pull zischwartz/tmpnb 
docker pull zischwartz/scipyserver

Then startup the docker containers for the proxy and tmpnb like so:

export TOKEN=$( head -c 30 /dev/urandom | xxd -p )

docker run --net=host -d -e CONFIGPROXY_AUTH_TOKEN=$TOKEN jupyter/configurable-http-proxy --default-target http://127.0.0.1:9999

docker run --net=host -d -e CONFIGPROXY_AUTH_TOKEN=$TOKEN -v /var/run/docker.sock:/docker.sock zischwartz/tmpnb python orchestrate.py --image="zischwartz/scipyserver" --allow_origin="*" --command="ipython3 notebook --no-browser --port {port} --ip=* --NotebookApp.allow_origin=* --NotebookApp.base_url={base_path}" --cull-timeout=300 --cull_period=60

Next, start a normal server in this directory

python -m SimpleHTTPServer

Or with python3,

python -m http.server

and visit it at localhost:8000 and localhost:8000/built.html

Run This Over https

Run the proxy like so

docker run --net=host -d -e CONFIGPROXY_AUTH_TOKEN=$TOKEN zischwartz/configurable-http-proxy configurable-http-proxy --default-target=http://127.0.0.1:9999 --ssl-key=key.pem --ssl-cert=cert.pem

And run the tmpnb image as above/usual. Then start up a local server that supports https, like this one with the -S option. It still won't work until you visit both the local server and the proxy address in your browser and say that you understand that the certs aren't valid and you want to proceed anyway.

More Repositories

1

Learning-OpenCV-3_examples

C++
2,237
star
2

open_government

Open Government, released as part of #PDFtribute
1,293
star
3

data_structures_and_algorithms_using_javascript

JavaScript
741
star
4

t-SNE-tutorial

A tutorial on the t-SNE learning algorithm
JavaScript
694
star
5

HTMLBook

Let's write books in HTML!
XSLT
631
star
6

etudes-for-elixir

Companion exercises for Introducing Elixir
Elixir
511
star
7

doing_data_science

This is the example code repository for Doing Data Science by Cathy O'Neil and Rachel Schutt (O'Reilly Media)
469
star
8

etudes-for-erlang

Companion exercises for O'Reilly Media's "Introducing Erlang"
Erlang
247
star
9

essential-sqlalchemy-2e

Jupyter Notebook
175
star
10

functional_thinking

This is the example code repository for Functional Thinking by Neal Ford (O'Reilly Media)
146
star
11

svg-essentials-examples

HTML
134
star
12

Using_SVG

Code examples and other supplementary material for the book Using SVG with CSS3 and HTML5.
HTML
105
star
13

iOS7_Programming_Cookbook

This is the example code repository for iOS 7 Programming Cookbook by Vandad Nahavandipoor (O'Reilly Media)
Objective-C
92
star
14

feedback_control_for_computer_systems

This is the example code repository for Feedback Control for Computer Systems by Philipp K. Janert (O'Reilly Media)
Python
87
star
15

building_maintainable_software

C#
86
star
16

97-things-every-agile-developer-should-know

97 Things Every Agile Software Developer Should Know
78
star
17

programming_rust

77
star
18

python_epiphanies

73
star
19

Learning_PHP

PHP
71
star
20

restful_java_jax-rs_2_0

This is the example code repository for RESTful Java with JAX-RS 2.0 by Bill Burke (O'Reilly Media)
65
star
21

java_cookbook_3e

This is the example code repository for Java Cookbook, 3E by Ian F. Darwin (O'Reilly Media)
63
star
22

docbook2asciidoc

XSL for transforming DocBook to AsciiDoc
XSLT
62
star
23

creating_apps_in_kivy

54
star
24

c_sharp_6_cookbook

53
star
25

decentralized_applications

53
star
26

bgp_in_the_data_center

Python
49
star
27

Data_Science_with_Java

Java
48
star
28

asciidoctor-htmlbook

Templates for the htmlbook backend for Asciidoctor
Ruby
44
star
29

ASP_NET-MVC-5-with-Bootstrap-and-Knockout_js

43
star
30

programming_hive

41
star
31

erlang_programming

This is the example code repository for Erlang Programming by Francesco Cesarini and Simon Thompson (O'Reilly Media)
41
star
32

c-in-a-nutshell-2E

39
star
33

atlas-cli

A command line for building and publishing (HTML only) Atlas projects
Go
36
star
34

learning-http2

29
star
35

backbone-gitlab

Backbone wrapper for the Gitlab API
JavaScript
28
star
36

hadoop_the_definitive_guide_4e

This is the Case Study repository for Hadoop: The Definitive Guide, 4E by Tom White (O'Reilly Media)
27
star
37

orm_book_samples

Sample book files for O'Reilly content
HTML
27
star
38

programming_javascript_applications

27
star
39

linux_pocket_guide

Shell
27
star
40

physics_for_game_developers_2e

26
star
41

SVG_Colors_Patterns_Gradients

Example code for the book SVG Colors, Patterns & Gradients
HTML
26
star
42

beautiful_javascript

JavaScript
25
star
43

htmlbook.js

html -> htmlbook parser
JavaScript
25
star
44

enterprise_web_development

This is the example code repository for Enterprise Web Development by Yakov Fain, Victor Rasputnis, Anatole Tartakovsky, and Viktor Gamov (O'Reilly Media)
23
star
45

liber_amicorum

Creative Coding Guidebook
22
star
46

ethics-datascience

Repository for Ethics and Data Science, by Mike Loukides, DJ Patil, and Hilary Mason
20
star
47

lean_enterprise

This is the example code repository for Lean Enterprise by Jez Humble, Joanne Molesky, and Barry O'Reilly (O'Reilly Media)
18
star
48

Learning-Path-Get-Started-with-Natural-Language-Processing-Using-Python-Spark-and-Scala

Links to example code downloads for Learning Path: Get Started with Natural Language Processing Using Python, Spark, and Scala
17
star
49

docbook2htmlbook

XSL Transform to convert Docbook XML to HTMLBook
XSLT
16
star
50

Getting-Started-with-Atlas

A guide to using Atlas, O'Reilly's wiki-like, git-managed authoring platform.
JavaScript
16
star
51

atlas_tech1c_theme

A CSS stylesheet for technical books.
CSS
15
star
52

learning_r

This is the example code repository for Learning R by Richard Cotton (O'Reilly Media)
15
star
53

jsonform

JS library for building JSON objects through a dynamic form
JavaScript
15
star
54

SVG_Text_Layout

Example files for the book SVG Text Layout
HTML
14
star
55

atlas_book_skeleton

Skeleton files for a new Atlas project
HTML
14
star
56

production-resources

Help and Information about O'Reilly Production
14
star
57

knockout_js

This is the example code repository for Knockout.js by Jamie Munro (O'Reilly Media)
CSS
13
star
58

ifpress-solr-plugin

Plugins that extend Solr's capabilities
Java
13
star
59

programmers_guide_to_drupal

Example code repository for Programmer's Guide to Drupal (Second edition) by Jennifer Hodgdon (O'Reilly Media)
PHP
13
star
60

Data_Analytics_with_Hadoop

Python
12
star
61

cypress-playback

Automatically record and playback HTTP requests in Cypress.
JavaScript
12
star
62

jnb-data-sci-handbook

Jupyter Notebook
11
star
63

Learning-Path-Mastering-SpaCy-for-Natural-Language-Processing

11
star
64

orm-awesome

O'Reilly Awesome List
11
star
65

atlas_trade_theme

One of two default themes for O'Reilly Atlas
CSS
10
star
66

biobuilder

9
star
67

dart_up_and_running

This is the example code repository for Dart: Up and Running by Kathy Walrath and Seth Ladd (O'Reilly Media)
9
star
68

Security_for_Web_Developers

9
star
69

cloud-function-template

google cloud function testing template
JavaScript
9
star
70

mapping_the_user_experience

This is the example code repository for Mapping the User Experience by James Kalbach (O'Reilly Media)
8
star
71

atlas_assets

Public repository with assets and design guide for the Atlas platform
SCSS
8
star
72

distributed_denial_of_service_ddos

Python
8
star
73

Spark_The_Definitive_Guide

7
star
74

introduction-to-seaborn

HTML
7
star
75

building_web_applications_with_erlang

This is the example code repository for Building Web Applications with Erlang by Zachary Kessin (O'Reilly Media)
7
star
76

learning_ratpack

6
star
77

audit-python-package

Checks for compliance with current Python packaging best practices
Python
6
star
78

cloud_native_java

6
star
79

ipython-kernel

IPython kernel server for Pyxie projects
Python
6
star
80

an-illustrated-introduction-to-the-t-sne-algorithm

An Illustrated Introduction to the t-SNE Algorithm using Docker
CSS
6
star
81

native-mobile-development

This is the example code repository for Native Mobile Development by Mike Dunn and Shaun Lewis (O'Reilly Media)
Swift
5
star
82

go-tang

Cache Rules Everything Around Me
Go
5
star
83

etudes_for_clojurescript

HTML
5
star
84

Designing_Efficient_BPM_Applications

5
star
85

prototype-imageproxy

Config for deploying imageproxy
Go
5
star
86

gulp-htmlbook

Gulp plugins for handling htmlbook content
HTML
5
star
87

best-of-fluent-verou

Best of Fluent (2012): Lea Verou
5
star
88

earsketch-samples

CSS
5
star
89

hello-alexa

JavaScript
4
star
90

designing-data-intensive-apps

4
star
91

satturn

A file editor with support for markdown and json
CSS
4
star
92

learning_path_go_programming_basics

HTML
4
star
93

atlas-api

Gem to interact with the O'Reilly Atlas API
Ruby
4
star
94

spock_up_and_running

4
star
95

article-template

Single chapter structure for short form writing.
HTML
4
star
96

pyxie-static

Companion repo to jupyter-kernel to show how to insert an ipython notebook code cell into a static site
JavaScript
4
star
97

automating_actionscript_projects_with_eclipse_and_ant

This is the example code repository for Automating ActionScript Projects with Eclipse and Ant by Sidney de Koning (O'Reilly Media)
4
star
98

ncsa-logparse

NCSA Logfile Parser in Haskell
Haskell
4
star
99

binder-trees-forest

Jupyter Notebook
3
star
100

atlas-book-sample

Sample Book Repo for Atlas v2
CSS
3
star