Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# (c) Stefan Countryman, 2019 

2 

3""" 

4Launch a Jupyter Notebook server. Specify the domain using environment variable 

5``LLAMA_DOMAIN`` (default: {DEFAULT_LLAMA_DOMAIN}) and the port as 

6``LLAMA_JUP_PORT`` (default: {DEFAULT_LLAMA_JUP_PORT}). 

7""" 

8 

9import os 

10import logging 

11from pathlib import Path 

12from plumbum.cmd import jupyter 

13from llama.utils import RemoteFileCacher, OUTPUT_DIR 

14from llama.classes import optional_env_var 

15from llama.serve.gui.domain import DEFAULT_LLAMA_DOMAIN 

16 

17DEFAULT_LLAMA_JUP_PORT = '8080' 

18LLAMA_JUP_PORT = optional_env_var( 

19 ['LLAMA_JUP_PORT'], 

20 """Specify the port on which ``llama serve jupyter`` is running.""", 

21 [DEFAULT_LLAMA_JUP_PORT], 

22)[0] 

23__doc__ = __doc__.format(DEFAULT_LLAMA_DOMAIN=DEFAULT_LLAMA_DOMAIN, 

24 DEFAULT_LLAMA_JUP_PORT=DEFAULT_LLAMA_JUP_PORT) 

25LOGGER = logging.getLogger(__name__) 

26README = RemoteFileCacher( 

27 "https://nyc3.digitaloceanspaces.com/llama/llama/objects/" 

28 "b56d3b4598940abd4ee603385bebe8db07065d87499aa4c3c6da728c5d93be87" 

29) 

30VIEW_LOGS = RemoteFileCacher( 

31 "https://nyc3.digitaloceanspaces.com/llama/llama/objects/" 

32 "2e25f0d555f6c0a8e708947822073f149cadbd71154f20c7a06a680725e1f529" 

33) 

34SERVER_LIST_FILE = os.path.join(OUTPUT_DIR, '.jup_servers') 

35 

36 

37def running_servers(): 

38 """Get a list of running servers as stored in ``SERVER_LIST_FILE``. Returns 

39 a list of tuples of the form ``(server_url, mounted_path)`` where 

40 ``server_url`` is the URL that can be used to access the server and 

41 ``mounted_path`` is the path on the server that the jupyter notebook has 

42 mounted. Returns ``None`` if ``SERVER_LIST_FILE`` does not exist. 

43 """ 

44 if not os.path.isfile(SERVER_LIST_FILE): 

45 return None 

46 with open(SERVER_LIST_FILE) as server_list: 

47 return [tuple(l.split(' :: ')) for l in server_list] 

48 

49 

50def load_notebook(remote, local, readonly=True, clobber=True): 

51 """Copy a distribution LLAMA JUPYTER notebook to the output directory 

52 (fetching it first from LLAMA S3 if necessary), set it as trusted, and set 

53 its permissions to readonly. Deletes the old notebook if present. 

54 

55 Parameters 

56 ---------- 

57 remote : RemoteFileCacher 

58 A ``RemoteFileCacher`` instance pointing to the file you want to load. 

59 local : str or Path 

60 Local path *relative* to the ``OUTPUT_DIR`` that you'd like to save the 

61 remote file as. 

62 readonly : bool, optional 

63 If ``True``, set the notebook to readonly mode so that user's can't 

64 accidentally break it. Set this to ``False`` to allow editing when 

65 making changes to the notebook notebook (and remember to commit those 

66 changes with ``llama dev upload``). 

67 clobber : bool, optional 

68 If ``True``, overwrite any existing notebook files. In production, you 

69 usually want this to be ``True`` since it will ensure you get the 

70 latest version of the documentation. While developing, you probably 

71 want this to be ``False`` so that you can keep incrementally modifying 

72 your notebook until you're ready to commit the changes to LLAMA S3. 

73 

74 Returns 

75 ------- 

76 local : pathlib.Path 

77 The path to the local Jupyter notebook. 

78 """ 

79 local = Path(OUTPUT_DIR)/local 

80 if local.exists(): 

81 if not clobber: 

82 LOGGER.info("Local copy of %s.ipynb exists and `clobber` is " 

83 "`False`; keeping old %s.", local, local) 

84 return local 

85 LOGGER.debug("Deleting old %s.ipynb", local) 

86 local.chmod(0o777) 

87 local.unlink() 

88 local.write_bytes(remote.get().read_bytes()) 

89 LOGGER.info("Wrote new %s.ipynb", local) 

90 res = jupyter['trust', str(local)].run() 

91 LOGGER.debug("Trusted new %s.ipynb. retval: %s stdout: %s stderr: %s", 

92 local, *res) 

93 if readonly: 

94 local.chmod(0o444) 

95 LOGGER.debug("Set %s.ipynb to readonly", local) 

96 else: 

97 local.chmod(0o777) 

98 LOGGER.debug("Set %s.ipynb to writeable", local) 

99 return local