Exemple #1
0
 def run_code(self):
     """Run the update of the project inside the :attr:`shell`"""
     if self.line_edit.isVisible():
         text = str(self.line_edit.text())
     else:
         text = str(self.text_edit.toPlainText())
     if not text or not self.fmtos:
         return
     group_ind = self.group_combo.currentIndex()
     if self.groups[group_ind] == COORDSGROUP:
         key = self.fmtos[group_ind][self.fmt_combo.currentIndex()]
         param = 'dims'
     else:
         key = self.fmtos[group_ind][self.fmt_combo.currentIndex()].key
         param = 'fmt'
     if self.yaml_cb.isChecked():
         import psyplot.project as psy
         psy.gcp().update(**{key: yaml.load(text)})
     else:
         code = "psy.gcp().update(%s={'%s': %s})" % (param, key, text)
         if ExecutionInfo is not None:
             info = ExecutionInfo(raw_cell=code, store_history=False,
                                  silent=True, shell_futures=False)
             e = ExecutionResult(info)
         else:
             e = ExecutionResult()
         self.console.run_command_in_shell(code, e)
         try:
             e.raise_error()
         except Exception:  # reset the console and clear the error message
             raise
         finally:
             self.console.reset()
Exemple #2
0
    def run_cell(self,
                 raw_cell,
                 store_history=True,
                 silent=False,
                 shell_futures=True):
        """
        run ipython code in ipython run_cell,
        run python console code in remoute console
        """
        if self.redirect_to_ipython(raw_cell):
            return InteractiveShellEmbed.run_cell(self, raw_cell,
                                                  store_history, silent,
                                                  shell_futures)

        result = ExecutionResult()
        self.displayhook.exec_result = result

        if (not raw_cell) or raw_cell.isspace():
            return result

        cell = self.ipython_pre_works(result, raw_cell, store_history, silent,
                                      shell_futures)

        output = self._runsource(cell)
        if output:
            result.result = output
            self._output(output)
            # self.write(output)
        self.ipython_post_works(result, store_history)

        return result
Exemple #3
0
 def run_code(self):
     """Run the update of the project inside the :attr:`shell`"""
     text = str(self.line_edit.text())
     if not text or not self.fmtos:
         return
     group_ind = self.group_combo.currentIndex()
     if self.groups[group_ind] == COORDSGROUP:
         key = self.fmtos[group_ind][self.fmt_combo.currentIndex()]
         param = 'dims'
     else:
         key = self.fmtos[group_ind][self.fmt_combo.currentIndex()].key
         param = 'fmt'
     e = ExecutionResult()
     self.shell.run_code(
         "psy.gcp().update(%s={'%s': %s})" % (param, key, text), e)
     e.raise_error()
 def run_code(self):
     """Run the update of the project inside the :attr:`shell`"""
     if self.line_edit.isVisible():
         text = str(self.line_edit.text())
     else:
         text = str(self.text_edit.toPlainText())
     if not text or not self.fmtos:
         return
     group_ind = self.group_combo.currentIndex()
     if self.groups[group_ind] == COORDSGROUP:
         key = self.fmtos[group_ind][self.fmt_combo.currentIndex()]
         param = 'dims'
     else:
         key = self.fmtos[group_ind][self.fmt_combo.currentIndex()].key
         param = 'fmt'
     if self.yaml_cb.isChecked():
         import psyplot.project as psy
         psy.gcp().update(**{key: yaml.load(text, Loader=yaml.Loader)})
     else:
         code = "psy.gcp().update(%s={'%s': %s})" % (param, key, text)
         if ExecutionInfo is not None:
             info = ExecutionInfo(raw_cell=code,
                                  store_history=False,
                                  silent=True,
                                  shell_futures=False)
             e = ExecutionResult(info)
         else:
             e = ExecutionResult()
         self.console.run_command_in_shell(code, e)
         try:
             e.raise_error()
         except Exception:  # reset the console and clear the error message
             raise
         finally:
             self.console.reset()
