0

My desired outcome is to get, similar to what is possible in qtconsole, this type of output:

%hist
import pandas as pd
pd.DataFrame()

But for code run in the python runtime.

I have explored inspect, pdb and traceback but they do not give the clean output of only the code in the __main__ scope.

How would I extract code run in the __main__ or current (eg: only in one function) scope?

I tried hacking around in https://github.com/ipython/ipython/blob/master/IPython/core/magics/code.py and doing stuff like:

import IPython.core.magics.code as magic_code
c = magic_code.CodeMagics()
c.save(parameter_s='%hist')

but to no avail. Thanks for any guidance.

jason m
  • 6,519
  • 20
  • 69
  • 122
  • The key to its action is `get` calls to the `self.shell.history_manager`. I don't know the details about this, but the manager has to be running (in the background) through the session. In `ipython` session, the manager manages the `In` list and `Out` dict. The history is derived from those. In other words, `%history` isn't getting any information from the base Python interpreters; it depends on the `ipython` console. – hpaulj Feb 21 '20 at 22:04
  • @hpaulj okay! let's say I was in a qtconsole or notebook environment. How would I access the current session's history? – jason m Feb 26 '20 at 19:13

0 Answers0