1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| from notebook.auth import passwd import os
home_path = os.path.expanduser('~') cfg_path = f"{home_path}/.jupyter/jupyter_notebook_config.py" if not os.path.exists(cfg_path): os.system("jupyter notebook --generate-config") key = passwd() port = input("port(default:8888): ") or "8888" with open(cfg_path, "r") as f: data = f.readlines() modify = { "c.NotebookApp.ip": "c.NotebookApp.ip= '*'\n", "c.NotebookApp.password": f"c.NotebookApp.password = '{key}'\n", "c.NotebookApp.open_browser": "c.NotebookApp.open_browser = False\n", "c.NotebookApp.port": f"c.NotebookApp.port = {port}\n" } for i, j in enumerate(data): for k in list(modify.keys()): if k in j: data[i] = modify[k] modify.pop(k) with open(cfg_path, "w") as f: f.writelines(data)
|