Exemple #5
0
    def _execute_cell(self, cell):
        '''Execute a single cell.'''
        self.exec_count += 1

        ## The original way

        exec(cell['code'], self.ns)

        if HAS_MATPLOTLIB:
            if plt.gcf().axes:
                plt.show()
            else:
                plt.close()
        result = ExecutionResult(ExecutionInfo(cell['source'], False, False, False))

        ## Uses IPython.run_cell to take advantage of IPython output handling

        # See: https://github.com/ipython/ipython/blob/b70b3f21749ca969088fdb54edcc36bb8a2267b9/IPython/core/interactiveshell.py#L2801
        # FIXME: calling run_cell messes with the local scope of lambda functions. No outside variables are defined.
        #        I assume this is also the case for reg functions but I haven't tested.
        # result = self.shell.run_cell(cell['source'])
        return result
Exemple #6
0
    def run_cell(self, s, store_history=True, silent=False, shell_futures=True):

        """Run a complete IPython cell.

        Parameters
        ----------
        raw_cell : str
          The code (including IPython code such as %magic functions) to run.
        store_history : bool
          If True, the raw and translated cell will be stored in IPython's
          history. For user code calling back into IPython's machinery, this
          should be set to False.
        silent : bool
          If True, avoid side-effects, such as implicit displayhooks and
          and logging.  silent=True forces store_history=False.
        shell_futures : bool
          If True, the code will share future statements with the interactive
          shell. It will both be affected by previous __future__ imports, and
          any __future__ imports in the code will affect the shell. If False,
          __future__ imports are not shared in either direction.

        Returns
        -------
        result : :class:`ExecutionResult`
        """

        result = ExecutionResult()

        if (not s) or s.isspace():
            self.shell.last_execution_succeeded = True
            return result

        if store_history:
            result.execution_count = self.shell.execution_count

        def error_before_exec(value):
            result.error_before_exec = value
            self.shell.last_execution_succeeded = False
            return result


        if not self.q:
            try:
                self.q = self.yapeng.query(s)
            except SyntaxError:
                return error_before_exec( sys.exc_info()[1])

        cell = s  # cell has to exist so it can be stored/logged

        # Store raw and processed history
        # if not silent:
        #    self.shell..logger.log(cell, s)

        has_raised = False
        try:
            #f = io.StringIO()
            # with redirect_stdout(f):
            run = self.q.next()
            # print('{0}'.format(f.getvalue()))
            # Execute the user code
            if run:
                myvs = self.q.namedVarsCopy()
                if myvs:
                    i = 0
                    for peq in myvs:
                        name = peq[0]
                        bind = peq[1]
                        if bind.isVar():
                            var = yap.YAPAtom('$VAR')
                            f = yap.YAPFunctor(var, 1)
                            bind.unify(yap.YAPApplTerm(f, (name)))
                        else:
                            i = bind.numberVars(i, True)
                            print(name.text() + " = " + bind.text())
                else:
                    print("yes")
                if self.q.deterministic():
                    self.closeq()
            else:
                print("No (more) answers")
                self.closeq()
        except:
            result.error_in_exec = sys.exc_info()[1]
            # self.showtraceback()
            has_raised = True
            self.closeq()


        self.shell.last_execution_succeeded = not has_raised
        result.result = self.shell.last_execution_succeeded
        print( self.q )
        # Reset this so later displayed values do not modify the
        # ExecutionResult
        # self.displayhook.exec_result = None

        #self.events.trigger('post_execute')
        #if not silent:
        #    self.events.trigger('post_run_cell')

        if store_history:
            # Write output to the database. Does nothing unless
            # history output logging is enabled.
            # self.history_manager.store_output(self.execution_count)
            # Each cell is a *single* input, regardless of how many lines it has
            self.shell.execution_count += 1

        return result
