Esempio n. 1
0
    def templatefile(self, line):
        """creates a file from template stored as a file
        """
        args = parse_argstring(self.templatefile, line)
        if not os.path.isfile(args.template):
            raise FileNotFoundError(args.template)

        with open(args.template) as f:
            fread = f.read()

        output = args.output
        p = params()
        if args.format == 'mustache':
            result = mustache(fread, p, output)
        elif args.foramt == 'fstring':
            result = fstring(fread, p, output)
        else:
            raise ValueError(
                f'Unsupported format: {args.format}. Must be: mustache|fstring'
            )

        if output and args.verbose:
            return display(FileLink(output), Code(result))
        elif output:
            return display(FileLink(output))
        else:
            return display(Code(result))
Esempio n. 2
0
    def ask(_):
        """Handle question requests"""
        output.clear_output(wait=True)
        with output:
            display(Code("Asking...", language="text"))
        context = data[docslider.value]["paragraphs"][
            paraslider.value]["context"]

        try:
            results_raw = answer_fetcher(context, question.value)
        except Exception as err:
            output.clear_output(wait=True)
            output.append_stderr("Failed to call answer fetcher\n")
            output.append_stderr(traceback.format_exc())
            return

        output.clear_output(wait=True)
        if not is_sequence(results_raw):
            # TODO: Proper way of displaying errors in callbacks
            output.append_stderr(
                "ValueError: answer_fetcher fn must return a tuple startix, endix; and optionally also a "
                f"raw response. Got {results_raw}\n")
        if is_sequence(results_raw[0]):
            # First result is a (startix, endix) tuple, second result is the raw output
            ixstart, ixend = results_raw[0]
            rawres = results_raw[1] if len(results_raw) > 1 else None
        else:
            # Just a tuple (startix, endix)... May be extended with the raw output as third element
            ixstart = results_raw[0]
            ixend = results_raw[1]
            rawres = results_raw[2] if len(results_raw) > 2 else None

        if ixstart < 0:
            output.append_stderr(
                f"Warn: Negative ixstart {ixstart} will be overridden to 0\n")
            ixstart = 0
        if ixend >= len(context):
            output.append_stderr(
                f"Warn: ixend {ixend} greater than context length {len(context)} and will be overridden.\n"
            )

        if rawres is not None:
            with output:
                display(Code(f"Raw result:\n{rawres}\n", language="text"))
            if isinstance(rawres, dict):
                confidence = rawres.get("confidence", rawres.get("score"))
            else:
                confidence = None
        else:
            confidence = None
        prestart = context[:ixstart]
        answer = context[ixstart:ixend]
        postend = context[ixend:]
        paracontent.clear_output(wait=True)
        with paracontent:
            display(
                HTML("".join((
                    "<p>", prestart,
                    f'<span style="background: {get_highlight_color(confidence=confidence)};">',
                    answer, "</span>", postend, "</p>"))))
Esempio n. 3
0
    async def do_magic(
        self, code: str, silent: bool, store_history: bool = True, user_expressions=None, allow_stdin: bool = False
    ):
        # Handle some custom line magics.
        if code == r"%python":
            with open(self.pyfile, "r") as f:
                pycode = f.read()
                if black:
                    pycode = black.format_str(pycode, mode=black.FileMode())
                code_ = Code(pycode.strip(), language="python")
                self.Code(code_)
                return self.ok()
        elif code == r"%fsharp":
            with open(self.fsfile, "r") as f:
                fscode = f.read()
                code_ = Code(fscode.strip(), language="fsharp")
                self.Code(code_)
                return self.ok()

        # Reset command
        elif code.startswith(r"%reset"):
            self.restart_kernel()
            return await super().do_execute(code, silent, store_history, user_expressions, allow_stdin)

        # Send all cell magics straight to the IPythonKernel
        elif code.startswith(r"%%"):
            # Make sure Python runs in the same context as us
            code = code.replace(r"%%python", "")
            return await super().do_execute(code, silent, store_history, user_expressions, allow_stdin)

        return
Esempio n. 4
0
    def tikz_to_plot(
        cls,
        code,
        x='1em',
        y='1em',
        scale='1',
        svg_scale=2,
        raw=False,
        **kwargs,
    ):
        code = cls.tikz_to_tex(code, x=x, y=y, scale=scale, raw=raw, **kwargs)
        try:
            with tempfile.TemporaryDirectory() as tmpdir:
                tmpdir = Path(tmpdir)

                main_tex = (tmpdir / 'main.tex')
                main_tex.write_text(code)

                run_process(['pdflatex', 'main.tex'], cwd=tmpdir)

                run_process([
                    'inkscape',
                    '--without-gui',
                    '--file=main.pdf',
                    '--export-plain-svg=main.svg',
                ],
                            cwd=tmpdir)
                res_svg = Path(tmpdir / 'main.svg').read_text()
            return SVG(resize_svg_code(res_svg, factor=svg_scale,
                                       method='bs4'))
        except Exception as e:
            display(Code(code, language='tex'))
            raise
