Coverage for src/jupytalk/talk_examples/treant_wrapper.py: 100%

8 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2023-01-26 01:11 +0100

1""" 

2@file 

3@brief Simple wrapper for `treant-js <http://fperucic.github.io/treant-js/>`_. 

4""" 

5 

6_template_html = """ 

7<style> 

8__STYLE__ 

9</style> 

10 

11<link rel="stylesheet" href="http://fperucic.github.io/treant-js/Treant.css"> 

12<script src="http://fperucic.github.io/treant-js/vendor/raphael.js"></script> 

13<script src="http://fperucic.github.io/treant-js/Treant.js"></script> 

14<div class="__CLASSCHART__" id="__DIVID__"></div> 

15""" 

16 

17_template_js = """ 

18var tree_structure__DIVID__ = { 

19 __CLASSCHART__: __JSONCHART__, 

20 nodeStructure: __JSONDATA__ 

21}; 

22new Treant( tree_structure__DIVID__ ); 

23""" 

24 

25 

26def display_treant(json_tree, json_data, css, classname): 

27 """ 

28 Display a chart using `treant-js <http://fperucic.github.io/treant-js/>`_. 

29 

30 @param json_tree json which describe global attributes of the tree 

31 @param json_data json which describe the tree structures 

32 @param css style 

33 @param classname class name associated to the section DIV which will receive the 

34 tree 

35 @return HTML object 

36 """ 

37 

38 global _template_html, _template_js # pylint: disable=W0603,W0602 

39 uid = "id_" + str(id(json_data)) 

40 

41 # this should be done with jinja2 or mako 

42 ht = _template_html.replace("__STYLE__", css) \ 

43 .replace("__CLASSCHART__", classname) \ 

44 .replace("__JSONCHART__", json_tree) \ 

45 .replace("__JSONDATA__", json_data) \ 

46 .replace("__DIVID__", uid) 

47 jv = _template_js.replace("__STYLE__", css) \ 

48 .replace("__CLASSCHART__", classname) \ 

49 .replace("__JSONCHART__", json_tree) \ 

50 .replace("__JSONDATA__", json_data) \ 

51 .replace("__DIVID__", uid) 

52 

53 from IPython.core.display import HTML 

54 return HTML(ht + "<script>" + jv + "</script>")