"""IPython config.""" import datetime import importlib import sys import IPython from IPython.core.interactiveshell import InteractiveShell from IPython.core.shellapp import InteractiveShellApp from IPython.terminal.prompts import Prompts from pygments.token import Token from traitlets.config import Config c: Config c.InteractiveShell: InteractiveShell c.InteractiveShellApp: InteractiveShellApp class TimePrompt(Prompts): def in_prompt_tokens(self) -> list[tuple[Token, str]]: return [ (Token.Prompt, datetime.datetime.now().strftime("%H:%M")), (Token.Prompt, " ["), (Token.PromptNum, str(self.shell.execution_count)), (Token.Prompt, "]: "), ] def out_prompt_tokens(self) -> list[tuple[Token, str]]: return [ (Token.OutPrompt, datetime.datetime.now().strftime("%H:%M")), (Token.OutPrompt, " ["), (Token.OutPromptNum, str(self.shell.execution_count)), (Token.OutPrompt, "]: "), ] version_parts = [f"IPython {IPython.__version__}"] for key_library, name in ( ("pandas", "pd"), ("numpy", "np"), ("numba", "nb"), ("sqlalchemy", "sa"), ("pydantic", "pydantic"), ): try: module = importlib.import_module(key_library) except ModuleNotFoundError: pass else: version_parts.append(f"{name} {module.__version__}") versions = ", ".join(version_parts) c.InteractiveShellApp.exec_lines = ["%autoreload 2"] c.InteractiveShellApp.extensions = ["autoreload"] c.InteractiveShell.history_length = 100_000 c.InteractiveShell.history_load_length = 100_000 c.InteractiveShell.prompts_class = TimePrompt c.InteractiveShell.banner1 = f"Python {sys.version.split()[0]} [{versions}]\n" c.InteractiveShell.banner2 = ""