Esempio n. 5
0
def get_code(reference, hide_doc_string=False):
    """
    Return the source code of the given reference path to a function.

    Parameters
    ----------
    reference : str
        Dot path of desired function.
    hide_doc_string : bool
        Option to hide the docstring.

    Returns
    -------
    IPython.display.Code
        Source code of the given class or function.
    """
    obj = inspect.getsource(_get_object_from_reference(reference))

    if hide_doc_string:
        obj = obj.split('"""')
        del obj[1]
        obj = ''.join(obj)

    if ipy:
        return Code(obj, language='python')
    else:
        simple_warning(
            "IPython is not installed. Run `pip install openmdao[notebooks]` or "
            "`pip install openmdao[docs]` to upgrade.")
Esempio n. 6
0
    def start(self, methods):
        self._methods = methods
        draw = self._methods.get("draw", None)

        if draw:
            self.print_status("Running...")
            display(self.stop_button)

        display(self.canvas)

        self.output_text_code = display(Code(self.output_text), display_id=True)

        self.canvas.on_mouse_down(self.on_mouse_down)
        self.canvas.on_mouse_up(self.on_mouse_up)
        self.canvas.on_mouse_move(self.on_mouse_move)

        self.kb_mon.on_dom_event(self.handle_kb_event)

        # Initialize text drawing settings for the canvas. ()
        self.canvas.font = f"{self.font_settings['size']}px {self.font_settings['font']}"
        self.canvas.text_baseline = 'top'
        self.canvas.text_align = 'left'

        thread = threading.Thread(target=self.loop)
        thread.start()
Esempio n. 7
0
    def get_embedding_code(self, widgets):
        params = self.get_current_params(widgets)
        params['n_iter'] = int(widgets['_iteration'].value)
        tsne = TSNE(**params, min_grad_norm=0)

        # Since TSNE is a subclass of sklearn's BaseEstimator, repr(tsne)
        # provides the code to reproduce the resulting embedding. We remove
        # whitespace and linebreaks so we can later break the text as we like.
        expression = repr(tsne)
        expression = re.sub('\n', '', expression)
        expression = re.sub(' +', ' ', expression)

        prefix = 'tsne = '
        assignment_line = prefix + expression

        chars_until_params = len(prefix) + len('TSNE(')
        tw = TextWrapper(subsequent_indent=' ' * chars_until_params, width=80)
        assignment_line = tw.fill(assignment_line)

        code = ('# pip install openTSNE\n'
                'from openTSNE import TSNE\n'
                f'{assignment_line}\n'
                'tsne.fit(X)')

        # return as IPython.display.Code
        # -> repr will print the code in fixed width font
        # -> str will print the actual string containing \n
        return Code(code)
Esempio n. 8
0
 def on_button_clicked(b):
     with output:
         if button.count < len(code_blocks):
             display(Code(data=code_blocks[button.count],
                          language='python'))
             button.count += 1
         else:
             button.disabled = True
Esempio n. 9
0
 def template(self, line, cell=None):
     """create a template from cell content
     """
     args = parse_argstring(self.template, line)
     filename = args.filename
     template = cell.strip()
     result = mustache(template, params(), filename)
     if args.verbose:
         return display(FileLink(filename), Code(result))
     else:
         return display(FileLink(filename))
Esempio n. 10
0
 def load_nbvars(self, line):
     """Loads envvars into current notebook
     """
     args = parse_argstring(self.load_nbvars, line)
     namespace = args.namespace or current_namespace()
     name = args.configmap or os.environ.get('NB_VARS', 'jupyter-vars')
     user_ns = get_ipython().user_ns
     display(Markdown(f"Loading notebook variables from configmap: `{namespace}/{name}`"))
     try:
         load_nbvars(name, namespace, )
     except ApiException as e:
         display(Code(f"Error reading configmap {namespace}/{name}: {e.status}, {e.reason}"))
         return e
Esempio n. 11
0
    def json(self) -> Union[str, Code]:
        """
        Outputs the uncompiled procedures to JSON.

        Returns:
        - JSON of the protocol.
          When in Jupyter, this string is wrapped in a `IPython.display.Code` object for nice syntax highlighting.
        """
        compiled_json = json.dumps(self.to_list(), sort_keys=True, indent=4)

        if get_ipython():
            return Code(compiled_json, language="json")
        return compiled_json
