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""" 

4Utilities for working with LLAMA in Jupyter notebooks. 

5""" 

6 

7 

8def create_download_link(filehandler): 

9 """Create a Jupyter notebook compatible download link for the file 

10 contents of a filehandler. Only works in an IPython notebook. 

11 

12 Parameters 

13 ---------- 

14 filehandler : llama.filehandler.FileHandler 

15 The ``FileHandler`` whose data you want to download. 

16 

17 Raises 

18 ------ 

19 FileNotFoundError 

20 If the file to be downloaded does not exist. 

21 """ 

22 from IPython.display import HTML 

23 from mimetypes import guess_type 

24 import base64 

25 title = f"Download {filehandler.FILENAME} for {filehandler.eventid}" 

26 mime = guess_type(filehandler.FILENAME)[0] 

27 with filehandler.open('rb') as f: 

28 b64 = base64.b64encode(f.read()) 

29 payload = b64.decode() 

30 html = (f'<a download="{filehandler.FILENAME}" ' 

31 f'href="data:{mime};base64,{payload}" ' 

32 f'target="_blank">{title}</a>') 

33 return HTML(html)