       get_input_value: (number) ->
         get_input_cell_by_number(notebook, number)?.editor.getValue()
 
+  create_run_context = (output_cell, extra_contexts...) ->
+    $top = output_cell.$el
+
+    scroll_to_top = ->
+      setTimeout ->
+        $('html, body').scrollTop $top.offset().top
+      , 10
+
+    result_handlers =[
+      ignored,
+      handle_cli_cmd,
+      handle_renderable,
+      handle_lead_node,
+      handle_any_object
+    ]
+
+    output = ($target) ->
+      (output) ->
+        $target.removeClass 'clean'
+        $item = $ '<div class="item"/>'
+        if output?
+          $item.append output
+        $target.append $item
+        $item
+
+    run_context =
+      cell: output_cell
+      ops: all_ops
+      current_options: {}
+      output: output output_cell.$el
+      scroll_to_top: scroll_to_top
+      functions: lead.define_functions {}, graphite_function_names
+      vars: lead: {github, graphite, colors}
+
+      render: (o) ->
+        $item = $ '<div class="renderable"/>'
+        @output $item
+
+        nested_context = _.extend {}, run_context,
+          output: output $item
+
+        nested_context.cli = bind_cli nested_context
+        handle_renderable.call nested_context, o
+        # TODO warn if not renderable
+
+      handle_exception: (e, compiled) ->
+        console.error e.stack
+        @cli.error printStackTrace({e}).join('\n')
+        @cli.text 'Compiled JavaScript:'
+        @cli.source 'javascript', compiled
+
+      error: (message) ->
+        $pre = $ '<pre class="error"/>'
+        $pre.text message
+        run_context.output $pre
+
+      display_object: (object) ->
+        for handler in result_handlers
+          return if handler.call run_context, object
+
+      value: (value) -> _lead_cli_value: value
+
       async: (fn) ->
         $item = $ '<div class="async"/>'
         $item.attr 'data-async-status', 'loading'

  run = (input_cell, string) ->
    output_cell = create_output_cell input_cell.notebook

    run_context = create_run_context input_cell, output_cell

    run_in_context run_context, string


          scroll_to_top()

    run_context.cli = cli = bind_cli run_context
    _.defaults run_context, extra_contexts...

    run_context

