From 4b8ed94012af1fb4f4752b5d199f79c6bcfd4c59 Mon Sep 17 00:00:00 2001 From: Alen Date: Mon, 25 Sep 2023 21:32:05 +0400 Subject: Add basic IPython config and startup scripts --- dot_ipython/profile_default/ipython_config.py | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 dot_ipython/profile_default/ipython_config.py (limited to 'dot_ipython/profile_default/ipython_config.py') diff --git a/dot_ipython/profile_default/ipython_config.py b/dot_ipython/profile_default/ipython_config.py new file mode 100644 index 0000000..07ab804 --- /dev/null +++ b/dot_ipython/profile_default/ipython_config.py @@ -0,0 +1,59 @@ +"""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 = "" -- cgit 1.4.1-2-gfad0