Ejemplo n.º 1
0
    def push_notebook(self):
        """ Update date for a plot in the IPthon notebook in place.

        This function can be be used to update data in plot data sources
        in the IPython notebook, without having to use the Bokeh server.

        Returns:
            None

        .. warning::
            The current implementation leaks memory in the IPython notebook,
            due to accumulating JS code. This function typically works well
            with light UI interactions, but should not be used for continuously
            updating data. See :bokeh-issue:`1732` for more details and to
            track progress on potential fixes.

        """
        from IPython.core import display
        from bokeh.protocol import serialize_json
        id = self.ref['id']
        model = self.ref['type']
        json = serialize_json(self.vm_serialize())
        js = """
            var ds = Bokeh.Collections('{model}').get('{id}');
            var data = {json};
            ds.set(data);
        """.format(model=model, id=id, json=json)
        display.display_javascript(js, raw=True)
Ejemplo n.º 2
0
def load_ipython_extension(ip):
    ip.magics_manager.register(CsoundMagics)
    ip.user_ns['runCsd'] = runCsd
    ip.user_ns['runOrcSco'] = runOrcSco
    ip.user_ns['ICsound'] = ICsound
    js = "IPython.CodeCell.config_defaults.highlight_modes['magic_csound'] = {'reg':[/^%%csound/, /^%%csd/, /^%%orc/, /^%%sco/]};"
    display_javascript(js, raw=True)
Ejemplo n.º 3
0
 def reset(self):
     out = None
     if self.cell != "":
         if self.runAsDecl:
             utils.declareCppCode(self.cell)
             self.runAsDecl = False
         elif self.runAsAclic:
             utils.invokeAclic(self.cell)
             self.runAsAclic = False
         elif self.runAsBash:
             cellNoEndNewLine = self.cell[:-1]
             out = utils._checkOutput(cellNoEndNewLine,"Error running shell command")
             if out: sys.stdout.write(out)
             self.runAsBash = False
         else:
             utils.processCppCode(self.cell)
         self.cell = ""
     if self.mustSwitchToPython:
         ip = get_ipython()
         unload_ipython_extension(ip)
         self.mustSwitchToPython = False
         cppcompleter.unload_ipython_extension(ip)
         # Change highlight mode
         display.display_javascript(utils.jsDefaultHighlight.format(mimeType = utils.ipyMIME), raw=True)
         print "Notebook is in Python mode"
Ejemplo n.º 4
0
def load_ipython_extension(ipython):
    ipython.register_magics(ODPSSql)
    js = "IPython.CodeCell.config_defaults.highlight_modes['magic_sql'] = {'reg':[/^%%sql/]};"
    display_javascript(js, raw=True)

    # Do global import when load extension
    ipython.user_ns['DataFrame'] = DataFrame
    ipython.user_ns['Scalar'] = Scalar
    ipython.user_ns['options'] = options
Ejemplo n.º 5
0
    def display(self, speed=1000, from_start=False, reset_io=False, buffer_num=0):
        """
    Display an animation, based on a starting state & the logged diff
    Once this is called, the starting state & log mark are advanced
    """
        self.log_buffer_data_diffs()

        # Create a new html pane with unique id
        chart_id = "%s-%s" % (buffer_num, self._chart_num)
        html = """
    <table>
      <tr><th><i>IO Counts</i></th><th>R</th><th>W</th></tr>
      <tr>
        <td><i>To/from Buffer</i></td>
        <td id="chart-{0}-bufferReads">0</td>
        <td id="chart-{0}-bufferWrites">0</td>
      </tr>
      <tr>
        <td><b>To/from Disk</b></td>
        <td id="chart-{0}-diskReads">0</td>
        <td id="chart-{0}-diskWrites">0</td>
      </tr>
    </table>
    <br />
    <div class="tooltip" id="chart-{0}-tooltip" style="position:absolute; z-index:100; color:white; background:black; opacity:0.7; padding:3px; border-radius:5px; display:none;">TOOLTIP!</div>
    <div class="tooltip" id="chart-{0}-tooltip-2" style="position:absolute; z-index:100; color:white; background:black; opacity:0.7; padding:3px; border-radius:5px; display:none;">TOOLTIP!</div>
    <div id="chart-{0}"></div>
    """.format(
            chart_id
        )

        # Dump log to json file
        with open("pagesLog.json", "wb") as f:
            json.dump(self._log, f)

        # Create animation in js/d3
        js_configs = {
            "DURATION": speed,
            "chartNum": chart_id,
            "numBufferPages": self.buffer_size,
            "pageSize": self.page_size,
            "numDiskPages": 5,
            "logStart": self._diff_log_start if not from_start else 0,
        }
        js = js_file_with_configs("compModel.js", js_configs)
        js_libs = [
            "http://rawgithub.com/mbostock/d3/master/d3.min.js",
            "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js",
            "https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js",
        ]
        display_html(HTML(data=html))
        display_javascript(Javascript(data=js, lib=js_libs))
        self._chart_num += 1

        # Advance current animation state in log
        self.display_set_mark(reset_io=reset_io)
