Skip to content

This is a JavaScript header for HTML exported Jupyter Notebooks to hide/show code cells.

License

Notifications You must be signed in to change notification settings

szolgyen/Code-Toggle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hide code cells of Notebooks

Scope: Exported Jupyter Notebooks in HTML format
Function: Hide/show input code cells

This JavaScript code snippet adds an active button to an HTML document which is exported from a Jupyter Notebook. It allows for hiding and showing the content of all code cells of the whole notebook.

Copy this code to a markdown cell in the top of your Jupyter Notebook.

<script>
    function toggle_all() {
        var codeCells = document.querySelectorAll('.jp-InputArea-editor');
        var firstCell = codeCells[0].parentNode;
        var anyCellVisible = firstCell.style.visibility === 'visible';

        for (var i = 0; i < codeCells.length; i++) {
            var cell = codeCells[i].parentNode;
            if (anyCellVisible) {
                cell.style.visibility = 'hidden';
                cell.style.position = 'absolute';
            } else {
                cell.style.visibility = 'visible';
                cell.style.position = '';
            }
        }

        var button = document.getElementById('toggleButton');
        button.innerHTML = anyCellVisible ? 'Show Code' : 'Hide Code';
    }

    function refresh() {
        var button = document.createElement('button');
        button.innerHTML = 'Show Code';
        button.id = 'toggleButton';
        button.onclick = function() {
            toggle_all();
            return false;
        };

        // Insert the button at the beginning of the notebook
        var notebook = document.querySelector('.jp-Notebook');
        notebook.insertBefore(button, notebook.firstChild);

        // Initially hide all code cells
        toggle_all();
    }

    // Call refresh when the document is loaded
    document.addEventListener("DOMContentLoaded", function() {
        refresh();
    });
</script>

About

This is a JavaScript header for HTML exported Jupyter Notebooks to hide/show code cells.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published