Belle II Software  release-05-02-19
create_ipython_config.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 # Skript to create or edit your ipython config to use a port and not open a browser window
4 # when starting.
5 # Please ensure you have the newest jupyter notebook version installed (greater/equal than 4.0.0).
6 #
7 
8 from jinja2 import Template
9 import os
10 from subprocess import check_output
11 from ROOT import Belle2
12 
13 
14 def main():
15  '''
16  Main function of the script.
17  '''
18  print("Please fill in the options you want to use for the notebook server.")
19 
20  # Ask the user for a port
21  while True:
22  try:
23  port = int(input('Network Port (the recommendation is a number between 8000 to 9000): '))
24  except ValueError:
25  print("Please fill in a valid network port.")
26  continue
27  else:
28  break
29 
30  print("Will now write your notebook config.")
31 
32  jupyter_template_file = Belle2.FileSystem.findFile("framework/examples/ipython_tools/jupyter_notebook_config.py.j2")
33  with open(jupyter_template_file, 'r') as f:
34  template = Template(f.read())
35 
36  try:
37  jupyter_folder = check_output(['jupyter', '--config-dir']).decode().strip()
38  except OSError:
39  print('Failed to create config file. Have you a recent ipython-notebook installation?')
40  raise
41 
42  if not os.path.isdir(jupyter_folder):
43  try:
44  check_output(['jupyter', 'notebook', '--generate-config'])
45  except BaseException:
46  print("Could not start jupyter notebook. There are many possible reasons for this.\n"
47  "Please see https://confluence.desy.de/display/BI/Software+Jupyter+Notebooks for possible fixes \n"
48  "and feel free to contact software@belle2.org for questions.")
49 
50  config_file = template.render(port=port)
51  jupyter_config_file = os.path.join(jupyter_folder, 'jupyter_notebook_config.py')
52 
53  # Ask the user whether to override his config
54  if os.path.isfile(jupyter_config_file):
55  while True:
56  choice = input('You already have a jupyter config file. Do you want to replace it? [Y/n] ').lower()
57  if choice == "n":
58  print('Not writing config file.')
59  exit()
60  if choice == 'y' or choice == '':
61  print('Overwriting config file.')
62  break
63  else:
64  print('Not a valid answer.')
65  continue
66  else:
67  print('Writing config file.')
68 
69  with open(jupyter_config_file, 'w') as out:
70  out.write(config_file)
71 
72  # Set the correct read-write-user-only permissions
73  os.chmod(jupyter_config_file, 0o600)
74 
75 
76 if __name__ == '__main__':
77  main()
main
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:77
Belle2::FileSystem::findFile
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
Definition: FileSystem.cc:147