Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/python_nvd3-0.14.2-py3.8.egg/nvd3/ipynb.py: 14%

35 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-03 06:25 +0000

1''' 

2ipython compatability module for nvd3-python 

3This adds simple ipython compatibility to the nvd3-python package, without making any 

4major modifications to how the main package is structured. It utilizes the IPython 

5display-formatter functionality, as described at: 

6http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/notebooks/Custom%20Display%20Logic.ipynb 

7For additional examples, see: 

8https://github.com/sympy/sympy/blob/master/sympy/interactive/printing.py 

9''' 

10 

11try: 

12 _ip = get_ipython() 

13except: 

14 _ip = None 

15if _ip and _ip.__module__.lower().startswith('ipy'): 

16 global _js_initialized 

17 _js_initialized = False 

18 

19 def _print_html(chart): 

20 '''Function to return the HTML code for the div container plus the javascript 

21 to generate the chart. This function is bound to the ipython formatter so that 

22 charts are displayed inline.''' 

23 global _js_initialized 

24 if not _js_initialized: 

25 print('js not initialized - pausing to allow time for it to load...') 

26 initialize_javascript() 

27 import time 

28 time.sleep(5) 

29 chart.buildhtml() 

30 return chart.htmlcontent 

31 

32 def _setup_ipython_formatter(ip): 

33 ''' Set up the ipython formatter to display HTML formatted output inline''' 

34 from IPython import __version__ as IPython_version 

35 from nvd3 import __all__ as nvd3_all 

36 

37 if IPython_version >= '0.11': 

38 html_formatter = ip.display_formatter.formatters['text/html'] 

39 for chart_type in nvd3_all: 

40 html_formatter.for_type_by_name('nvd3.' + chart_type, chart_type, _print_html) 

41 

42 def initialize_javascript(d3_js_url='https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js', 

43 nvd3_js_url='https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.js', 

44 nvd3_css_url='https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.css', 

45 use_remote=False): 

46 '''Initialize the ipython notebook to be able to display nvd3 results. 

47 by instructing IPython to load the nvd3 JS and css files, and the d3 JS file. 

48 

49 by default, it looks for the files in your IPython Notebook working directory. 

50 

51 Takes the following options: 

52 

53 use_remote: use remote hosts for d3.js, nvd3.js, and nv.d3.css (default False) 

54 * Note: the following options are ignored if use_remote is False: 

55 nvd3_css_url: location of nvd3 css file (default https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.css) 

56 nvd3_js_url: location of nvd3 javascript file (default https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.css) 

57 d3_js_url: location of d3 javascript file (default https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js) 

58 ''' 

59 from IPython.display import display, Javascript, HTML 

60 

61 if not use_remote: 

62 # these file locations are for IPython 1.x, and will probably change when 2.x is released 

63 d3_js_url = 'files/d3.v3.js' 

64 nvd3_js_url = 'files/nv.d3.js' 

65 nvd3_css_url = 'files/nv.d3.css' 

66 

67 # load the required javascript files 

68 

69 #display(Javascript('''$.getScript("%s")''' %(d3_js_url))) 

70 display(HTML('''<link media="all" href="%s" type="text/css" 

71 rel="stylesheet"/>''' % (nvd3_css_url))) 

72 # The following two methods for loading the script file are redundant. 

73 # This is intentional. 

74 # Ipython's loading of javascript in version 1.x is a bit squirrely, especially 

75 # when creating demos to view in nbviewer. 

76 # by trying twice, in two different ways (one using jquery and one using plain old 

77 # HTML), we maximize our chances of successfully loading the script. 

78 display(Javascript('''$.getScript("%s")''' % (nvd3_js_url))) 

79 display(Javascript('''$.getScript("%s", function() { 

80 $.getScript("%s", function() {})});''' % (d3_js_url, nvd3_js_url))) 

81 display(HTML('<script src="%s"></script>' % (d3_js_url))) 

82 display(HTML('<script src="%s"></script>' % (nvd3_js_url))) 

83 

84 global _js_initialized 

85 _js_initialized = True 

86 

87 print('loaded nvd3 IPython extension\n' 

88 'run nvd3.ipynb.initialize_javascript() to set up the notebook\n' 

89 'help(nvd3.ipynb.initialize_javascript) for options') 

90 

91 _setup_ipython_formatter(_ip)