Ejemplo n.º 6
0
def load_ipython_extension(ip):
    """Load the extension in IPython."""
    ip.register_magics(FortranMagics)

    # enable fortran highlight
    patch = ("IPython.config.cell_magic_highlight['magic_fortran'] = "
             "{'reg':[/^%%fortran/]};")
    js = display.Javascript(data=patch,
                            lib=["https://raw.github.com/marijnh/CodeMirror/master/mode/"
                                 "fortran/fortran.js"])
    display.display_javascript(js)
Ejemplo n.º 7
0
def load_ipython_extension(ip):
    """Load the extension in IPython."""
    ip.register_magics(PigMagics)

    # enable Pig highlight
    js = display.Javascript(data=js_string)
    display.display_javascript(js)

    # some custom CSS to augment the syntax highlighting
    css = display.HTML(css_string)
    display.display_html(css)
Ejemplo n.º 8
0
 def push_notebook(self):
     from IPython.core import display
     from bokeh.protocol import serialize_json
     id = self.ref['id']
     model = self.ref['type']
     json = serialize_json(self.vm_serialize())
     js = """
         var ds = Bokeh.Collections('{model}').get('{id}');
         var data = {json};
         ds.set(data);
     """.format(model=model, id=id, json=json)
     display.display_javascript(js, raw=True)
Ejemplo n.º 9
0
  def display(self, chart_num=0, configs_in={}):
    """Display the TXN viewer based on full current log"""

    # dump input txns to jsonfor transfer to js
    with open('txnLog.json', 'wb') as f:
      json.dump(self._log, f)

    # merge default configs
    config = {
      'chartNum': chart_num,
      'numThreads': self.n_threads
    }
    config.update(configs_in)
    js = ''.join('var %s = %s\n' % (k,v) for k,v in config.iteritems())

    # JS
    js += open('txnViewer.js', 'rb').read()
    js_libs = [
      'http://rawgithub.com/mbostock/d3/master/d3.min.js', 
      'https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js',
      'https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js'
    ]

    # HTML
    html_scripts = [
      '<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">'
    ]
    html="""
    <head>{0}</head>
    <h3>TXN VIEWER</h3>
    <table style="border: none; border-collapse: collapse;">
      <tr style="border: none;">
        <td style="border: none;">
          <div id="top-spacer-{1}"></div>
          <div id="chart-{1}"></div>
          <div id="slider-{1}"></div>
        </td>
        <td style="vertical-align:top; border: none;">
          <table id="vals-{1}"></table>
        </td>
      </tr>
      <tr style="border: none;">
        <td colspan="2" style="border: none;"><h4>The Log</h4></td>
      </tr>
      <tr><td colspan="2"><div id="log-{1}"></td></tr>
    </table>
    """.format(''.join(html_scripts), chart_num)

    # Display in IPython notebook
    display_html(HTML(data=html))
    display_javascript(Javascript(data=js, lib=js_libs))
