Ejemplo n.º 1
0
    def modify_document(self, doc):
        if self.failed:
            return

        module = self._runner.new_module()

        # One reason modules are stored is to prevent the module
        # from being gc'd before the document is. A symptom of a
        # gc'd module is that its globals become None. Additionally
        # stored modules are used to provide correct paths to
        # custom models resolver.
        sys.modules[module.__name__] = module
        doc._modules.append(module)

        old_doc = curdoc()
        set_curdoc(doc)
        old_io = self._monkeypatch_io()

        try:
            def post_check():
                newdoc = curdoc()
                # script is supposed to edit the doc not replace it
                if newdoc is not doc:
                    raise RuntimeError("%s at '%s' replaced the output document" % (self._origin, self._runner.path))
            self._runner.run(module, post_check)
        finally:
            self._unmonkeypatch_io(old_io)
            set_curdoc(old_doc)
Ejemplo n.º 2
0
    def modify_document(self, doc):
        if self.failed:
            return
        from types import ModuleType
        self._module_name = 'bk_script_' + str(uuid.uuid4()).replace('-', '')
        self._module = ModuleType(self._module_name)
        self._module.__dict__['__file__'] = abspath(self._path)
        old_doc = curdoc()
        set_curdoc(doc)
        old_io = self._monkeypatch_io()
        try:
            exec(self._code, self._module.__dict__)
            newdoc = curdoc()
            # script is supposed to edit the doc not replace it
            if newdoc is not doc:
                raise RuntimeError("Script at '%s' replaced the output document" % (self._path))
        except Exception as e:
            self._failed = True
            import traceback
            self._error_detail = traceback.format_exc()

            exc_type, exc_value, exc_traceback = sys.exc_info()
            filename, line_number, func, txt = traceback.extract_tb(exc_traceback)[-1]

            self._error = "%s\nFile \"%s\", line %d, in %s:\n%s" % (str(e), os.path.basename(filename), line_number, func, txt)
        finally:
            self._unmonkeypatch_io(old_io)
            set_curdoc(old_doc)
Ejemplo n.º 3
0
    def modify_document(self, doc):
        if self.failed:
            return

        module = self._runner.new_module()

        # This is to prevent the module from being gc'd before the
        # document is.  A symptom of a gc'd module is that its
        # globals become None.
        if not hasattr(doc, '_ScriptHandler__modules'):
            setattr(doc, '_ScriptHandler__modules', [])
        doc.__modules.append(module)

        old_doc = curdoc()
        set_curdoc(doc)
        old_io = self._monkeypatch_io()

        try:

            def post_check():
                newdoc = curdoc()
                # script is supposed to edit the doc not replace it
                if newdoc is not doc:
                    raise RuntimeError(
                        "Script at '%s' replaced the output document" %
                        (self._runner.path))

            self._runner.run(module, post_check)
        finally:
            self._unmonkeypatch_io(old_io)
            set_curdoc(old_doc)
Ejemplo n.º 4
0
Archivo: code.py Proyecto: 0-T-0/bokeh
    def modify_document(self, doc):
        if self.failed:
            return

        module = self._runner.new_module()

        # This is to prevent the module from being gc'd before the
        # document is.  A symptom of a gc'd module is that its
        # globals become None.
        if not hasattr(doc, '_CodeHandler__modules'):
            setattr(doc, '_CodeHandler__modules', [])
        doc.__modules.append(module)

        old_doc = curdoc()
        set_curdoc(doc)
        old_io = self._monkeypatch_io()

        try:
            def post_check():
                newdoc = curdoc()
                # script is supposed to edit the doc not replace it
                if newdoc is not doc:
                    raise RuntimeError("%s at '%s' replaced the output document" % (self._origin, self._runner.path))
            self._runner.run(module, post_check)
        finally:
            self._unmonkeypatch_io(old_io)
            set_curdoc(old_doc)
Ejemplo n.º 5
0
    def modify_document(self, doc):
        if self.failed:
            return
        from types import ModuleType
        self._module_name = 'bk_script_' + str(uuid.uuid4()).replace('-', '')
        self._module = ModuleType(self._module_name)
        self._module.__dict__['__file__'] = abspath(self._path)
        old_doc = curdoc()
        set_curdoc(doc)
        old_io = self._monkeypatch_io()
        try:
            exec(self._code, self._module.__dict__)
            newdoc = curdoc()
            # script is supposed to edit the doc not replace it
            if newdoc is not doc:
                raise RuntimeError(
                    "Script at '%s' replaced the output document" %
                    (self._path))
        except Exception as e:
            self._failed = True
            import traceback
            self._error_detail = traceback.format_exc()

            exc_type, exc_value, exc_traceback = sys.exc_info()
            filename, line_number, func, txt = traceback.extract_tb(
                exc_traceback)[-1]

            self._error = "%s\nFile \"%s\", line %d, in %s:\n%s" % (
                str(e), os.path.basename(filename), line_number, func, txt)
        finally:
            self._unmonkeypatch_io(old_io)
            set_curdoc(old_doc)
