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