stack-data is a Python library designed to extract detailed information from stack frames and tracebacks, offering more useful and customizable displays than Python's default tracebacks. It's currently at version 0.6.3 and is actively maintained, powering features in tools like IPython and futurecoder.
pip install stack-dataVerified import paths — ran on the pinned version, not inferred.
This example demonstrates basic usage by creating a FrameInfo object from the current frame and then iterating through its lines to print the source code, highlighting the current line.
Pass `collapse_repeated_frames=False` to `stack_data.FrameInfo` when initializing, e.g., `stack_data.FrameInfo(frame, collapse_repeated_frames=False)`.
Use `line.render(markers, escape_html=True)` when preparing output for HTML display, especially if markers themselves contain unescaped HTML.
For custom indentation control, consider iterating through `line.token_ranges` and reconstructing the line, or use `line.range_from_node(node, data)` for more granular control over specific parts of the line.
Ensure the package is installed using `pip install stack-data`.
Use `line.render()` to get the string representation of the code line.
Upgrade your Python installation to version 3.8 or newer, or switch to an environment with a compatible Python version.
Pass an actual frame object (e.g., obtained from `sys._getframe()` or an exception traceback) to `stack_data.FrameInfo`.