jupyter_notebook_config.py.jinja 1.05 KB
Newer Older
1 2 3 4
'''
  This script initializes Jupyter's configuration such as passwords and other
  things. It is run by IPython hence why it can use functions like get_config().
'''
Jérome Perrin's avatar
Jérome Perrin committed
5
import ConfigParser
6 7 8 9 10 11 12 13 14
import random
from notebook.auth import passwd
import os

def random_password(length = 10):
  result = ""
  for i in range(0, length):
    result = result + chr(random.randint(0, 25) + ord('a'))
  return result
Jérome Perrin's avatar
Jérome Perrin committed
15 16 17

knowledge_0 = '{{ config_cfg }}'

18 19 20 21 22
if not os.path.exists(knowledge_0):
  print "Your software does <b>not</b> embed 0-knowledge. \
  This interface is useless in this case</body></html>"
  exit(0)

Jérome Perrin's avatar
Jérome Perrin committed
23 24 25 26 27
c = get_config()

parser = ConfigParser.ConfigParser()
parser.read(knowledge_0)

28 29
if not parser.has_section("jupyter_notebook"):
  parser.add_section("jupyter_notebook")
30

31 32 33
if not parser.has_option("jupyter_notebook", "password") or \
  parser.get("jupyter_notebook", "password") == "":
  parser.set("jupyter_notebook", "password", random_password())
34

35
c.NotebookApp.password = passwd(parser.get("jupyter_notebook", "password"))
Jérome Perrin's avatar
Jérome Perrin committed
36

37
with open(knowledge_0, 'w') as file:
38
  parser.write(file)