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# -*- coding: utf-8 -*- 

2""" 

3@file 

4@brief Renders a graph in Javascript. 

5""" 

6import os 

7from .render_nb_js import RenderJS 

8 

9 

10class RenderJsDot(RenderJS): 

11 """ 

12 Renders a graph in a :epkg:`notebook` 

13 defined in :epkg:`DOT` language 

14 with :epkg:`viz.js`. On `binder 

15 <https://mybinder.org/v2/gh/sdpython/jyquickhelper/ 

16 master?filepath=_doc%2Fnotebooks>`_, 

17 argument *local* should be set to True to be working. 

18 """ 

19 

20 def __init__(self, dot, local=False, width="100%", height="100%", divid=None, 

21 style=None, only_html=True, div_class=None, check_urls=True, 

22 lite=False): 

23 """ 

24 @param dot (str) dot 

25 @param local (bool) use local path to javascript dependencies 

26 @param script (str) script 

27 @param width (str) width 

28 @param height (str) height 

29 @param style (str) style (added in ``<style>...</style>``) 

30 @param divid (str|None) id of the div 

31 @param only_html (bool) use only function `display_html <http://ipython.readthedocs.io/en/stable/ 

32 api/generated/IPython.display.html? 

33 highlight=display_html#IPython.display.display_html>`_ 

34 and not `display_javascript <http://ipython.readthedocs.io/en/stable/api/generated/ 

35 IPython.display.html?highlight=display_html#IPython.display.display_javascript>`_ to add 

36 javascript to the page. 

37 @param div_class (str) class of the section ``div`` which will host the results 

38 of the javascript 

39 @param check_urls (bool) by default, check url exists 

40 @param lite (bool) use lite version 

41 (no `neato <http://www.graphviz.org/pdf/neatoguide.pdf>`_) 

42 """ 

43 script = RenderJsDot._build_script(dot) 

44 libs, css = RenderJsDot._get_libs_css(local, lite) 

45 RenderJS.__init__(self, script, width=width, height=height, divid=divid, 

46 only_html=only_html, div_class=div_class, check_urls=True, 

47 libs=libs, css=css, local=local) 

48 

49 @staticmethod 

50 def _get_libs_css(local, lite): 

51 """ 

52 Returns the dependencies. 

53 

54 @param local use local file (True) or remote urls (False) 

55 @param lite use lite version 

56 @return tuple *(libs, css)* 

57 """ 

58 jsvers = "viz-lite.js" if lite else "viz.js" 

59 if local: 

60 this = os.path.dirname(__file__) 

61 js = os.path.join(this, '..', 'js', 'vizjs', jsvers) 

62 libs = [js] 

63 else: 

64 libs = ['http://www.xavierdupre.fr/js/vizjs/' + jsvers] 

65 css = None 

66 return libs, css 

67 

68 @staticmethod 

69 def _build_script(dot): 

70 """ 

71 Builds the javascript script based wrapping the 

72 :epkg:`DOT` language. 

73 

74 @param dot :epkg:`DOT` language 

75 @return javascript 

76 """ 

77 dot = dot.replace('"', '\\"').replace('\n', '\\n') 

78 script = """var svgGraph = Viz("{0}");\ndocument.getElementById('__ID__').innerHTML = svgGraph;""".format( 

79 dot) 

80 return script