main.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. define(['base/js/namespace', 'base/js/events'], function (Jupyter, events) {
  2. // Template cells including markdown and imports
  3. var setUp = function () {
  4. Jupyter.notebook.insert_cell_at_index('markdown', 0)
  5. .set_text(`# Introduction
  6. State notebook purpose here`)
  7. Jupyter.notebook.insert_cell_at_index('markdown', 1).set_text(`### Imports
  8. Import libraries and write settings here.`)
  9. // Define imports and settings
  10. Jupyter.notebook.insert_cell_at_index('code', 2)
  11. .set_text(`# Data manipulation
  12. import pandas as pd
  13. import numpy as np
  14. # Options for pandas
  15. pd.options.display.max_columns = 50
  16. pd.options.display.max_rows = 30
  17. # Display all cell outputs
  18. from IPython.core.interactiveshell import InteractiveShell
  19. InteractiveShell.ast_node_interactivity = 'all'
  20. from IPython import get_ipython
  21. ipython = get_ipython()
  22. # autoreload extension
  23. if 'autoreload' not in ipython.extension_manager.loaded:
  24. %load_ext autoreload
  25. %autoreload 2
  26. # Visualizations
  27. import chart_studio.plotly as py
  28. import plotly.graph_objs as go
  29. from plotly.offline import iplot, init_notebook_mode
  30. init_notebook_mode(connected=True)
  31. import cufflinks as cf
  32. cf.go_offline(connected=True)
  33. cf.set_config_file(theme='white')`)
  34. Jupyter.notebook.insert_cell_at_index('markdown', 3)
  35. .set_text(`# Analysis/Modeling
  36. Do work here`)
  37. Jupyter.notebook.insert_cell_at_index('markdown', 4).set_text(`# Results
  38. Show graphs and stats here`)
  39. Jupyter.notebook.insert_cell_at_index('markdown', 5)
  40. .set_text(`# Conclusions and Next Steps
  41. Summarize findings here`)
  42. // Run all cells
  43. Jupyter.notebook.execute_all_cells()
  44. }
  45. // Prompts user to enter name for notebook
  46. var promptName = function () {
  47. // Open rename notebook box if 'Untitled' in name
  48. if (Jupyter.notebook.notebook_name.search('Untitled') != -1) {
  49. document.getElementsByClassName('filename')[0].click()
  50. }
  51. }
  52. // Run on start
  53. function load_ipython_extension () {
  54. // Add default cells for new notebook
  55. if (Jupyter.notebook.get_cells().length === 1) {
  56. setTimeout(setUp, 500)
  57. } else {
  58. promptName()
  59. }
  60. }
  61. // Run when cell is executed
  62. events.on('execute.CodeCell', function () {
  63. promptName()
  64. })
  65. // Run when notebook is saved
  66. events.on('before_save.Notebook', function () {
  67. promptName()
  68. })
  69. return {
  70. load_ipython_extension: load_ipython_extension
  71. }
  72. })