Ejemplo n.º 10
0
 def __init__(self, *args, **kwargs):
     super(NowSQL, self).__init__(*args, **kwargs)
     javascript = """
         var mode = 'magic_text/x-sql';
         if (!Jupyter.CodeCell.options_default.highlight_modes[mode]) {
             Jupyter.CodeCell.options_default.highlight_modes[mode] = {
                 'reg':[]
             };
         }
         Jupyter.CodeCell.options_default.highlight_modes[mode].reg.push(
             /^%%now_sql/
         );
     """
     display_javascript(javascript, raw=True)
Ejemplo n.º 11
0
 def __init__(self, *args, **kwargs):
     super(NowSQL, self).__init__(*args, **kwargs)
     js = """
         var mode = 'magic_text/x-sql';
         if (!IPython.CodeCell.config_defaults.highlight_modes[mode]) {
             IPython.CodeCell.config_defaults.highlight_modes[mode] = {
                 'reg':[]
             };
         }
         IPython.CodeCell.config_defaults.highlight_modes[mode].reg.push(
             /^%%now_sql/
         );
     """;
     display_javascript(js, raw=True)
Ejemplo n.º 12
0
    def render_tree(self):
        """
        Renders d3 visualization of the d3 tree, for IPython notebook display
        Depends on html/js files in vis/ directory, which is assumed to be in same dir...
        :return:
        """
        # TODO: Make better control over what format / what attributes displayed @ nodes!
        html = open('lib/vis/tree-chart.html').read() % self.id
        display_html(HTML(data=html))

        # JS
        JS_LIBS = ["https://d3js.org/d3.v3.min.js"]
        js = open('lib/vis/tree-chart.js').read() % (json.dumps(self.json), self.id)
        display_javascript(Javascript(data=js, lib=JS_LIBS))
Ejemplo n.º 13
0
def add_syntax_coloring():
    """Adds syntax coloring to cell magic for SELECT, FROM, etc."""
    js_sql_syntax = '''
    require(['notebook/js/codecell'], function(codecell) {
      // https://github.com/jupyter/notebook/issues/2453
      codecell.CodeCell.options_default.highlight_modes['magic_text/x-sql'] = {'reg':[/^%read_sql/, /.*=\s*%read_sql/,
                                                                                      /^%%read_sql/]};
      Jupyter.notebook.events.one('kernel_ready.Kernel', function(){
          console.log('BBBBB');
          Jupyter.notebook.get_cells().map(function(cell){
              if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;
      });
    });
    '''
    display_javascript(js_sql_syntax, raw=True)
Ejemplo n.º 14
0
  def render_tree(self, highlight=[]):
    """
    Renders d3 visualization of the d3 tree, for IPython notebook display
    Depends on html/js files in vis/ directory, which is assumed to be in same dir...
    """
    # HTML
    WORD = '<span class="word-' + self.id + '-%s">%s</span>'
    words = ' '.join(WORD % (i,w) for i,w in enumerate(corenlp_cleaner(self.words))) if self.words else ''
    html = open('%s/vis/tree-chart.html' % APP_HOME).read() % (self.id, self.id, words) 
    display_html(HTML(data=html))

    # JS
    JS_LIBS = ["http://d3js.org/d3.v3.min.js"]
    js = open('%s/vis/tree-chart.js' % APP_HOME).read() % (self.id, json.dumps(self.to_json()), str(highlight))
    display_javascript(Javascript(data=js, lib=JS_LIBS))