Exemple #7
0
    def run_cell(self,
                 raw_cell,
                 uuid=None,
                 code_dict={},
                 store_history=False,
                 silent=False,
                 shell_futures=True,
                 update_downstream_deps=False):
        """Run a complete IPython cell.

        Parameters
        ----------
        raw_cell : str
          The code (including IPython code such as %magic functions) to run.
        store_history : bool
          If True, the raw and translated cell will be stored in IPython's
          history. For user code calling back into IPython's machinery, this
          should be set to False.
        silent : bool
          If True, avoid side-effects, such as implicit displayhooks and
          and logging.  silent=True forces store_history=False.
        shell_futures : bool
          If True, the code will share future statements with the interactive
          shell. It will both be affected by previous __future__ imports, and
          any __future__ imports in the code will affect the shell. If False,
          __future__ imports are not shared in either direction.

        Returns
        -------
        result : :class:`ExecutionResult`
        """

        # print("CODE_DICT:", code_dict)
        # print("RUNNING CELL", uuid, raw_cell)
        # print("RUN_CELL USER_NS:", self.user_ns)
        self._last_traceback = None

        if store_history:
            self.dataflow_history_manager.update_codes(code_dict)
            # also put the current cell into the cache and force recompute
            if uuid not in code_dict:
                self.dataflow_history_manager.update_code(uuid, raw_cell)
            self.dataflow_history_manager.update_flags(
                store_history=store_history,
                silent=silent,
                shell_futures=shell_futures,
                update_downstream_deps=update_downstream_deps)
        result = ExecutionResult()

        if (not raw_cell) or raw_cell.isspace():
            self.last_execution_succeeded = True
            return result

        if silent:
            store_history = False

        if store_history:
            result.execution_count = uuid

        def error_before_exec(value):
            result.error_before_exec = value
            self.last_execution_succeeded = False
            return result

        self.events.trigger('pre_execute')
        if not silent:
            self.events.trigger('pre_run_cell')

        # If any of our input transformation (input_transformer_manager or
        # prefilter_manager) raises an exception, we store it in this variable
        # so that we can display the error after logging the input and storing
        # it in the history.
        preprocessing_exc_tuple = None
        try:
            # Static input transformations
            cell = self.input_transformer_manager.transform_cell(raw_cell)
        except SyntaxError:
            preprocessing_exc_tuple = sys.exc_info()
            cell = raw_cell  # cell has to exist so it can be stored/logged
        else:
            if len(cell.splitlines()) == 1:
                # Dynamic transformations - only applied for single line commands
                with self.builtin_trap:
                    try:
                        # use prefilter_lines to handle trailing newlines
                        # restore trailing newline for ast.parse
                        cell = self.prefilter_manager.prefilter_lines(
                            cell) + '\n'
                    except Exception:
                        # don't allow prefilter errors to crash IPython
                        preprocessing_exc_tuple = sys.exc_info()

        # Store raw and processed history
        if store_history:
            self.execution_count += 1
            # store cur_execution_count because of recursion
            cur_execution_count = self.execution_count
            # print("STORING INPUTS:", self.execution_count)
            self.history_manager.store_inputs(self.execution_count, cell,
                                              raw_cell)
        if not silent:
            self.logger.log(cell, raw_cell)

        # Display the exception if input processing failed.
        if preprocessing_exc_tuple is not None:
            self.showtraceback(preprocessing_exc_tuple)
            # if store_history:
            #     self.execution_count += 1
            return error_before_exec(preprocessing_exc_tuple[2])

        # Our own compiler remembers the __future__ environment. If we want to
        # run code with a separate __future__ environment, use the default
        # compiler
        compiler = self.compile if shell_futures else CachingCompiler()

        with self.builtin_trap:
            # TODO seems that uuid is more appropriate than execution_count here
            cell_name = self.compile.cache(cell, uuid)

            with self.display_trap:
                # Compile to bytecode
                try:
                    code_ast = compiler.ast_parse(cell, filename=cell_name)
                except self.custom_exceptions as e:
                    etype, value, tb = sys.exc_info()
                    self.CustomTB(etype, value, tb)
                    return error_before_exec(e)
                except IndentationError as e:
                    self.showindentationerror()
                    # if store_history:
                    #     self.execution_count += 1
                    return error_before_exec(e)
                except (OverflowError, SyntaxError, ValueError, TypeError,
                        MemoryError) as e:
                    self.showsyntaxerror()
                    # if store_history:
                    #     self.execution_count += 1
                    return error_before_exec(e)

                # Apply AST transformations
                try:
                    code_ast = self.transform_ast(code_ast)
                except InputRejected as e:
                    self.showtraceback()
                    # if store_history:
                    #     self.execution_count += 1
                    return error_before_exec(e)

                # Give the displayhook a reference to our ExecutionResult so it
                # can fill in the output value.

                # displayhook exec_result changed to reflect recursion
                old_result = self.displayhook.exec_result
                self.displayhook.exec_result = result
                old_uuid = self.uuid
                self.uuid = uuid

                # Execute the user code
                interactivity = "none" if silent else self.ast_node_interactivity
                has_raised = self.run_ast_nodes(code_ast.body,
                                                cell_name,
                                                interactivity=interactivity,
                                                compiler=compiler,
                                                result=result)

                self.last_execution_succeeded = not has_raised

                # Reset this so later displayed values do not modify the
                # ExecutionResult
                self.displayhook.exec_result = old_result
                self.uuid = old_uuid

                self.events.trigger('post_execute')
                if not silent:
                    self.events.trigger('post_run_cell')

        if not has_raised:
            if store_history:
                # Write output to the database. Does nothing unless
                # history output logging is enabled.
                # print("STORING HISTORY", cur_execution_count)
                self.history_manager.store_output(cur_execution_count)
                # print("STORING UPDATE VALUE:", uuid, result)
                self.dataflow_history_manager.update_value(uuid, result.result)
                self.dataflow_history_manager.set_not_stale(uuid)

                # Each cell is a *single* input, regardless of how many lines it has
                # self.execution_count += 1

            if store_history:
                result.imm_upstream_deps = self.dataflow_history_manager.get_upstream(
                    uuid)
                result.all_upstream_deps = self.dataflow_history_manager.all_upstream(
                    uuid)
                result.imm_downstream_deps = self.dataflow_history_manager.get_downstream(
                    uuid)
                result.all_downstream_deps = self.dataflow_history_manager.all_downstream(
                    uuid)

        return result
