diff options
author | Alen <alen@dotfiles.xyz> | 2023-09-25 21:32:05 +0400 |
---|---|---|
committer | Alen <alen@dotfiles.xyz> | 2023-09-25 21:32:05 +0400 |
commit | 4b8ed94012af1fb4f4752b5d199f79c6bcfd4c59 (patch) | |
tree | ad54d702d385e973f367cc02c9115b1948510caa /dot_ipython/profile_default/ipython_config.py | |
parent | 33df3ffbe31ea3b722bfeec21e6a4ebfd038aa00 (diff) |
Add basic IPython config and startup scripts
Diffstat (limited to 'dot_ipython/profile_default/ipython_config.py')
-rw-r--r-- | dot_ipython/profile_default/ipython_config.py | 59 |
1 files changed, 59 insertions, 0 deletions
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 = "" |