Ejemplo n.º 15
0
 def reset(self):
     if self.cell != "":
         if self.mustDeclare:
             utils.declareCppCode(self.cell)
             self.mustDeclare = False
         else:
             utils.processCppCode(self.cell)
         self.cell = ""
     if self.mustSwitchToPython:
         ip = get_ipython()
         unload_ipython_extension(ip)
         self.mustSwitchToPython = False
         cppcompleter.unload_ipython_extension(ip)
         # Change highlight mode
         display.display_javascript(utils.jsDefaultHighlight.format(mimeType = utils.ipyMIME), raw=True)
         print "Notebook is in Python mode"
 def _display_dataframe(self, data, update=None):
     table_id = 'table_{}'.format(
         update._df_display_id if update else self._df_display_id)
     columns = [{
         'title': ''
     }] + [{
         'title': str(column)
     } for column in data.columns]
     format_window_info_in_dataframe(data)
     # Convert the dataframe into rows, each row looks like
     # [column_1_val, column_2_val, ...].
     rows = data.applymap(lambda x: str(x)).to_dict('split')['data']
     # Convert each row into dict where keys are column index in the datatable
     # to be rendered and values are data from the dataframe. Column index 0 is
     # left out to hold the int index (not part of the data) from dataframe.
     # Each row becomes: {1: column_1_val, 2: column_2_val, ...}.
     rows = [{k + 1: v for k, v in enumerate(row)} for row in rows]
     # Add the dataframe int index (used as default ordering column) to datatable
     # column index 0 (will be rendered as the first column).
     # Each row becomes:
     # {1: column_1_val, 2: column_2_val, ..., 0: int_index_in_dataframe}.
     for k, row in enumerate(rows):
         row[0] = k
     script = _DATAFRAME_SCRIPT_TEMPLATE.format(table_id=table_id,
                                                columns=columns,
                                                data_as_rows=rows)
     script_in_jquery_with_datatable = ie._JQUERY_WITH_DATATABLE_TEMPLATE.format(
         customized_script=script)
     # Dynamically load data into the existing datatable if not empty.
     if update and not update._is_datatable_empty:
         display_javascript(Javascript(script_in_jquery_with_datatable))
     else:
         html = _DATAFRAME_PAGINATION_TEMPLATE.format(
             table_id=table_id,
             script_in_jquery_with_datatable=script_in_jquery_with_datatable
         )
         if update:
             if not data.empty:
                 # Re-initialize a datatable to replace the existing empty datatable.
                 update_display(HTML(html),
                                display_id=update._df_display_id)
                 update._is_datatable_empty = False
         else:
             # Initialize a datatable for the first time rendering.
             display(HTML(html), display_id=self._df_display_id)
             if not data.empty:
                 self._is_datatable_empty = False
Ejemplo n.º 17
0
 def __exit__(self, exc_type, exc_value, traceback):
     try:
         from IPython.core.display import Javascript
         from IPython.core.display import display
         from IPython.core.display import display_javascript
         from apache_beam.runners.interactive import interactive_environment as ie
         if ie.current_env().is_in_notebook:
             script = self.spinner_removal_template.format(id=self._id)
             display_javascript(
                 Javascript(
                     ie._JQUERY_WITH_DATATABLE_TEMPLATE.format(
                         customized_script=script)))
         else:
             display(self._exit_text)
     except ImportError as e:
         _LOGGER.error('Please use interactive Beam features in an IPython'
                       'or notebook environment: %s' % e)
Ejemplo n.º 18
0
  def _display_overview(self, data, update=None):
    if (not data.empty and self._include_window_info and
        all(column in data.columns
            for column in ('event_time', 'windows', 'pane_info'))):
      data = data.drop(['event_time', 'windows', 'pane_info'], axis=1)

    gfsg = GenericFeatureStatisticsGenerator()
    proto = gfsg.ProtoFromDataFrames([{'name': 'data', 'table': data}])
    protostr = base64.b64encode(proto.SerializeToString()).decode('utf-8')
    if update:
      script = _OVERVIEW_SCRIPT_TEMPLATE.format(
          display_id=update._overview_display_id, protostr=protostr)
      display_javascript(Javascript(script))
    else:
      html = _OVERVIEW_HTML_TEMPLATE.format(
          display_id=self._overview_display_id, protostr=protostr)
      display(HTML(html))
Ejemplo n.º 19
0
    def render_tree(self, highlight=[]):
        """
        Renders d3 visualization of the d3 tree, for IPython notebook display
        Depends on html/js files in vis/ directory, which is assumed to be in same dir...
        """
        # HTML
        WORD = '<span class="word-' + self.id + '-%s">%s</span>'
        words = ' '.join(WORD % (i, w) for i, w in enumerate(
            corenlp_cleaner(self.words))) if self.words else ''
        html = open('%s/vis/tree-chart.html' %
                    APP_HOME).read() % (self.id, self.id, words)
        display_html(HTML(data=html))

        # JS
        JS_LIBS = ["https://d3js.org/d3.v3.min.js"]
        js = open('%s/vis/tree-chart.js' % APP_HOME).read() % (
            self.id, json.dumps(self.to_json()), str(highlight))
        display_javascript(Javascript(data=js, lib=JS_LIBS))