Esempio n. 12
0
def format_source(obj):
    source = getsource(obj)

    if is_jupyter():
        from IPython.display import display, Code
        return Code(source, language='python')
    else:
        try:
            from pygments import highlight
            from pygments.lexers import PythonLexer
            from pygments.formatters import Terminal256Formatter
            return highlight(source, PythonLexer(), Terminal256Formatter())
        except ImportError:
            return source
Esempio n. 13
0
    def __init__(self, globals_dict):
        self.status_text = display(Code(""), display_id=True)
        self._globals_dict = globals_dict
        self._methods = {}

        self.stop_button = Button(description="Stop")
        self.stop_button.on_click(self.on_stop_button_clicked)

        self.canvas = Canvas()
        self.output_text = ""
        self.width, self.height = DEFAULT_CANVAS_SIZE
        self.mouse_x = 0
        self.mouse_y = 0
        self.mouse_is_pressed = False
Esempio n. 14
0
    def show_cells(self, tag):
        if isinstance(tag, str):
            tag = (tag, )
        cells = [
            cell for cell in self.cells if all(t in cell['tags'] for t in tag)
        ]
        out = Carousel()
        display(out)

        for cell in cells:
            with out.capture_item(layout=self.cell_code_layout) as o:
                # HACK: This is to fix syntax highlighting in Jupyterlab
                o.add_class('output_html')
                # TODO: fix overflow for code cells to prevent wrapping.
                display(Code(cell['source'], language='ipython3'))
Esempio n. 15
0
    def yaml(self) -> Union[str, Code]:
        """
        Outputs the uncompiled procedures to YAML.

        Internally, this is a conversion of the output of `Protocol.json` for the purpose of enhanced human readability.

        Returns:
        - YAML of the procedure list.
        When in Jupyter, this string is wrapped in an `IPython.display.Code` object for nice syntax highlighting.

        """
        compiled_yaml = yaml.safe_dump(self.to_list(),
                                       default_flow_style=False)

        if get_ipython():
            return Code(compiled_yaml, language="yaml")
        return compiled_yaml
Esempio n. 16
0
    def __init__(self, globals_dict):
        self.status_text = display(Code(""), display_id=True)
        self._globals_dict = globals_dict
        self._methods = {}

        self.stop_button = Button(description="Stop")
        self.stop_button.on_click(self.on_stop_button_clicked)
        self._globals_dict["canvas"] = Canvas()
        self.kb_mon = Event(source=self.canvas,
                            watched_events=['keydown', 'keyup'],
                            wait=1000 // FRAME_RATE,
                            prevent_default_actions=True)
        self.output_text = ""
        self.color_strings = {"default": "#888888"}
        match_255 = r"(?:(?:2(?:(?:5[0-5])|(?:[0-4][0-9])))|(?:[01]?[0-9]{1,2}))"
        match_alpha = r"(?:(?:1(?:\.0*)?)|(?:0(?:\.[0-9]*)?))"
        match_360 = r"(?:(?:3[0-5][0-9])|(?:[0-2]?[0-9]{1,2}))"
        match_100 = r"(?:100|[0-9]{1,2})"
        self.regexes = [
            re.compile(r"#[0-9A-Fa-f]{6}"),
            re.compile(r"rgb\({},{},{}\)".format(match_255, match_255,
                                                 match_255)),
            re.compile(r"rgba\({},{},{},{}\)".format(match_255, match_255,
                                                     match_255, match_alpha)),
            re.compile(r"hsl\({},{}%,{}%\)".format(match_360, match_100,
                                                   match_100)),
            re.compile(r"hsla\({},{}%,{}%,{}\)".format(match_360, match_100,
                                                       match_100, match_alpha))
        ]
        self.width, self.height = DEFAULT_CANVAS_SIZE
        self.mouse_x = 0
        self.mouse_y = 0
        self.mouse_is_pressed = False
        self.key = ""
        self._keys_held = {}

        # Settings for drawing text (https://ipycanvas.readthedocs.io/en/latest/drawing_text.html).
        self.font_settings = {
            'size': 12.0,
            'font': 'sans-serif',
            'baseline': 'top',
            'align': 'left'
        }
Esempio n. 17
0
 def toggle_answer(question):
     if bt.description == 'Reveal solution':
         # Display solution and change button label
         with sol_area:
             if not out_format:
                 print(sol)
             else:
                 for sol_block, fmt in zip(sol, out_format):
                     if fmt == 'py':
                         display(Code(data=sol_block, language='py'))
                     elif fmt == 'md':
                         display(Markdown(data=sol_block))
                     else:
                         print('Format not recognised...')
         bt.description = 'Hide solution'
     else:
         # Hide solution and change button label
         sol_area.clear_output()
         bt.description = 'Reveal solution'
