LoginSignup
0
0

More than 1 year has passed since last update.

ipythonでプロンプトをカスタマイズする

Last updated at Posted at 2022-09-30

ipythonのデフォルトのプロンプトは以下のようになっている:

In [1]:
Out[1]:

これを

>>> print('hello')
hello

>>> 

に変える。

このPythonスクリプトを実行すればよい

from IPython.terminal.prompts import Prompts, Token
import os
class MyPrompt(Prompts):
      def in_prompt_tokens(self, cli=None):
          return [(Token.Prompt, '>>> ')]
      def out_prompt_tokens(self, cli=None):
          return []

ip = get_ipython()
ip.prompts = MyPrompt(ip)

ipythonを起動してこれを実行すればその場でプロンプトが>>> に変わる。

くわて、Out[1]みたいなアウトプット時の表示も消える。

スタートアップに設定

毎回上記のスクリプトをいちいち書いて実行するのは面倒だから、ipython起動時に自動で実行するように設定する。

そのために、~/.ipython/profile_default/startupディレクトリを作成する。

そして上のスクリプトをstart.pyとしてこのディレクトリに保存すればOK(~/.ipython/profile_default/startup/start.py

これで次回の起動からプロンプトが>>> になってる。

ちなみに、ipython実行時のオプションで--classicを指定するとプロンプトが>>> になるが、そうすると色がつかないので注意。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0