Ejemplo n.º 20
0
 def reset(self):
     if self.cell != "":
         if self.mustDeclare:
             utils.declareCppCode(self.cell)
             self.mustDeclare = False
         else:
             cell = self.cell
             code = commentRemover(self.cell)
             utils.processCppCode(code)
         self.cell = ""
     if self.mustSwitchToPython:
         unload_ipython_extension(get_ipython())
         self.mustSwitchToPython = False
         cppcompleter.unload_ipython_extension(get_ipython())
         # Change highlight mode
         display.display_javascript(
             utils.jsDefaultHighlight.format(mimeType=utils.ipyMIME),
             raw=True)
         print "Notebook is in Python mode"
Ejemplo n.º 21
0
def coffeescript_recipe(input_filename, input_dir="", output_dir=""):
    input_path_full = os.path.join(input_dir, input_filename)
    file_stem = input_filename.split('.coffee')[0]
    output_file_name = os.path.join(output_dir, file_stem + '.js')

    if not os.path.exists(output_file_name) or\
        os.path.getmtime(input_path_full)>os.path.getmtime(output_file_name):
        # Compile coffeescript to javascript + source map
        subprocess.check_output(['coffee', '-c', '-m','-o', output_dir,
                                 input_path_full],
                                 stderr=subprocess.STDOUT)
    
    output_mapping_file_name = os.path.join(output_dir, file_stem + '.js.map')
    
    # Coffeescript can only output source maps as files and they are bound by their file names
    # So for inlined javascript it is necessary to wrangle the two output files
    js_code = file(output_file_name).read()
    # Remove sourceMappingUrl directive which contains a useless file name
    
    js_code = "\n".join([line for line in js_code.split("\n")
                     if not line.startswith('//# sourceMappingURL=')])

    
    # Load coffeescript source code for inclusion into source map
    coffee_code = file(os.path.join(input_dir, input_filename)).read()
    
    # load source map as a dict structure to amend some particulars
    mapping = json.loads(file(output_mapping_file_name).read())
    
    # In Chrome Dev Tools newly inlined source maps don't replace old source maps for the same file name
    unique_token = os.urandom(4).encode('hex') # Create a relatively unique token
    
    # Change source map to work nicely when inlined
    mapping['sourceRoot'] = ''
    mapping['sourcesContent'] = [coffee_code]
    mapping['sources'] = [input_filename + unique_token]

    mapping['file'] = input_filename + unique_token
    
    source_map = json.dumps(mapping)
    js_code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,"+source_map.encode("base64").replace('\n', '')+'\n'
    display.display_javascript(js_code, raw=True)
Ejemplo n.º 22
0
def setup_notebook():
    # assign text/x-c++src MIME type to pybind11 cells
    code = """
    require(['notebook/js/codecell'], function(cc) {
        cc.CodeCell.options_default.highlight_modes['magic_text/x-c++src'] =
            {reg: [/^\s*%%pybind11/]};
    });
    """
    display_javascript(Javascript(data=code))

    # assign non-black colour to C/C++ keywords
    html = """
    <style>
    .cm-s-ipython span.cm-variable-3 {
        color: #208ffb;
        font-weight: bold;
    }
    </style>
    """
    display_html(HTML(data=html))
Ejemplo n.º 23
0
    def import_html_to_head(self, html_hrefs):
        """Imports given external HTMLs (supported through webcomponents) into
    the head of the document.

    On load of webcomponentsjs, import given HTMLs. If HTML import is already
    supported, skip loading webcomponentsjs.

    No matter how many times an HTML import occurs in the document, only the
    first occurrence really embeds the external HTML. In a notebook environment,
    the body of the document is always changing due to cell [re-]execution,
    deletion and re-ordering. Thus, HTML imports shouldn't be put in the body
    especially the output areas of notebook cells.
    """
        try:
            from IPython.core.display import Javascript
            from IPython.core.display import display_javascript
            display_javascript(
                Javascript(_HTML_IMPORT_TEMPLATE.format(hrefs=html_hrefs)))
        except ImportError:
            pass  # NOOP if dependencies are not available.