Exemple #8
0
    def run_cell(self, raw_cell, store_history=True, silent=False,
                 shell_futures=True):
        """Run a complete IPython cell.

        Parameters
                   ----------
                   raw_cell : str
                   The code (including IPython code such as
                   %magic functions) to run.
                   store_history : bool
          If True, the raw and translated cell will be stored in IPython's
                   history. For user code calling back into
                   IPython's machinery, this
                   should be set to False.
                   silent : bool
          If True, avoid side-effects, such as implicit displayhooks and
                   and logging.  silent=True forces store_history=False.
                   shell_futures : bool
          If True, the code will share future statements with the interactive
                   shell. It will both be affected by previous
                    __future__ imports, and any __future__ imports in the code
                     will affect the shell. If False,
                   __future__ imports are not shared in either direction.

        Returns

                   -------

`result : :class:`ExecutionResult`
                   """

        # construct a query from a one-line string
        # q is opaque to Python
        # vs is the list of variables
        # you can print it out, the left-side is the variable name,
        # the right side wraps a handle to a variable
        # pdb.set_trace()
        #     #pdb.set_trace()
        # atom match either symbols, or if no symbol exists, strings, In this case
        # variable names should match strings
        # ask = True
        # launch the query
        result = ExecutionResult()

        if (not raw_cell) or raw_cell.isspace():
            self.last_execution_succeeded = True
            return result

        if silent:
            store_history = False

        if store_history:
            result.execution_count = self.execution_count

        def error_before_exec(value):
            result.error_before_exec = value
            self.last_execution_succeeded = False
            return result

        self.events.trigger('pre_execute')
        if not silent:
            self.events.trigger('pre_run_cell')

        # If any of our input transformation (input_transformer_manager or
        # prefilter_manager) raises an exception, we store it in this variable
        # so that we can display the error after logging the input and storing
        # it in the history.
        # preprocessing_exc_tuple = None
        # try:
        #     # Static input transformations
        #     cell = raw_cell #self.input_transformer_manager.transform_cell(raw_cell)
        # except SyntaxError:
        #     preprocessing_exc_tuple = sys.exc_info()
        cell = raw_cell  # cell has to exist so it can be stored/logged
        # else:
        #     # import pdb; pdb.set_trace()
        #     if False and len(cell.splitlines()) == 1:
        #         # Dynamic transformations - only applied for single line commands
        #         with self.builtin_trap:
        #             try:
        #                 # use prefilter_lines to handle trailing newlines
        #                 # restore trailing newline for ast.parse
        #                 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
        #             except Exception:
        #                 # don't allow prefilter errors to crash IPython
        #                 preprocessing_exc_tuple = sys.exc_info()


        # Store raw and processed history
        if store_history:
            self.history_manager.store_inputs(self.execution_count,
                                              cell, raw_cell)
        if not silent:
            self.logger.log(cell, raw_cell)

        # # Display the exception if input processing failed.
        # if preprocessing_exc_tuple is not None:
        #     self.showtraceback(preprocessing_exc_tuple)
        #     if store_history:
        #         self.execution_count += 1
        #     return error_before_exec(preprocessing_exc_tuple[2])

        # Our own compiler remembers the __future__ environment. If we want to
        # run code with a separate __future__ environment, use the default
        # compiler
        # compiler = self.compile if shell_futures else CachingCompiler()

        cell_name = str( self.execution_count)

        if cell[0] == '%':
            if cell[1] == '%':
                linec = False
                mcell = cell.lstrip('%%')
            else:
                linec = True
                mcell = cell.lstrip('%')
            txt0 = mcell.split(maxsplit = 2, sep = '\n')
            txt = txt0[0].split(maxsplit = 2)
            magic = txt[0]
            if len(txt) == 2:
                line = txt[1]
            else:
                line = ""
            if linec:
                self.run_line_magic(magic, line)
                if len(txt0) == 2:
                    cell = txt0[1]
                else:
                    cellArea = ""
            else:
                self.run_cell_magic(magic, line, cell)
                return
        # Give the displayhook a reference to our ExecutionResult so it
        # can fill in the output value.
        self.displayhook.exec_result = result
        has_raised = False
        try:
            self.bindings = dict = {}
            state = self.jupyter_query(cell)
            if state:
                self.last_execution_succeeded = True
                result.result = (True, dict)
            else:
                self.last_execution_succeeded = True
                result.result = (True, {})
        except Exception as e:
            print(e)
            has_raised = True
            result.result = False

        self.last_execution_succeeded = not has_raised

        # Reset this so later displayed values do not modify the
        # ExecutionResult
        self.displayhook.exec_result = None
        print(self.complete)
        self.events.trigger('post_execute')
        if not silent:
            self.events.trigger('post_run_cell')

        if store_history:
            # Write output to the database. Does nothing unless
            # history output logging is enabled.
            self.history_manager.store_output(self.execution_count)
            # Each cell is a *single* input, regardless of how many lines it has
            self.execution_count += 1

        return result
