summary refs log tree commit diff
path: root/dot_ipython/profile_default/ipython_config.py
blob: 07ab804080cc2f242d70ede868dab9546452984e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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 = ""