jupyter/qtconsole

How to access objects in my application follow the example qtconsole/examples/inprocess_qtconsole.py?

EvaPeng0107 opened this issue · 2 comments

I start the qtconsole in a .py file just the same as qtconsole/examples/inprocess_qtconsole.py.
However before starting the qtconsole, some local variables need to be defined in inprocess_qtconsole.py, I want to use these local variables in qtconsole without define them in the qtconsole, that is to reach the script local variables in the qtconsole. Could you please give me an example on how to do this?

Take IPython as an example, I want to parse the local variables to the console, the globals and locals can be reached in the console.
IPython.start_ipython(user_ns=dict(globals(), **locals()))

My code below, I want to use "local_a" in the qtconsole directly.

from qtpy import QtWidgets
from qtconsole.rich_jupyter_widget import RichJupyterWidget
from qtconsole.inprocess import QtInProcessKernelManager

def show():
global ipython_widget # Prevent from being garbage collected
local_a = 5
# Create an in-process kernel
kernel_manager = QtInProcessKernelManager()
kernel_manager.start_kernel()
kernel = kernel_manager.kernel
kernel.gui = 'qt4'

kernel_client = kernel_manager.client()
kernel_client.start_channels()

ipython_widget = RichJupyterWidget()
ipython_widget.kernel_manager = kernel_manager
ipython_widget.kernel_client = kernel_client
ipython_widget.show()

if name == "main":
app = QtWidgets.QApplication([])
show()
app.exec_()

Thank you very much.
Eva Peng

You just have to add local_a to the shell user_ns dictionary. I think the following should work:

kernel.shell.user_ns['local_a'] = local_a

You just have to add local_a to the shell user_ns dictionary. I think the following should work:

kernel.shell.user_ns['local_a'] = local_a

Thank you very much! Your reply solved my question. I will go to clear the issue.