Exemple #9
0
    def run_cell(self,
                 s,
                 store_history=True,
                 silent=False,
                 shell_futures=True):
        """Run a complete IPython cell.

        Parameters
        ----------
        raw_cell : str
          The code (including IPython code such as %magic functions) to run.
        store_history : bool
          If True, the raw and translated cell will be stored in IPython's
          history. For user code calling back into IPython's machinery, this
          should be set to False.
        silent : bool
          If True, avoid side-effects, such as implicit displayhooks and
          and logging.  silent=True forces store_history=False.
        shell_futures : bool
          If True, the code will share future statements with the interactive
          shell. It will both be affected by previous __future__ imports, and
          any __future__ imports in the code will affect the shell. If False,
          __future__ imports are not shared in either direction.

        Returns
        -------
        result : :class:`ExecutionResult`
        """

        result = ExecutionResult()

        if (not s) or s.isspace():
            self.shell.last_execution_succeeded = True
            return result

        if store_history:
            result.execution_count = self.shell.execution_count

        def error_before_exec(value):
            result.error_before_exec = value
            self.shell.last_execution_succeeded = False
            return result

        if not self.q:
            try:
                self.q = self.yapeng.query(s)
            except SyntaxError:
                return error_before_exec(sys.exc_info()[1])

        cell = s  # cell has to exist so it can be stored/logged

        # Store raw and processed history
        # if not silent:
        #    self.shell..logger.log(cell, s)

        has_raised = False
        try:
            #f = io.StringIO()
            # with redirect_stdout(f):
            run = self.q.next()
            # print('{0}'.format(f.getvalue()))
            # Execute the user code
            if run:
                myvs = self.q.namedVarsCopy()
                if myvs:
                    i = 0
                    for peq in myvs:
                        name = peq[0]
                        bind = peq[1]
                        if bind.isVar():
                            var = yap.YAPAtom('$VAR')
                            f = yap.YAPFunctor(var, 1)
                            bind.unify(yap.YAPApplTerm(f, (name)))
                        else:
                            i = bind.numberVars(i, True)
                            print(name.text() + " = " + bind.text())
                else:
                    print("yes")
                if self.q.deterministic():
                    self.closeq()
            else:
                print("No (more) answers")
                self.closeq()
        except:
            result.error_in_exec = sys.exc_info()[1]
            # self.showtraceback()
            has_raised = True
            self.closeq()

        self.shell.last_execution_succeeded = not has_raised
        result.result = self.shell.last_execution_succeeded
        print(self.q)
        # Reset this so later displayed values do not modify the
        # ExecutionResult
        # self.displayhook.exec_result = None

        #self.events.trigger('post_execute')
        #if not silent:
        #    self.events.trigger('post_run_cell')

        if store_history:
            # Write output to the database. Does nothing unless
            # history output logging is enabled.
            # self.history_manager.store_output(self.execution_count)
            # Each cell is a *single* input, regardless of how many lines it has
            self.shell.execution_count += 1

        return result
