Coverage for src/lcdoc/mkdocs/lp/plugs/python/pyplugs/data_table/__init__.py: 71.67%

Shortcuts 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

40 statements  

1import os lp|index.md

2import shutil lp|index.md

3 

4from lcdoc.mkdocs.lp.plugs import python lp|index.md

5from lcdoc.mkdocs.tools import app, style, script lp|index.md

6from lcdoc.tools import read_file lp|index.md

7 

8config, page, Session = (python.config, python.page, python.Session) lp|index.md

9 

10 

11def match(s, inner_kw): lp|index.md

12 """We want to be the default for listed data - no requirement for a typ kwarg""" 

13 if not s or not isinstance(s, list): lp|features/lp/plugs/lightbox/index.mdlp|features/lp/python/_tech.mdlp|features/lp/syntax.md

14 return lp|features/lp/python/_tech.mdlp|features/lp/syntax.md

15 c = inner_kw.get('columns') lp|features/lp/plugs/lightbox/index.md

16 if c: 16 ↛ 20line 16 didn't jump to line 20, because the condition on line 16 was never falselp|features/lp/plugs/lightbox/index.md

17 if len(c) == len(s[0]): 17 ↛ exitline 17 didn't return from function 'match', because the condition on line 17 was never falselp|features/lp/plugs/lightbox/index.md

18 return True lp|features/lp/plugs/lightbox/index.md

19 else: 

20 return is_dict_list(s) 

21 

22 

23def is_dict_list(s): lp|index.md

24 return isinstance(s[0], dict) and isinstance(s[-1], dict) lp|features/lp/plugs/lightbox/index.md

25 

26 

27def register(fmts): lp|index.md

28 """registering us as renderer for show(<pyplot module>) within lp python""" 

29 fmts[match] = make_data_table lp|index.md

30 

31 

32# https://datatables.net/manual/tech-notes/3 (=> retrieve: true) 

33js = ''' 

34function do_%(id)s () { 

35 let data = %(data)s 

36 $('#%(id)s_datatbl').DataTable( { data: data, columns: %(columns)s, retrieve: true } ); 

37} 

38do_%(id)s() 

39''' 

40 

41here = os.path.dirname(os.path.abspath(__file__)) lp|index.md

42dflt_style = read_file(here + '/assets/jquery.dataTables.css') lp|index.md

43 

44 

45def make_data_table(s, **inner_kw): lp|index.md

46 columns = inner_kw.get('columns', ()) lp|features/lp/plugs/lightbox/index.md

47 if is_dict_list(s): 47 ↛ 48line 47 didn't jump to line 48, because the condition on line 47 was never truelp|features/lp/plugs/lightbox/index.md

48 if not columns: 

49 columns = list(s[0].keys()) 

50 if not isinstance(columns[0], dict): 

51 columns = [{'data': i, 'title': i} for i in columns] 

52 else: 

53 if not isinstance(columns[0], dict): 53 ↛ 55line 53 didn't jump to line 55, because the condition on line 53 was never falselp|features/lp/plugs/lightbox/index.md

54 columns = [{'title': i} for i in columns] lp|features/lp/plugs/lightbox/index.md

55 kw = python.Session.kw lp|features/lp/plugs/lightbox/index.md

56 # A = Session.cur['assets'].setdefault('page_assets', {}).setdefault('datatables', {}) 

57 A = {'mode': ['jquery', 'jquery_datatables']} lp|features/lp/plugs/lightbox/index.md

58 # user can disable by setting to empty - and provide his own via mkdocs 

59 fn_style = kw.get('fnstyle') lp|features/lp/plugs/lightbox/index.md

60 if not fn_style: 60 ↛ 63line 60 didn't jump to line 63, because the condition on line 60 was never falselp|features/lp/plugs/lightbox/index.md

61 styl = dflt_style lp|features/lp/plugs/lightbox/index.md

62 else: 

63 styl = read_file(here + '/assets/' + fn_style) 

64 A['header'] = style(styl) lp|features/lp/plugs/lightbox/index.md

65 kw['data'] = s lp|features/lp/plugs/lightbox/index.md

66 kw['columns'] = columns lp|features/lp/plugs/lightbox/index.md

67 # Session.cur['assets']['footer'] = script(js % kw) # the per block js 

68 return { 

69 'res': '<table id="%(id)s_datatbl" class="display" width="100%%"></table>' % kw, 

70 'footer': script(js % kw), 

71 'page_assets': {'datatables': A}, 

72 }