Skip to content

Instantly share code, notes, and snippets.

@MatthewScholefield
Last active December 31, 2023 10:53
Show Gist options
  • Save MatthewScholefield/25b4c892126da56d5ff3775a41cd8016 to your computer and use it in GitHub Desktop.
Save MatthewScholefield/25b4c892126da56d5ff3775a41cd8016 to your computer and use it in GitHub Desktop.
Script to Efficiently Extract and Organize IPython Session History: A tool for seamlessly transitioning your prototype code, including import statements and function definitions, from an IPython environment to a structured Python script

IPython History Exporter Gist

Have you ever found yourself prototyping in an IPython session and wished you could easily dump all your code, in a deduplicated form, into a proper Python file? This Gist is your solution. It's designed to extract and organize your IPython history, making it simpler to transition from experimental coding to structured programming.

Usage

Paste the entire content of this Gist into a cell.

The output will display all unique import statements followed by the source code of your defined functions. This output can then be copied into a Python script file.

How It Works

The Gist comprises two primary functions:

  1. _print_import_statements(): Extracts and prints all unique import statements from your IPython session history.
  2. _print_function_sources(): Retrieves and prints the source code of all user-defined functions (excluding those starting with an underscore).

Together, these functions enable you to capture the essence of your IPython session's codebase, neatly organized and ready for further development.

# Define functions with underscore so they aren't included in output
def _print_import_statements():
from IPython import get_ipython
ipython = get_ipython()
if not ipython:
print("This function only works in an IPython environment.")
return
history = list(ipython.history_manager.get_range(output=False))
lines = set()
for _, _, entry in history:
# Simple check for import statements
for line in entry.split('\n'):
if line.strip().startswith("import ") or line.strip().startswith("from "):
lines.add(line)
print('\n'.join(sorted(x for x in lines if x.startswith('import'))))
print()
print('\n'.join(sorted(x for x in lines if x.startswith('from'))))
def _print_function_sources():
import inspect
import contextlib
for name, obj in globals().items():
if name.startswith("_"):
continue # Skip names starting with an underscore
if inspect.isfunction(obj):
with contextlib.suppress(TypeError):
source = inspect.getsource(obj)
print(source)
# Call the functions
_print_import_statements(); print(); print(); _print_function_sources()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment