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