Python Forum
Can a program execute code in iPython shell and get result?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can a program execute code in iPython shell and get result?
#1
I am playing around with iPython magic commands. I am trying to write a command to "enter" an object. When the user types:
ENTER expression
the command evaluates the expression which should return an object. The magic command then resets the shell namespace to be object.__dict__.

The function (simplified) looks like this:
def _enter_object(expression):
    object = ???(expression)
    kernel.shell.reset()
    kernel.shell.push(object.__dir__)
What I am looking for is code to evaluate "expression" in the kernel.shell context and return the value of the expression.
Reply
#2
If ipython is installed properly this should do it for you: hover over code you want to execute and press shift enter.

This is probably redundant, but included for those who are unfamiliar with jupyter and ipython:
Note: you also need jupyter notebook installed see: Installing Ipython which also includes instructions here for installing jupyter notebook with ipython.
Reply
#3
I don't think I described my problem well.

I'm using QtConsole which gives me a nice interactive python console for my Qt application. I have no problem executing code inside the console. I can type in code or have it load a file. I can even do debugging. Using iPython through the interface is not the problem. My problem is that I do not know how to excecute code in iPython without using the console interface.

When I make my console I "push" attributes of my application to populate the local namespace in the iPython shell. Many of the objects in my application are hierarchical or composite objects. For example, I have an object called "controller" that is a model of a device my application is controlling. My controller contains a list of "channel" objects. Each of my channel objects has "feedback", "servo" and "drive" objects, and each of these objects have many attributes. Using the console I can modify these attributes by entering python expressions. This expression changes the proportional gain of one of the servo objects:
controller.channel[2].servo.proportional = 2.6
What I'm trying to do is write some "magic" functions that make it easier to navigate around. I would like it if I could do this:
ENTER controller.channel[2].servo
proportional = 2.6
integral = 0.1
error
BACK
This code should set iPython shell local variables to the attributes of the servo object. The servo object attributes are now local variables. I use the attribute/variables to set proportional and integral gains, and display the current servo error. Then I used the BACK magic command to restore the original namespace.

What the "ENTER" magic command does is evaluate the following expression (controller.channel[2].servo) which is a servo object, and push the servo.__dict__ as the "namespace" for the iPython shell. The problem is that the magic function I wrote gets "controller.channel[2].servo" as a string. I want to evaluate the expression in the iPython shell, get back the servo object, and use that to set the namespace.

I have this working without using Magic functions. When I push the namespace I add my own "built-in" functions: enter(expression) and back(). If the user types enter(controller.channel[2].servo) my console application calls this function:
    def _push_context(self, context_object):
        """Push new context object onto the stack"""
        self.contexts.append(context_object)
        return self._restore_context()

    def _pop_context(self):
        """Pop current context off the stack and restore previous context"""
        if len(self.contexts) > 1:
            self.contexts.pop()
        return self.restore_context()

    def _restore_context(self):
        """Set kernel shell namespace to reference the top context object"""
        if len(self.contexts) == 0:
            return None
        obj = self.contexts[-1]
        context = obj.__dict__.copy()
        context["self"] = obj
        context["enter"] = self.push_context
        context["back"] = self.pop_context
        self.kernel.shell.reset()
        self.kernel.shell.push(context)
        return obj
This works fine, but I wanted to see if I could do this with magic functions.
Reply
#4
OK, sorry I didn't understand. This looks intriguing, if you haven't solved it by my AM (it's 11:47 PM EDT here, and I need sleep) Perhaps I can get clever, even if you have solved it, I'd like to give it a try.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IPython errors for numpy array min/max methods muelaner 1 573 Nov-04-2023, 09:22 PM
Last Post: snippsat
  help me simple code result min and max number abrahimusmaximus 2 914 Nov-12-2022, 07:52 AM
Last Post: buran
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 1,515 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  What do i have to type in the IDLE shell to use this password manager program? MaartenRo 4 1,755 Jan-15-2022, 02:01 PM
Last Post: MaartenRo
  Some Code For a program PythonCodeMaker_119 2 2,341 Apr-14-2021, 08:47 PM
Last Post: BashBedlam
  How to make a Python program run in a dos shell (cmd) Pedroski55 2 2,333 Nov-09-2020, 10:17 AM
Last Post: DeaD_EyE
  [split] SyntaxError when trying to execute code on Windows nehaya 2 2,013 Aug-04-2020, 11:18 AM
Last Post: nehaya
  Modify code from running program ? samuelbachorik 2 2,454 Jun-26-2020, 08:17 PM
Last Post: samuelbachorik
  IPython console vs Spyder script losc 3 2,741 Apr-30-2020, 04:57 AM
Last Post: deanhystad
  SyntaxError when trying to execute code on Windows Fred0n 2 2,436 Apr-25-2020, 04:30 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020