Ejemplo n.º 24
0
  def load_jquery_with_datatable(self):
    """Loads common resources to enable jquery with datatable configured for
    notebook frontends if necessary. If the resources have been loaded, NOOP.

    A window.interactive_beam_jquery with datatable plugin configured can be
    used in following notebook cells once this is invoked.

    #. There should only be one jQuery imported.
    #. Datatable needs to be imported after jQuery is loaded.
    #. Imported jQuery is attached to window named as jquery[version].
    #. The window attachment needs to happen at the end of import chain until
       all jQuery plugins are set.
    """
    try:
      from IPython.core.display import Javascript
      from IPython.core.display import display_javascript
      display_javascript(
          Javascript(
              _JQUERY_WITH_DATATABLE_TEMPLATE.format(customized_script='')))
    except ImportError:
      pass  # NOOP if dependencies are not available.
Ejemplo n.º 25
0
def _register_syntax_highlighting():  # pragma: no cover
    from IPython.core import display

    types = map(str, _SQL_TO_PYTHON_FRAMES.keys())
    functions = list(RexCallPlugin.OPERATION_MAPPING.keys())

    # Create a new mimetype
    mime_type = {
        "name": "sql",
        "keywords": _create_set(KEYWORDS + functions),
        "builtin": _create_set(types),
        "atoms": _create_set(["false", "true", "null"]),
        # "operatorChars": /^[*\/+\-%<>!=~&|^]/,
        "dateSQL": _create_set(["time"]),
        # More information
        # https://opensource.apple.com/source/WebInspectorUI/WebInspectorUI-7600.8.3/UserInterface/External/CodeMirror/sql.js.auto.html
        "support": _create_set(["ODBCdotTable", "doubleQuote", "zerolessFloat"]),
    }

    # Code original from fugue-sql, adjusted for dask-sql and using some more customizations
    js = (
        r"""
    require(["codemirror/lib/codemirror"]);

    // We define a new mime type for syntax highlighting
    CodeMirror.defineMIME("text/x-dasksql", """
        + json.dumps(mime_type)
        + r"""
    );
    CodeMirror.modeInfo.push({
        name: "Dask SQL",
        mime: "text/x-dasksql",
        mode: "sql"
    });
    """
    )

    display.display_javascript(js + _JS_ENABLE_DASK_SQL, raw=True)
Ejemplo n.º 26
0
def run_js(js):
    display_javascript(js, raw=True)
Ejemplo n.º 27
0
def load_ipython_extension(ipython):
    ipython.register_magics(ODPSSql)
    js = "IPython.CodeCell.config_defaults.highlight_modes['magic_sql'] = {'reg':[/^%%sql/]};"
    display_javascript(js, raw=True)
Ejemplo n.º 28
0
 def __init__(self, *args, **kwargs):
     super(CypherMagic, self).__init__(*args, **kwargs)
     display_javascript(js, raw=True)
Ejemplo n.º 29
0
 def __init__(self, *args, **kwargs):
     super(CypherMagic, self).__init__(*args, **kwargs)
     display_javascript(js, raw=True)
Ejemplo n.º 30
0
def load_ipython_extension(ip):
    js = "IPython.CodeCell.config_defaults.highlight_modes['magic_text/x-sql'] = {'reg':[/^%%sparksql/]};"
    display_javascript(js, raw=True)
    ip.register_magics(SparkSqlMagic)
