jupyter notebookのテーマ(CSS)を簡単に変える方法

jupyter notebookのテーマを変更するには、custom.cssを直接編集するという常套手段が存在するが、それだと、後でdefault themeに戻すのに苦労するので、notebook毎にスタイルを変えるのが良いようだ。テーマの変更にはjupyterthemesを使うという手もある。テーマの変更は非常に簡単で、適当なcssをネットで探してきて、それをimportするだけでいい。

from IPython.core.display import display, HTML
a = open("../custom.css", "r")   
display(HTML('<style>{}</style>'.format(a.read())))

ネットから直接引っ張ってくるという手もあるが、これだと、将来的にリンク切れが生じた時に困るので、やはり、一旦CSSをダウンロードして使うのがいいようだ。

from IPython.core.display import display, HTML
import urllib.request
r = urllib.request.urlopen('http://bit.ly/1LC7EI7')
display(HTML('<style>{}</style>'.format(r.read().decode("utf-8"))))
-----------------------------------------------------------------------
HTTPError                             Traceback (most recent call last)
<ipython-input-2-2fe7acc12f5c> in <module>()
      2 import urllib.request
      3 # this link is to my Kalman filter book CSS file.
----> 4 r = urllib.request.urlopen('http://bit.ly/1LC7EI7')
      5 display(HTML('<style>{}</style>'.format(r.read().decode("utf-8"))))

~/.pyenv/versions/3.6.5/lib/python3.6/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    221     else:
    222         opener = _opener
--> 223     return opener.open(url, data, timeout)
    224 
    225 def install_opener(opener):

~/.pyenv/versions/3.6.5/lib/python3.6/urllib/request.py in open(self, fullurl, data, timeout)
    530         for processor in self.process_response.get(protocol, []):
    531             meth = getattr(processor, meth_name)
--> 532             response = meth(req, response)
    533 
    534         return response

~/.pyenv/versions/3.6.5/lib/python3.6/urllib/request.py in http_response(self, request, response)
    640         if not (200 <= code < 300):
    641             response = self.parent.error(
--> 642                 'http', request, response, code, msg, hdrs)
    643 
    644         return response

~/.pyenv/versions/3.6.5/lib/python3.6/urllib/request.py in error(self, proto, *args)
    562             http_err = 0
    563         args = (dict, proto, meth_name) + args
--> 564         result = self._call_chain(*args)
    565         if result:
    566             return result

~/.pyenv/versions/3.6.5/lib/python3.6/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    502         for handler in handlers:
    503             func = getattr(handler, meth_name)
--> 504             result = func(*args)
    505             if result is not None:
    506                 return result

~/.pyenv/versions/3.6.5/lib/python3.6/urllib/request.py in http_error_302(self, req, fp, code, msg, headers)
    754         fp.close()
    755 
--> 756         return self.parent.open(new, timeout=req.timeout)
    757 
    758     http_error_301 = http_error_303 = http_error_307 = http_error_302

~/.pyenv/versions/3.6.5/lib/python3.6/urllib/request.py in open(self, fullurl, data, timeout)
    530         for processor in self.process_response.get(protocol, []):
    531             meth = getattr(processor, meth_name)
--> 532             response = meth(req, response)
    533 
    534         return response

~/.pyenv/versions/3.6.5/lib/python3.6/urllib/request.py in http_response(self, request, response)
    640         if not (200 <= code < 300):
    641             response = self.parent.error(
--> 642                 'http', request, response, code, msg, hdrs)
    643 
    644         return response

~/.pyenv/versions/3.6.5/lib/python3.6/urllib/request.py in error(self, proto, *args)
    568         if http_err:
    569             args = (dict, 'default', 'http_error_default') + orig_args
--> 570             return self._call_chain(*args)
    571 
    572 # XXX probably also want an abstract factory that knows when it makes

~/.pyenv/versions/3.6.5/lib/python3.6/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    502         for handler in handlers:
    503             func = getattr(handler, meth_name)
--> 504             result = func(*args)
    505             if result is not None:
    506                 return result

~/.pyenv/versions/3.6.5/lib/python3.6/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
    648 class HTTPDefaultErrorHandler(BaseHandler):
    649     def http_error_default(self, req, fp, code, msg, hdrs):
--> 650         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    651 
    652 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 404: Not Found

以下の例のように、CSSを直接セル内に書き込むこともできる。

from IPython.core.display import display, HTML

css = """
.container {
width:100%;
}
div.CodeMirror pre {
font-family: 'YaHei Consolas Hybrid', monospace;
font-size: 20pt;
}
.CodeMirror pre, .output pre {
font-family: Monaco, monospace;
font-size: 20pt
}
"""
display(HTML('<style>{}</style>'.format(css)))

notebookのテーマは簡単に変えられるので、自分の使いやすいようにCSS、あるいは、jsを都度改変していけば、かなり使い勝手の良いthemeが出来上がるだろう。

参考サイトhttps://stackoverflow.com/
参考サイトhttps://github.com/

スポンサーリンク