Exemple #10
0
    async def run_workflow(self, notebook):
        result = ExecutionResult(None)

        def error_before_exec(val):
            result.error_before_exec = val
            self.last_execution_succeeded = False
            self.last_execution_result = result
            return result

        cells = [
            self.transform_cell(cell['code']) for cell in notebook['cells']
        ]
        with self.builtin_trap, self.display_trap:
            try:
                # Extract cells code
                jupyter_cells = []
                for cell, metadata in zip(cells, [
                        c.get('metadata', {'step': {}})
                        for c in notebook['cells']
                ]):
                    cell_name = self.compile.cache(cell,
                                                   self.execution_count,
                                                   raw_code=cell)
                    code_ast = self.compile.ast_parse(cell, filename=cell_name)
                    code_ast = self.transform_ast(code_ast)
                    to_run = [(node, 'exec') for node in code_ast.body]
                    jupyter_cells.append(
                        JupyterCell(name=cell_name,
                                    code=to_run,
                                    compiler=self.compile,
                                    metadata=metadata))
                # Build workflow
                translator = JupyterNotebookTranslator(context=self.context)
                workflow = await translator.translate(
                    JupyterNotebook(cells=jupyter_cells,
                                    autoawait=self.autoawait,
                                    metadata=notebook.get('metadata')))
                # Inject inputs
                input_injector = BaseJob(name=utils.random_name(),
                                         step=BaseStep(utils.random_name(),
                                                       self.context),
                                         inputs=[])
                for step in workflow.steps.values():
                    await self._inject_inputs(step=step, job=input_injector)
            except self.custom_exceptions as e:
                etype, value, tb = sys.exc_info()
                self.CustomTB(etype, value, tb)
                return error_before_exec(e)
            except (InputRejected, WorkflowDefinitionException) as e:
                self.showtraceback()
                return error_before_exec(e)
            except IndentationError as e:
                self.showindentationerror()
                return error_before_exec(e)
            except (OverflowError, SyntaxError, ValueError, TypeError,
                    MemoryError) as e:
                self.showsyntaxerror()
                return error_before_exec(e)
            self.displayhook.exec_result = result
            # Execute workflow
            d = tempfile.mkdtemp()
            try:
                with open(os.devnull, 'w') as devnull:
                    with redirect_stdout(devnull), redirect_stderr(devnull):
                        await StreamFlowExecutor(
                            context=self.context,
                            workflow=workflow).run(output_dir=d)
                        # Print output logs
                        output_retriever = utils.random_name()
                        d = tempfile.mkdtemp()
                        result.result = {}
                        for step in workflow.steps.values():
                            output = await _get_output(
                                step=step,
                                output_retriever=output_retriever,
                                d=d)
                            if output:
                                result.result[step.name] = output
            except:
                if result:
                    result.error_before_exec = sys.exc_info()[1]
                self.showtraceback()
        return result
