thebelab.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <!-- Include Thebelab for interactive code if it's enabled -->
  2. {% if site.use_thebelab_button %}
  3. <!-- Display Thebelab button in each code cell -->
  4. {% include js/thebelab-cell-button.html %}
  5. <script type="text/x-thebe-config">
  6. {
  7. requestKernel: true,
  8. binderOptions: {
  9. repo: '{{ site.binder_repo_org }}/{{ site.binder_repo_name }}',
  10. ref: '{{ site.binder_repo_branch }}',
  11. },
  12. codeMirrorConfig: {
  13. theme: "{{ site.codemirror_theme }}"
  14. },
  15. kernelOptions: {
  16. name: '{% if page.kernel_name %}{{ page.kernel_name }}{% else %}python3{% endif %}',
  17. }
  18. }
  19. </script>
  20. <script src="https://unpkg.com/thebelab@0.4.0/lib/index.js" async></script>
  21. <script>
  22. /**
  23. * Add attributes to Thebelab blocks
  24. */
  25. const initThebelab = () => {
  26. const addThebelabToCodeCells = () => {
  27. console.log("Adding thebelab to code cells...");
  28. // If Thebelab hasn't loaded, wait a bit and try again. This
  29. // happens because we load ClipboardJS asynchronously.
  30. if (window.thebelab === undefined) {
  31. setTimeout(addThebelabToCodeCells, 250)
  32. return
  33. }
  34. // If we already detect a Thebelab cell, don't re-run
  35. if (document.querySelectorAll('div.thebelab-cell').length > 0) {
  36. return;
  37. }
  38. // Find all code cells, replace with Thebelab interactive code cells
  39. const codeCells = document.querySelectorAll('.input_area pre')
  40. codeCells.forEach((codeCell, index) => {
  41. const id = codeCellId(index)
  42. codeCell.setAttribute('data-executable', 'true')
  43. // Figure out the language it uses and add this too
  44. var parentDiv = codeCell.parentElement.parentElement;
  45. var arrayLength = parentDiv.classList.length;
  46. for (var ii = 0; ii < arrayLength; ii++) {
  47. var parts = parentDiv.classList[ii].split('language-');
  48. if (parts.length === 2) {
  49. // If found, assign dataLanguage and break the loop
  50. var dataLanguage = parts[1];
  51. break;
  52. }
  53. }
  54. codeCell.setAttribute('data-language', dataLanguage)
  55. // If the code cell is hidden, show it
  56. var inputCheckbox = document.querySelector(`input#hidebtn${codeCell.id}`);
  57. if (inputCheckbox !== null) {
  58. setCodeCellVisibility(inputCheckbox, 'visible');
  59. }
  60. });
  61. // Remove the event listener from the page so keyboard press doesn't
  62. // Change page
  63. document.removeEventListener('keydown', initPageNav)
  64. keyboardListener = false;
  65. // Init thebelab
  66. thebelab.bootstrap();
  67. // Remove copy buttons since they won't work anymore
  68. const copyAndThebeButtons = document.querySelectorAll('.copybtn, .thebebtn')
  69. copyAndThebeButtons.forEach((button, index) => {
  70. button.remove();
  71. });
  72. // Remove outputs since they'll be stale
  73. const outputs = document.querySelectorAll('.output *, .output')
  74. outputs.forEach((output, index) => {
  75. output.remove();
  76. });
  77. // Find any cells with an initialization tag and ask ThebeLab to run them when ready
  78. var thebeInitCells = document.querySelectorAll('div.tag_thebelab-init');
  79. thebeInitCells.forEach((cell) => {
  80. console.log("Initializing ThebeLab with cell: " + cell.id);
  81. cell.querySelector('.thebelab-run-button').click();
  82. });
  83. }
  84. // Add event listener for the function to modify code cells
  85. const thebelabButtons = document.querySelectorAll('[id^=thebelab], [id$=thebelab]')
  86. thebelabButtons.forEach((thebelabButton,index) => {
  87. if (thebelabButton === null) {
  88. setTimeout(initThebelab, 250)
  89. return
  90. };
  91. thebelabButton.addEventListener('click', addThebelabToCodeCells);
  92. });
  93. }
  94. // Initialize Thebelab
  95. initFunction(initThebelab);
  96. </script>
  97. {% endif %}