Esempio n. 18
0
    def show_preview(self, path):
        """Show preview of csv, md or txt in output widget

        Args:
            path (str): Currently selected path
        """
        real_path = re.sub(r"\s\(.*?\)$", "", path)
        parts = real_path.split(".")
        if len(parts) > 0 and parts[-1].lower() in [
                "md",
                "html",
                "csv",
                "txt",
                "sh",
                "sql",
                "py",
                "scala",
                "json",
                "jpg",
                "jpeg",
                "png",
                "gif",
        ]:
            ext = parts[-1].lower()
            filename = "/dbfs" + real_path
            # text = self.dbutils.fs.head(real_path)
            self.preview.clear_output()
            with self.preview:
                if ext == "html":
                    display(HTML(filename=filename))
                elif ext in ["py", "sh", "sql", "scala"]:
                    display(Code(filename=filename))
                elif ext in ["jpg", "jpeg", "png", "gif"]:
                    display(Image(filename=filename))
                elif ext == "md":
                    display(Markdown(filename=filename))
                elif ext == "csv":
                    df = pd.read_csv(filename)
                    display(df)
                else:
                    with open(filename, "r") as fd:
                        print(fd.read())
Esempio n. 19
0
    def start(self, methods):
        self._methods = methods
        draw = self._methods.get("draw", None)
        
        if draw:
            self.print_status("Running...")
            display(self.stop_button)
        else:
            self.print_status("Done drawing")

        display(self.canvas)
        
        self.output_text_code = display(Code(self.output_text), display_id=True)

        self.canvas.on_mouse_down(self.on_mouse_down)
        self.canvas.on_mouse_up(self.on_mouse_up)
        self.canvas.on_mouse_move(self.on_mouse_move)

        thread = threading.Thread(target=self.loop)
        thread.start()
Esempio n. 20
0
    def _iter_cells(self, cells, show=True, append=False, collapsed=False):
        if append and self._run_output:
            run_output = self._run_output
        else:
            run_output = self._run_output = Carousel()

            if show:
                display(run_output)

        for cell in super()._iter_cells(cells):
            with run_output.capture_item(layout=self.grid_cell_layout):
                cell_output = Accordion(children=())
                if '__hide__' not in cell['tags']:
                    display(cell_output)

                i = self.exec_count + 1
                if self.display_code:
                    with cell_output.capture_item(
                            'In [{}]'.format(i),
                            layout=self.cell_code_layout) as o:
                        # HACK: This is to fix syntax highlighting in Jupyterlab
                        o.add_class('output_html')
                        display(Code(cell['source'], language='ipython3'))

                if self.allow_both_code_and_output_open:
                    cell_output = Accordion(children=())
                    if '__hide__' not in cell['tags']:
                        display(cell_output)

                with cell_output.capture_item(
                        'Out [*]',
                        selected=not (collapsed
                                      or '__collapsed__' in cell['tags']),
                        layout=self.cell_output_layout) as o:
                    try:
                        yield cell
                    except GeneratorExit:
                        pass
                    cell_output.set_title(
                        len(cell_output.children) - 1, 'Out [{}]'.format(i))
Esempio n. 21
0
def display_code(code):
    display(Code(code,language='Python'))
Esempio n. 22
0
def show_function(function):
    
    return Code(getsource(function), language='python')
Esempio n. 23
0
def render_ipython(node):
    """Convenience method for rendering AST node in IPython environment."""
    from IPython.display import display, Code  # pylint: disable=import-error
    display(Code(render_module(node), language='python'))
Esempio n. 24
0
def py(filename):
    with open(filename) as f:
        txt = f.read()
        display(Code(txt, language='python'))
        exec(txt)
Esempio n. 25
0
def json_code(j):
    return Code(data=json.dumps(j, indent=4), language="json")
Esempio n. 26
0
 def print_status(self, msg):
     self.status_text.update(Code(msg))
Esempio n. 27
0
    def print(self, *args, sep=' ', end='\n', flush=True):
        global _sparkplug_running
        self.output_text += sep.join([str(arg) for arg in args]) + end

        if _sparkplug_running and flush:
            self.output_text_code.update(Code(self.output_text))
Esempio n. 28
0
    def print(self, msg):
        global _sparkplug_running
        self.output_text += str(msg) + "\n"

        if _sparkplug_running:
            self.output_text_code.update(Code(self.output_text))
def display_sourcecode(fun):
    display(Code(inspect.getsource(fun), language='python3'))
Esempio n. 30
0
 def Code(self, code: Code):
     """Print HTML formatted code to stdout."""
     content = {"data": {"text/html": code._repr_html_(), "text/plain": repr(code)}, "metadata": {}}
     self.send_response(self.iopub_socket, "display_data", content)