Ejemplo n.º 31
0
                    pass
            if len(fail_list) > 0:
                print("The following macros could not be found: %s" % ', '.join(fail_list))
            if len(macro_dict.keys()) > 0:
                self.shell.push({'macro_dict' : macro_dict})
                print("Several (%dx) macros have been added to the dictionary: macro_dict" % count_success),

        with open(self._pid_file, 'w') as pid_text:
            pid_text.write(','.join(map(str, self.pid_list)))

        if not args.noprint:
            if args.graph:
                if not len(out) < 5:
                    print(out)
                if re.search('could not find Graph window', out, flags=re.I) is None:
                    return Image(graph_out, retina=True)
                else:
                    print('\nNo graph displayed, could not find one generated in this cell.')
            else:
                return print(out)
        else:
            return

# Register the magic function:
ip = get_ipython()
ip.register_magics(iPyStataMagic)

# Enable the stata syntax highlighting:
js = "IPython.CodeCell.config_defaults.highlight_modes['magic_stata'] = {'reg':[/^%%stata/]};"
display.display_javascript(js, raw=True)
Ejemplo n.º 32
0
def load_ipython_extension(ipython):
    ipython.register_magics(ODPSSql)
    js = "IPython.CodeCell.config_defaults.highlight_modes['magic_sql'] = {'reg':[/^%%sql/]};"
    display_javascript(js, raw=True)
Ejemplo n.º 33
0
def javascript_recipe(input_filename, input_dir="", output_dir=""):
    # Output dir is ignored because files are copied as-is
    input_path_full = os.path.join(input_dir, input_filename)
    js_code = file(input_path_full).read()
    display.display_javascript(js_code, raw=True)
Ejemplo n.º 34
0
def import_pymb_magic():
    # so many imports
    from IPython.core.magic import (magics_class, Magics, cell_magic,
                                    line_magic, MagicsManager)
    from IPython.core.magic_arguments import (argument, kwds, defaults,
                                              magic_arguments, parse_argstring)
    from IPython.core.display import display_javascript
    import PyMB

    # turn on C++ highlighting for PyMB cells
    display_javascript("IPython.config.cell_magic_highlight['magic_clike'] =" +
                       "{'reg':[/^%%PyMB/]};",
                       raw=True)

    # define PyMB magic class
    @magics_class
    class PyMBMagics(Magics):
        """A PyMB magic for writing a model in .
        """
        def __init__(self, shell):
            """
            Parameters
            ----------

            shell : IPython shell

            """
            super(PyMBMagics, self).__init__(shell)

        @magic_arguments()
        @argument('name',
                  type=str,
                  help='The model name. A new model object will' +
                  ' be created using this name.')
        @argument('-NO_WRAP',
                  '--NO_WRAP_OBJ_FUN',
                  dest='WRAP',
                  action='store_false',
                  help='Turn off automatic wrapping of' +
                  ' cell in objective function.')
        @argument('-WRAP',
                  '--WRAP_OBJ_FUN',
                  dest='WRAP',
                  action='store_true',
                  help='Wrap the contents of the cell' +
                  ' in an objective function.')
        @defaults(WRAP=True)
        @argument('-TMB',
                  '--TMB_DIR',
                  type=str,
                  default='/usr/local/lib/R/site-library/TMB/include',
                  help='''TMB_DIR : str, default ''' +
                  ''' '/usr/local/lib/R/site-library/TMB/include'
                        location of TMB library''')
        @argument('-R',
                  '--R_DIR',
                  type=str,
                  default='/usr/share/R/include',
                  help='''R_DIR : str, default '/usr/share/R/include'
                        location of R shared library
                        Note: R must be built with shared libraries
                        See http://stackoverflow.com/a/13224980/1028347''')
        @argument('-LR',
                  '--R_LIB',
                  type=str,
                  default='/usr/share/R/include',
                  help='''R_LIB : str, default '/usr/lib/R/lib'
                        location of R library
                        Note: R must be built with shared libraries
                        See http://stackoverflow.com/a/13224980/1028347''')
        @argument('-CC',
                  '--CCOMPILER',
                  type=str,
                  default='g++',
                  help='''CCOMPILER : str, default 'g++'
                        C++ compiler to use''')
        @argument('-OUT',
                  '--OUTPUT_DIR',
                  type=str,
                  default='tmb_tmp',
                  help='''OUTPUT_DIR : str, default 'tmb_tmp'
                        output directory for .cpp and .o files''')
        @argument('-I',
                  '--INCLUDE',
                  type=list,
                  default=['TMB.hpp'],
                  help='''INCLUDE : list, default ['TMB.hpp']
                        other libraries to include''')
        @argument('-U',
                  '--USING',
                  type=list,
                  default=['namespace density'],
                  help='''USING: list, default ['namespace density']
                        namespaces, libraries, etc to' +
                        '' include, e.g. Eigen::SparseMatrix''')
        @argument('-V',
                  '--VERBOSE',
                  dest='VERBOSE',
                  action='store_true',
                  help='''VERBOSE: boolean, default False
                          prints model code and compiler warnings''')
        @argument('-Q',
                  '--QUIET',
                  dest='VERBOSE',
                  action='store_false',
                  help='''QUIET: boolean, default True
                          silences compiler output (opposite of verbose)''')
        @defaults(VERBOSE=False)
        @cell_magic
        def PyMB(self, line, cell):
            # parse arguments
            args = parse_argstring(self.PyMB, line)
            name = args.name
            # opts = args.opts

            # make a new model
            model = PyMB.model(name=name)
            self.shell.push({name: model})
            print('Created model {}.'.format(name))

            # create code string
            code = ''

            # add includes
            for i in args.INCLUDE:
                code += '#include <{}>\n'.format(i)

            # add using
            for u in args.USING:
                code += 'using {};\n'.format(u)

            # wrap cell in objective function by default
            if args.WRAP:
                code += 'template<class Type>\n'
                code += 'Type objective_function<Type>::operator() () {\n'

            # add cell contents (with tab indenting to make it look nicer)
            code += '\n'.join(['    ' + c for c in cell.split('\n')]) + '\n'

            # close objective function
            if args.WRAP:
                code += '}\n'

            # compile model
            model.compile(codestr=code,
                          output_dir=args.OUTPUT_DIR,
                          cc=args.CCOMPILER,
                          R=args.R_DIR,
                          TMB=args.TMB_DIR,
                          LR=args.R_LIB,
                          verbose=args.VERBOSE)

            # print if verbose
            if args.VERBOSE:
                print('Code:\n\n{}'.format(code))

    ip = get_ipython()
    ip.register_magics(PyMBMagics)
