JupyterLabで、初期ディレクトリと起動ブラウザを指定するだけのjupyter_lab_config.py
を作るPythonコードです。
import os
def generate_config():
user = os.environ.get("USER") # Ubuntuの環境変数
username = os.environ.get("USERNAME ") # Windowsの環境変数
if user: # Ubuntu
config_list = [
"c = get_config()",
f"c.ServerApp.notebook_dir = '/home/{user}/Documents/'",
"c.ServerApp.browser = '/usr/bin/google-chrome-stable --args --app=%s --start-maximized'",
]
config_file = f"/home/{user}/.jupyter/jupyter_lab_config.py"
elif username: # Windows
config_list = [
"c = get_config()",
f"c.ServerApp.notebook_dir = 'C:/Users/{username}/Documents'",
"c.ServerApp.browser = '\"C:/Program Files/Google/Chrome/Application/Chrome.exe\" --app=%s --start-maximized'",
]
config_file = f"C:/Users/{username}/.jupyter/jupyter_lab_config.py"
else:
return
with open(config_file, "w") as f:
f.write("\n".join(config_list))
with open(config_file, "r") as f:
print(f.read())
return
generate_config()