Ejemplo n.º 6
0
    def modify_document(self, doc):
        if self.failed:
            return
        from types import ModuleType
        module_name = 'bk_script_' + str(uuid.uuid4()).replace('-', '')
        module = ModuleType(module_name)
        module.__dict__['__file__'] = abspath(self._path)
        # This is to prevent the module from being gc'd before the
        # document is.  A symptom of a gc'd module is that its
        # globals become None.
        if not hasattr(doc, '_ScriptHandler__modules'):
            setattr(doc, '_ScriptHandler__modules', [])
        doc.__modules.append(module)

        old_doc = curdoc()
        set_curdoc(doc)
        old_io = self._monkeypatch_io()
        try:
            exec(self._code, module.__dict__)
            newdoc = curdoc()
            # script is supposed to edit the doc not replace it
            if newdoc is not doc:
                raise RuntimeError("Script at '%s' replaced the output document" % (self._path))
        except Exception as e:
            self._failed = True
            import traceback
            self._error_detail = traceback.format_exc()

            exc_type, exc_value, exc_traceback = sys.exc_info()
            filename, line_number, func, txt = traceback.extract_tb(exc_traceback)[-1]

            self._error = "%s\nFile \"%s\", line %d, in %s:\n%s" % (str(e), os.path.basename(filename), line_number, func, txt)
        finally:
            self._unmonkeypatch_io(old_io)
            set_curdoc(old_doc)
Ejemplo n.º 7
0
 def _with_self_as_curdoc(self, f):
     from bokeh.io import set_curdoc, curdoc
     old_doc = curdoc()
     try:
         set_curdoc(self)
         f()
     finally:
         set_curdoc(old_doc)
Ejemplo n.º 8
0
 def _with_self_as_curdoc(self, f):
     from bokeh.io import set_curdoc, curdoc
     old_doc = curdoc()
     try:
         set_curdoc(self)
         f()
     finally:
         set_curdoc(old_doc)
Ejemplo n.º 9
0
 def test_scatter(self):
     from bokeh.io import set_curdoc
     from bokeh.plotting import figure
     import numpy as np
     d = document.Document()
     set_curdoc(d)
     assert not d.roots
     assert len(d._all_models) == 0
     p1 = figure(tools=[])
     N = 10
     x = np.linspace(0, 4*np.pi, N)
     y = np.sin(x)
     p1.scatter(x,y, color="#FF00FF", nonselection_fill_color="#FFFF00", nonselection_fill_alpha=1)
     assert len(d.roots) == 1
Ejemplo n.º 10
0
 def test_scatter(self):
     from bokeh.io import set_curdoc
     from bokeh.plotting import figure
     import numpy as np
     d = document.Document()
     set_curdoc(d)
     assert not d.roots
     assert len(d._all_models) == 0
     p1 = figure(tools=[])
     N = 10
     x = np.linspace(0, 4*np.pi, N)
     y = np.sin(x)
     p1.scatter(x,y, color="#FF00FF", nonselection_fill_color="#FFFF00", nonselection_fill_alpha=1)
     assert len(d.roots) == 1
Ejemplo n.º 11
0
    def modify_document(self, doc):
        if self.failed:
            return
        from types import ModuleType
        module_name = 'bk_script_' + str(uuid.uuid4()).replace('-', '')
        module = ModuleType(module_name)
        module.__dict__['__file__'] = abspath(self._path)
        # This is to prevent the module from being gc'd before the
        # document is.  A symptom of a gc'd module is that its
        # globals become None.
        if not hasattr(doc, '_ScriptHandler__modules'):
            setattr(doc, '_ScriptHandler__modules', [])
        doc.__modules.append(module)

        old_doc = curdoc()
        set_curdoc(doc)
        old_io = self._monkeypatch_io()
        try:
            exec(self._code, module.__dict__)
            newdoc = curdoc()
            # script is supposed to edit the doc not replace it
            if newdoc is not doc:
                raise RuntimeError(
                    "Script at '%s' replaced the output document" %
                    (self._path))
        except Exception as e:
            self._failed = True
            import traceback
            self._error_detail = traceback.format_exc()

            exc_type, exc_value, exc_traceback = sys.exc_info()
            filename, line_number, func, txt = traceback.extract_tb(
                exc_traceback)[-1]

            self._error = "%s\nFile \"%s\", line %d, in %s:\n%s" % (
                str(e), os.path.basename(filename), line_number, func, txt)
        finally:
            self._unmonkeypatch_io(old_io)
            set_curdoc(old_doc)
Ejemplo n.º 12
0
 def _with_self_as_curdoc(self, f):
     from bokeh.io import set_curdoc, curdoc
     old_doc = curdoc()
     try:
         if getattr(f, "nolock", False):
             set_curdoc(UnlockedDocumentProxy(self))
         else:
             set_curdoc(self)
         return f()
     finally:
         set_curdoc(old_doc)
Ejemplo n.º 13
0
 def _with_self_as_curdoc(self, f):
     from bokeh.io import set_curdoc, curdoc
     old_doc = curdoc()
     try:
         if getattr(f, "nolock", False):
             set_curdoc(UnlockedDocumentProxy(self))
         else:
             set_curdoc(self)
         return f()
     finally:
         set_curdoc(old_doc)