ipynb.py 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. '''
  2. ipython compatability module for nvd3-python
  3. This adds simple ipython compatibility to the nvd3-python package, without making any
  4. major modifications to how the main package is structured. It utilizes the IPython
  5. display-formatter functionality, as described at:
  6. http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/notebooks/Custom%20Display%20Logic.ipynb
  7. For additional examples, see:
  8. https://github.com/sympy/sympy/blob/master/sympy/interactive/printing.py
  9. '''
  10. try:
  11. _ip = get_ipython()
  12. except:
  13. _ip = None
  14. if _ip and _ip.__module__.lower().startswith('ipy'):
  15. global _js_initialized
  16. _js_initialized = False
  17. def _print_html(chart):
  18. '''Function to return the HTML code for the div container plus the javascript
  19. to generate the chart. This function is bound to the ipython formatter so that
  20. charts are displayed inline.'''
  21. global _js_initialized
  22. if not _js_initialized:
  23. print('js not initialized - pausing to allow time for it to load...')
  24. initialize_javascript()
  25. import time
  26. time.sleep(5)
  27. chart.buildhtml()
  28. return chart.htmlcontent
  29. def _setup_ipython_formatter(ip):
  30. ''' Set up the ipython formatter to display HTML formatted output inline'''
  31. from IPython import __version__ as IPython_version
  32. from nvd3 import __all__ as nvd3_all
  33. if IPython_version >= '0.11':
  34. html_formatter = ip.display_formatter.formatters['text/html']
  35. for chart_type in nvd3_all:
  36. html_formatter.for_type_by_name('nvd3.' + chart_type, chart_type, _print_html)
  37. def initialize_javascript(d3_js_url='https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js',
  38. nvd3_js_url='https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.js',
  39. nvd3_css_url='https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.css',
  40. use_remote=False):
  41. '''Initialize the ipython notebook to be able to display nvd3 results.
  42. by instructing IPython to load the nvd3 JS and css files, and the d3 JS file.
  43. by default, it looks for the files in your IPython Notebook working directory.
  44. Takes the following options:
  45. use_remote: use remote hosts for d3.js, nvd3.js, and nv.d3.css (default False)
  46. * Note: the following options are ignored if use_remote is False:
  47. nvd3_css_url: location of nvd3 css file (default https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.css)
  48. nvd3_js_url: location of nvd3 javascript file (default https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.css)
  49. d3_js_url: location of d3 javascript file (default https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js)
  50. '''
  51. from IPython.display import display, Javascript, HTML
  52. if not use_remote:
  53. # these file locations are for IPython 1.x, and will probably change when 2.x is released
  54. d3_js_url = 'files/d3.v3.js'
  55. nvd3_js_url = 'files/nv.d3.js'
  56. nvd3_css_url = 'files/nv.d3.css'
  57. # load the required javascript files
  58. #display(Javascript('''$.getScript("%s")''' %(d3_js_url)))
  59. display(HTML('''<link media="all" href="%s" type="text/css"
  60. rel="stylesheet"/>''' % (nvd3_css_url)))
  61. # The following two methods for loading the script file are redundant.
  62. # This is intentional.
  63. # Ipython's loading of javascript in version 1.x is a bit squirrely, especially
  64. # when creating demos to view in nbviewer.
  65. # by trying twice, in two different ways (one using jquery and one using plain old
  66. # HTML), we maximize our chances of successfully loading the script.
  67. display(Javascript('''$.getScript("%s")''' % (nvd3_js_url)))
  68. display(Javascript('''$.getScript("%s", function() {
  69. $.getScript("%s", function() {})});''' % (d3_js_url, nvd3_js_url)))
  70. display(HTML('<script src="%s"></script>' % (d3_js_url)))
  71. display(HTML('<script src="%s"></script>' % (nvd3_js_url)))
  72. global _js_initialized
  73. _js_initialized = True
  74. print('loaded nvd3 IPython extension\n'
  75. 'run nvd3.ipynb.initialize_javascript() to set up the notebook\n'
  76. 'help(nvd3.ipynb.initialize_javascript) for options')
  77. _setup_ipython_formatter(_ip)