Preguntas frecuentes en Jupyter Notebook

Tutorial | Jupyter Notebook
tutoriales | jupyter-notebook

¿Cómo instalar Jupyter Notebook?

Usando pip podemos instalar con el siguiente comando:

pip install notebook

Usando Conda:

conda install -c conda-forge notebook

[204]

¿Cómo instalar Jupyter Notebook desde su código fuente de Github?

[203]

¿Cómo agregar funcionalidad a Jupyter Notebook?

[202]

¿Cómo agregar una extensión?

Se puede agregar una extensión a nivel del servidor [208,209,210,211] y a nivel del front [201,301,303].

Configuración local

Para el Servidor (asumiendo que el usuario es ubuntu)se modifican el archivo ~/.jupyter/jupyter_notebook_config.py, agregando lo siguiente

c = get_config()
c.NotebookApp.nbserver_extensions = {
    'mi_paquete.mi_modulo': True,
}

donde mi_paquete.mi_modulo debe ser un módulo que debe estar instaldo, por ejemplo usando pip. También podemos indicar la ruta de nuestro módulo local, por ejemplo creamos el archivo ~/jupyter_notebook_editar/server/mi_paquete/mi_modulo.py con el siguiente contenido:

from notebook.utils import url_path_join
from notebook.base.handlers import IPythonHandler

class HelloWorldHandler(IPythonHandler):
    def get(self):
        self.write({'message':'Hola'})

def load_jupyter_server_extension(nb_server_app):
    """
    Se llama cuando la extension es cargado.

    Args:
        nb_server_app (NotebookWebApplication): handle to the Notebook webserver instance.
    """
    web_app = nb_server_app.web_app
    host_pattern = '.*$'
    route_pattern = url_path_join(web_app.settings['base_url'], '/hola')
    web_app.add_handlers(host_pattern, [(route_pattern, HelloWorldHandler)])

Este módulo exponer una api, con endpoint /hola.

Para que se pueda cargar el módulo al iniciar jupyter notebook, debemos agregar sys.path.append('/home/ubuntu/jupyter_notebook_editar/server') al archivo ~/.jupyter/jupyter_notebook_config.py de la siguiente manera:

import sys
sys.path.append('/home/ubuntu/jupyter_notebook_editar/server') #corregir con su usuario
c = get_config()
c.NotebookApp.nbserver_extensions = {
    'mi_paquete.mi_modulo': True,
}

Con todos estos cambios reiniciamos jupyter

$ jupyter notebook

Para el front creamos el archivo ~/jupyter_notebook_editar/front/mi_extension/main.js y agregamos:

define([
    'base/js/namespace' //con esto aparece el icono
], function(
    Jupyter
) {
    function load_ipython_extension() {
        var handler = async function () {
            alert("hola");
        };

        var action = {
            icon: 'fa-comment-o',
            help    : 'Show an alert',
            help_index : 'zz',
            handler : handler
        };
        var prefix = 'my_extension';
        var action_name = 'show-alert';

        var full_action_name = Jupyter.actions.register(action, action_name, prefix); // returns 'my_extension:show-alert'
        Jupyter.toolbar.add_buttons_group([full_action_name]);
    }

    return {
        load_ipython_extension: load_ipython_extension
    };
});

Luego instalamos nuestra extensión usamos el siguiente comando:

$ jupyter nbextension install /home/ubuntu/jupyter_notebook_editar/front/mi_extension --user

Con eso se copiar nuestro archivo a ~/.local/share/jupyter/nbextensions/mi_extension/main.js

Y habilitamos la extensión con

$ jupyter nbextension enable mi_extension --user

Para deshabilitar hacemos

$ jupyter nbextension disable mi_extension/main --user

[202,212]

¿Dónde encontrar los archivos a modificar para agregar una extensión?

[207]

¿Cómo ver la versión de Jupyter Notebook?

Usando el comando

jupyter --version

[205]

¿Cómo contribuir en el desarrollo de Jupyter Notebook?

[206]

¿Obtener la ruta actual desde el front?

Jupyter.notebook.notebook_path

¿Cómo mostrar un gif en Jupyter Notebook?

Podemos usar agregar con el siguiente codigo

![ChessUrl](https://upload.wikimedia.org/wikipedia/commons/7/71/ChessPawnSpecialMoves.gif "chess")

ChessUrl

[213, 214]

Referencias

tutoriales | jupyter-notebook
Tutorial | Jupyter Notebook