Ejemplo n.º 35
0
                    self.shell.push({args.output: output_ipys})
            except:
                print(
                    "Exception has occured. File could not be loaded. (Note, Pandas needs to be 0.17.x or higher.)"
                )

        if not args.noprint:
            if args.graph:
                if not len(out) < 5:
                    print(out)
                if re.search('could not find Graph window', out,
                             flags=re.I) is None:
                    return IFrame('./temp_graph.pdf', width=700, height=400)
                else:
                    print(
                        '\nNo graph displayed, could not find one generated in this cell.'
                    )
            else:
                return print(out)
        else:
            return


# Register the magic function:
ip = get_ipython()
ip.register_magics(iPyStataMagic)

# Enable the stata syntax highlighting:
js = "IPython.CodeCell.config_defaults.highlight_modes['magic_stata'] = {'reg':[/^%%stata/]};"
display.display_javascript(js, raw=True)
Ejemplo n.º 36
0
 def _ipython_display_(self):
     htmlcontent = '<div id="{arg1}" style="height: ' + str(self.height) + 'px; width:95%;"></div>'
     display_html(htmlcontent.format(arg1=self.uuid),
         raw=True
     )
     display_javascript(Javascript(data=(self.codestring % (self.uuid)), css=self.cssfile))
Ejemplo n.º 37
0
def load_ipython_extension(ip):
    js = "IPython.CodeCell.config_defaults.highlight_modes['magic_text/x-sql'] = {'reg':[/^%%sparksql/]};"
    display_javascript(js, raw=True)
    ip.register_magics(SparkSqlMagic)
Ejemplo n.º 38
0
def load_ipython_extension(ip):
    display_javascript("""console.log("Xcalar SQL magic loaded")""", raw=True)
    """Load the extension in IPython."""
    utils.add_syntax_coloring()
    
    ip.register_magics(SQL)