Exemple #11
0
    async def run_cell_async(
            self,
            raw_cell: str,
            store_history=False,
            silent=False,
            shell_futures=True,
            *,
            transformed_cell: Optional[str] = [],
            cell_id=None,
            preprocessing_exc_tuple: Optional[Any] = []) -> ExecutionResult:
        """Run a complete IPython cell asynchronously.

        Parameters
        ----------
        raw_cell : str
          The code (including IPython code such as %magic functions) to run.
        store_history : bool
          If True, the raw and translated cell will be stored in IPython's
          history. For user code calling back into IPython's machinery, this
          should be set to False.
        silent : bool
          If True, avoid side-effects, such as implicit displayhooks and
          and logging.  silent=True forces store_history=False.
        shell_futures : bool
          If True, the code will share future statements with the interactive
          shell. It will both be affected by previous __future__ imports, and
          any __future__ imports in the code will affect the shell. If False,
          __future__ imports are not shared in either direction.
        transformed_cell: str
          cell that was passed through transformers
        preprocessing_exc_tuple:
          trace if the transformation failed.

        Returns
        -------
        result : :class:`ExecutionResult`

        .. versionadded: 7.0
        """
        info = ExecutionInfo(raw_cell, store_history, silent, shell_futures,
                             cell_id)
        result = ExecutionResult(info)

        if (not raw_cell) or raw_cell.isspace():
            self.last_execution_succeeded = True
            self.last_execution_result = result
            return result

        if (not transformed_cell) or transformed_cell.isspace():
            self.last_execution_succeeded = True
            self.last_execution_result = result
            return result

        if silent:
            store_history = False

        if store_history:
            result.execution_count = self.execution_count

        self.events.trigger('pre_execute')
        if not silent:
            self.events.trigger('pre_run_cell', info)

        if transformed_cell is None:
            warnings.warn(
                "`run_cell_async` will not call `transform_cell`"
                " automatically in the future. Please pass the result to"
                " `transformed_cell` argument and any exception that happen"
                " during the ffsform in `preprocessing_exc_tuple` in"
                " IPython 7.17 and above.",
                DeprecationWarning,
                stacklevel=2,
            )
            engine.shell = self
            # If any of our input transformation (input_transformer_manager or
            # prefilter_manager) raises an exception, we store it in this variable
            # so that we can display the error after logging the input and storing
            # it in the history.
            try:
                cell = self.transform_cell(raw_cell)
            except IndentationError as e:
                preprocessing_exc_tuple = None
                cell = raw_cell  # cell has to exist so it can be stored/logged
            except Exception as e:
                preprocessing_exc_tuple = sys.exc_info()
                cell = raw_cell  # cell has to exist so it can be stored/logged
            else:
                preprocessing_exc_tuple = None
        else:
            if preprocessing_exc_tuple is None:
                cell = transformed_cell
            else:
                cell = raw_cell
        _run_async = False
        # Store raw and processed history
        if store_history:
            self.history_manager.store_inputs(self.execution_count, cell,
                                              raw_cell)
        if not silent:
            self.logger.log(cell, raw_cell)

        def error_before_exec(value):
            if store_history:
                self.execution_count += 1
            result.error_before_exec = value
            self.last_execution_succeeded = False
            self.last_execution_result = result
            return result

        # Display the exception if input processing failed.
        if preprocessing_exc_tuple is not None:
            showtraceback(preprocessing_exc_tuple)
            if store_history:
                self.execution_count += 1
            return error_before_exec(preprocessing_exc_tuple[1])

        # Our own compiler remembers the __future__ environment. If we want to
        # run code with a separate __future__ environment, use the default
        # compiler
        #compiler = self.compile if shell_futures else self.compiler_class()
        has_raised = False
        if raw_cell.find("#!python") == 0 or raw_cell.find("# %%") == 0:
            # Our own compiler remembers the __future__ environment. If we want to
            # run code with a separate __future__ environment, use the default
            # compiler
            compiler = self.compile if shell_futures else self.compiler_class()

            _run_async = False

            with self.builtin_trap:
                cell_name = compiler.cache(cell,
                                           self.execution_count,
                                           raw_code=raw_cell)

                with self.display_trap:
                    # Compile to bytecode
                    try:
                        if sys.version_info < (3, 8) and self.autoawait:
                            if _should_be_async(cell):
                                # the code AST below will not be user code: we wrap it
                                # in an `async def`. This will likely make some AST
                                # transformer below miss some transform opporunity and
                                # introduce a small coupling to run_code (in which we
                                # bake some assumptions of what _ast_asyncify returns.
                                # they are ways around (like grafting part                                                                                                                           of the ast
                                # later:
                                #    - Here, return code_ast.body[0].body[1:-1], as well
                                #    as last expression in  return statement which is
                                #    the user code part.
                                #    - Let it go through the AST transformers, and graft
                                #    - it back after the AST transform
                                # But that seem unreasonable, at least while we
                                # do not need it.
                                code_ast = _ast_asyncify(
                                    cell, 'async-def-wrapper')
                                _run_async = True
                            else:
                                code_ast = compiler.ast_parse(
                                    cell, filename=cell_name)
                        else:
                            code_ast = compiler.ast_parse(cell,
                                                          filename=cell_name)
                    except self.custom_exceptions as e:
                        etype, value, tb = sys.exc_info()
                        self.CustomTB(etype, value, tb)
                        return error_before_exec(e)
                    except IndentationError as e:
                        return error_before_exec(e)
                    except (OverflowError, SyntaxError, ValueError, TypeError,
                            MemoryError) as e:
                        self.showsyntaxerror()
                        return error_before_exec(e)

                    # Apply AST transformations
                    try:
                        code_ast = self.transform_ast(code_ast)
                    except InputRejected as e:
                        self.showtraceback()
                        return error_before_exec(e)

                    # Give the displayhook a reference to our ExecutionResult so it
                    # can fill in the output value.
                    self.displayhook.exec_result = result

                    # Execute the user code
                    interactivity = "none" if silent else self.ast_node_interactivity
                    if _run_async:
                        interactivity = 'async'

                has_raised = await self.run_ast_nodes(
                    code_ast.body,
                    cell_name,
                    interactivity=interactivity,
                    compiler=compiler,
                    result=result)
        else:
            if raw_cell.isspace():
                return result

            self.engine = engine

            # Execute the user code
            interactivity = "none" if silent else 'all'
            if _run_async:
                interactivity = 'async'
            has_raised = await engine.jupyter_cell(result,
                                                   raw_cell,
                                                   self,
                                                   store_history=store_history)

            self.last_execution_succeeded = not has_raised
            self.last_execution_result = result

        # Reset this so later displayed values do not modify the
        # ExecutionResult
        self.displayhook.exec_result = None

        if store_history:
            # Write output to the database. Does nothing unless
            # history output logging is enabled.
            self.history_manager.store_output(self.execution_count)
            # Each cell is a *single* input, rgeardless of how many lines it has
            self.execution_count += 1

        return result
    def run_cell(self,
                 s,
                 store_history=True,
                 silent=False,
                 shell_futures=True):
        """Run a complete IPython cell.

        Parameters
                   ----------
                   raw_cell : str
                   The code (including IPython code such as
                   %magic functions) to run.
                   store_history : bool
          If True, the raw and translated cell will be stored in IPython's
                   history. For user code calling back into
                   IPython's machinery, this
                   should be set to False.
                   silent : bool
          If True, avoid side-effects, such as implicit displayhooks and
                   and logging.  silent=True forces store_history=False.
                   shell_futures : bool
          If True, the code will share future statements with the interactive
                   shell. It will both be affected by previous
                    __future__ imports, and any __future__ imports in the code
                     will affect the shell. If False,
                   __future__ imports are not shared in either direction.

        Returns
                   -------
rquwer                                                                                 result : :class:`ExecutionResult`
                   """

        # construct a query from a one-line string
        # q is opaque to Python
        # vs is the list of variables
        # you can print it out, the left-side is the variable name,
        # the right side wraps a handle to a variable
        # pdb.set_trace()
        #     #pdb.set_trace()
        # atom match either symbols, or if no symbol exists, strings, In this case
        # variable names should match strings
        # ask = True
        # launch the query
        result = ExecutionResult()
        if not s or s.isspace():
            self.shell.last_execution_succeeded = True
            return result

        if store_history:
            result.execution_count = self.shell.execution_count

        try:
            if self.q == jupyter_query(s, Dict):
                self.shell.last_execution_succeeded = True
                result.result = (True, Dict)
            else:
                self.shell.last_execution_succeeded = True
                result.result = (True, {})
        except Exception as e:
            print(e)
            self.shell.last_execution_succeeded = False
            result.result = False

        # Reset this so later displayed values do not modify the
        # ExecutionResult
        # self.displayhook.exec_result = None

        #self.events.trigger('post_execute')
        #if not silent:
        #    self.events.trigger('post_run_cell')

        if store_history:
            # Write output to the database. Does nothing unless
            # history output logging is enabled.
            # self.history_manager.store_output(self.execution_count)
            # Each cell is a *single* input, regardless of how many lines it has
            self.shell.execution_count += 1

        return result