Repository: bokeh/bokeh

Issue:
Deprecate Bokeh.$  and Bokeh._
This is required in preparation to remove JQuery and underscore (or replace which something smaller).


Discussion:

Resolving patch:
diff --git a/bokeh/embed.py b/bokeh/embed.py
--- a/bokeh/embed.py
+++ b/bokeh/embed.py
@@ -30,23 +30,25 @@
 from .util.serialization import make_id
 from .util.deprecation import deprecated
 
-def _prefix(text, prefix):
-    return "\n".join([ prefix + line for line in text.split("\n") ])
-
-def _indent(text):
-    return _prefix(text, "    ")
-
-def _wrap(pre, text, post):
-    return '%s%s%s' % (pre, _indent(text), post)
-
-def _wrap_in_function(code):
-    return _wrap('Bokeh.$(function() {\n', code, '\n});')
+def _indent(text, n=2):
+    return "\n".join([ " "*n + line for line in text.split("\n") ])
 
 def _wrap_in_safely(code):
-    return _wrap('Bokeh.safely(function() {\n', code, '\n});')
+    return """\
+Bokeh.safely(function() {
+%(code)s
+});""" % dict(code=_indent(code, 2))
 
 def _wrap_in_onload(code):
-    return _wrap('document.addEventListener("DOMContentLoaded", function(event) {\n', code, '\n});')
+    return """\
+(function() {
+  var fn = function() {
+%(code)s
+  };
+  if (document.readyState != "loading") fn();
+  else document.addEventListener("DOMContentLoaded", fn);
+})();
+""" % dict(code=_indent(code, 4))
 
 def components(models, resources=None, wrap_script=True, wrap_plot_info=True):
     '''
@@ -247,7 +249,7 @@ def notebook_div(model, notebook_comms_target=None):
     else:
         notebook_comms_target = ''
 
-    script = _wrap_in_function(DOC_JS.render(
+    script = _wrap_in_onload(DOC_JS.render(
         docs_json=serialize_json(docs_json),
         render_items=serialize_json(render_items)
     ))
@@ -446,10 +448,10 @@ def autoload_server(model, app_path="/", session_id=None, url="default"):
     return encode_utf8(tag)
 
 def _script_for_render_items(docs_json, render_items, websocket_url=None, wrap_script=True):
-    plot_js = _wrap_in_function(_wrap_in_safely(DOC_JS.render(
+    plot_js = _wrap_in_onload(_wrap_in_safely(DOC_JS.render(
         websocket_url=websocket_url,
         docs_json=serialize_json(docs_json),
-        render_items=serialize_json(render_items)
+        render_items=serialize_json(render_items),
     )))
 
     if wrap_script:
diff --git a/bokeh/util/compiler.py b/bokeh/util/compiler.py
--- a/bokeh/util/compiler.py
+++ b/bokeh/util/compiler.py
@@ -20,21 +20,20 @@
 _plugin_prelude = \
 """
 (function outer(modules, cache, entry) {
-  if (typeof Bokeh !== "undefined") {
-    var _ = Bokeh._;
-
+  if (Bokeh != null) {
     for (var name in modules) {
       Bokeh.require.modules[name] = modules[name];
     }
 
     for (var i = 0; i < entry.length; i++) {
-        var exports = Bokeh.require(entry[i]);
+      var plugin = Bokeh.require(entry[i]);
+      Bokeh.Models.register_models(plugin.models);
 
-        if (_.isObject(exports.models)) {
-          Bokeh.Models.register_models(exports.models);
+      for (var name in plugin) {
+        if (name !== "models") {
+          Bokeh[name] = plugin[name];
         }
-
-        _.extend(Bokeh, _.omit(exports, "models"));
+      }
     }
   } else {
     throw new Error("Cannot find Bokeh. You have to load it prior to loading plugins.");
diff --git a/bokeh/util/notebook.py b/bokeh/util/notebook.py
--- a/bokeh/util/notebook.py
+++ b/bokeh/util/notebook.py
@@ -34,7 +34,7 @@ def load_notebook(resources=None, verbose=False, hide_banner=False, load_timeout
     publish_display_data({'application/javascript': js})
 
 FINALIZE_JS = """
-Bokeh.$("#%s").text("BokehJS is loading...");
+document.getElementById("%s").textContent = "BokehJS is loading...";
 """
 
 def _load_notebook_html(resources=None, verbose=False, hide_banner=False,


Test patch:
diff --git a/bokeh/tests/test_resources.py b/bokeh/tests/test_resources.py
--- a/bokeh/tests/test_resources.py
+++ b/bokeh/tests/test_resources.py
@@ -8,28 +8,14 @@
 from bokeh.models import Model
 from bokeh.resources import _get_cdn_urls, websocket_url_for_server_url
 
-
 # if BOKEH_RESOURCES is set many tests in this file fail
 if os.environ.get("BOKEH_RESOURCES"):
     raise RuntimeError("Cannot run the unit tests with BOKEH_RESOURCES set")
 
-WRAPPER = """Bokeh.$(function() {
-    foo
-});"""
-
-
-WRAPPER_DEV = '''require(["jquery", "main"], function($, Bokeh) {
-Bokeh.set_log_level("info");
-    Bokeh.$(function() {
-        foo
-    });
-});'''
-
 LOG_LEVELS = ['trace', 'debug', 'info', 'warn', 'error', 'fatal']
 
 DEFAULT_LOG_JS_RAW = 'Bokeh.set_log_level("info");'
 
-
 ## Test JSResources
 
 def test_js_resources_default_mode_is_inline():
diff --git a/tests/integration/interaction/test_tools.py b/tests/integration/interaction/test_tools.py
--- a/tests/integration/interaction/test_tools.py
+++ b/tests/integration/interaction/test_tools.py
@@ -26,7 +26,7 @@ def generate_plot():
             data = cb_obj.data,
             selected_names = '';
 
-        Bokeh.$.each(indices, function(i, index) {
+        indices.forEach(function(index) {
             selected_names += data['name'][index];
         });