jupyter notebook

Table of Contents

1. jupyter notebook

1.3. Issue de Github Mejoras de Developer Experience para Jupyter

1.5. Trucos

1.5.1. Saltarse ejecución de una celda

%%script false --no-raise-error

https://stackoverflow.com/questions/19309287/how-to-intermittently-skip-certain-cells-when-running-ipython-notebook
As a heads up, this gets rid of existing output. Not useful if you want to keep previous output of the cell.
En Jupytext te lo exporta con comentarios

1.5.3. Logging in Jupyter Notebooks

import logging
import sys

logging.basicConfig(format='%(asctime)s | %(levelname)s : %(message)s', level=logging.INFO, stream=sys.stdout)

logging.info('Hello world!')

1.5.4. Jupyter Notebook Remoto

1.6. Jupyterlab

1.6.1. Plugins que voy utilizando

1.6.1.1. Plotly

Instalar fuera del entorno virtual

jupyter labextension install @jupyter-widgets/jupyterlab-manager --no-build
jupyter labextension install plotlywidget --no-build
jupyter labextension install jupyterlab-plotly --no-build
jupyter labextension install jupyterlab-chart-editor --no-build
pip install jupyter-dash

https://medium.com/plotly/introducing-jupyterdash-811f1f57c02e

# app = dash.Dash(__name__)
app = JupyterDash(__name__)
app.run_server(mode='inline')
# app.run_server(debug=True)
  1. https://stackoverflow.com/questions/63460213/plotly-how-to-define-colors-in-a-figure-using-plotly-graph-objects-and-plotly-e → colores por defecto en plotly

    px.colors.qualitative.Plotly

1.6.1.2. Insertar imagen de plotly en jupyter notebook
  • Hay que instalarse orca de plotly
  • sudo npm install -g electron@6.1.4 orca --unsafe-perm=true --allow-root
import plotly.express as px
from IPython.display import Image
fig = px.line([1, 2, 3], [1, 1, 1])
Image(fig.to_image())
1.6.1.3. Jupyterlab Vim
  1. QoL improvements jupyter vim   someday_20230330
1.6.1.4. Temas de JupyterLab

https://www.npmjs.com/package/@hokyjack/jupyterlab-monokai-plus
jupyter labextension install @hokyjack/jupyterlab-monokai-plus

1.6.1.5. https://github.com/jtpio/jupyterlab-system-monitor

Para poner la RAM, útil

1.6.2. Instalar cosas en JupyterLab vs fuera de él

La norma general es que tengo que instalar todas las dependencias en el entorno virtual, y Jupyter sólamente en el entorno global
Sin embargo para ciertas cosas como estas, tienes que instalarlo también en el entorno global, porque utiliza ese por defecto
Otra alternativa es lanzarla desde el propio entorno virtual

1.6.2.1. Cuando cargas un pickle, utiliza la versión de fuera de jupyter, no la de dentro
ipympl
nbformat
scikit-learn # Cuando cargas un pickle, utiliza la versión de fuera de jupyter, no la de dentro
import sklearn; print(sklearn.__path__); print(sklearn.__version__)

1.6.3. Plugins para probar en jupyter lab   try someday_20230330

1.6.3.1. https://www.reddit.com/r/Python/comments/ixbibn/black_for_jupyter_notebook_and_jupyterlab/

jupyterblack :: Black format existing *.ipynb files using a CLI. This is useful if you’d like to tidy up existing Jupyter Notebooks or JupyterLab files.

  $ jblack my_notebook.ipynb

blackcellmagic :: Black format code cells using magic. In my opinion, this is useful if you want to tidy up specific code cells. Simply load the Jupyter Notebook extension %load_ext blackcellmagic and type %%black at the top of the code cell you want formatted. Execute cell and you have formatted code.
nb_black :: Automatically Black format every code cell when executed. This is great if you are starting a new .ipynb file. In Jupyter Notebook, load the extension using %load_ext nb_black, and every code cell executed thereafter will format itself.
I prefer jupyterlab-code-formatter. It adds formatting options under the Edit menu or can be used with shortcuts. Also easily configurable.

1.6.4. Debugger en JupyterLab

Por defecto si pones un breakpoint() no te va a funcionar, pero esto sí
import sys; sys.__breakpointhook__()
También otra cosa que puedes hacer es utilizar el debugger de JupyterLab
https://github.com/jupyterlab/jupyterlab/issues/10996

1.6.5. GitHub - jupyterlab/jupyterlab_app: A desktop application for JupyterLab, based on Electron.

https://github.com/jupyterlab/jupyterlab_app#download
App de escritorio de JupyterLab basada en Electron

1.6.6. 5 Hidden Features in JupyterLab You Should Start Using

https://link.medium.com/WnHABjepEbb

  • Window management
    1*pkrzwLqlrlqlx-NVjQJSFw.gif
  • Open the same notebook side by side (tab Left click> New view for notebook)
    1*OYohCjEk7GW6TJQbQC28iA.gif
  • Workspaces

      cat ~/.jupyter/lab/workspaces/* | jq -r '.metadata.id'
    

1.6.7. Jupyter using docker

1.6.7.4. jupyter notebook docker

https://hub.docker.com/r/jupyter/datascience-notebook
Para abrir jupyter notebooks solo lectura en modo blanco
Tengo que encontrar una manera de hacer los entornos virtuales persistentes + las dependencias de apt que instale
Quizás mejor simplemente hacer un dockerfile con esta imagen como base

  1. Jupyter y entornos virtuales con docker

    Puedo montar los entornos virtuales como un volumen de docker

    1. Para que cuando ejecuto un jupyter en docker me guarde las librerías en local y tenga persistencia. Esto estará en /usr/local/lib/python3.9/dist-packages o donde sea
    2. Lo que tiene sentido es que sea el mismo sitio que zpy
    3. Para poder utilizar un entorno virtual dentro de un docker para que vscode o lo que sea me autocomplete
    4. Para poder utilizar kernels de entornos virtuales que tengo en la máquina local en el docker

1.6.8. Jupytext

Jupytext - Jupyter Notebooks as Markdown Documents, Julia, Python or R Scripts — Jupytext documentation
Empareja distintos tipos de archivos (ipynb con md, ipynb con python con/sin celdas de markdown)
In JupyterLab, Jupytext adds a set of commands to the command palette (View / Activate Command Palette, or Ctrl+Shift+C):

1.6.8.1. Installation — Jupytext documentation
pip install jupytext --upgrade
conda install jupytext -c conda-forge
# Esos dos comandos instalan únicamente un comando para mantenerlos sincronizados
# Con eso sólamente ya puedes poner un pre-commit para que te los sincronize (pej si no quieres trabajar con notebooks)
jupyter serverextension enable jupytext # Para que sincronize los archivos
jupyter nbextension install jupytext --user --py  # Para que funcione en Jupyterlab
jupyter nbextension enable jupytext --user --py
jupyter labextension install jupyterlab-jupytext
jupyter labextension enable jupyterlab-jupytext

If you want to use Jupytext within Jupyter Notebook or JupyterLab, make sure you install Jupytext in the Python environment where the Jupyter server runs

No pilla bien jupytext si te lo instalas en local, así que me ha tocado hacer el prohibidísimo sudo pip install 😱

1.6.8.3. Encontrar una manera de instalar jupytext sin sudo pip   someday_20230330
  1. Instalar todo
    pip install --user jupytext
    jupyter serverextension install jupytext
    jupyter serverextension enable jupytext
    jupyter nbextension install jupytext --user --py
    jupyter nbextension enable jupytext --user --py
    jupyter labextension install jupyterlab-jupytext
    jupyter labextension enable jupyterlab-jupytext
    
  2. Desinstalar todo
    jupyter serverextension disable jupytext
    jupyter serverextension uninstall jupytext
    jupyter nbextension disable jupytext --user --py
    jupyter nbextension uninstall jupytext --user --py
    jupyter labextension disable jupyterlab-jupytext
    jupyter labextension uninstall jupyterlab-jupytext
    pip install --user jupytext
    

1.6.10. Error de Jupyter Lab al iniciar

!Parece que es simplemente cuestión de que tienes que salir del navegador!
Al menos cuando no te da un error de Python como el de abajo pero te sigue sin funcionar

[E 2022-03-16 16:02:07.749 ServerApp] Uncaught exception GET /lab/api/settings?1647442927412 (127.0.0.1)
    HTTPServerRequest(protocol='http', host='localhost:8889', method='GET', uri='/lab/api/settings?1647442927412', version='HTTP/1.1', remote_ip='127.0.0.1')
    Traceback (most recent call last):
      File "/home/julian/.local/lib/python3.9/site-packages/tornado/web.py", line 1704, in _execute
        result = await result
    tornado.iostream.StreamClosedError: Stream is closed

Quizás tiene sentido ponerle una configuración para que lance un navegador específico, distinto del que utilizo para navegar, que ocupe poca RAM, optimizado…

https://github.com/jupyter/notebook/issues/5814
Hice esto y lo reinstalé con pip en $HOME:

sudo apt remove python3-jupyter-core python3-nbformat
# Esto me desinstala esto: python3-jupyter-core python3-nbformat python3-plotly python3-qgis qgis qgis-plugin-grass
# Tiene dependencias con qgis
# qgis → python3-qgis → python3-plotly → python3-nbformat → python3-jupyter-core
pip3 uninstall -y jupyter jupyter_core jupyter-client jupyter-console notebook qtconsole nbconvert nbformat
pip3 install jupyter jupyter_core jupyter-client jupyter-console notebook qtconsole nbconvert nbformat

Esto también puede que tenga que ver:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
quilt3 3.5.0 requires jsonschema==3.*, but you have jsonschema 4.4.0 which is incompatible.

Con pip install 'jsonschema==3.*' se soluciona

1.6.10.1. Warnings

Esto es un warning pero también da mucho

Config option `template_path` not recognized by

https://github.com/ipython-contrib/jupyter_contrib_nbextensions/issues/1529 → es por una versión desactualizada, explica cómo instalar las nuevas

1.6.11. Maneras de instalar extensiones

https://stackoverflow.com/questions/67390716/unable-to-install-jupyterlab-execute-time-extension

Extension authors targeting the JupyterLab 3.0+ can choose to either:
1. continue publishing on NPM and ignore the new federated extensions mechanism, or
2. adapt federated extensions and start publishing to PyPI/conda-forge only, or
3. publish to both NPM and PyPI/conda-forge
In case of =jupyterlab-execute-time= the author went the (2) route, only publishing to PyPI/conda-forge, with the latest published release as for the day of writing being 2.0.2 on PyPI;

1.6.12. Extensiones contrib

1.6.13. Me funciona sólo bien del todo en chrome/chromium

1.6.14. PyQt6 con matplotlib

# Importar la librería antes de poner %matplotlib qt
import PyQt6
# Importar esta librería, si no no puede importarlo
import PyQt6.QtCore

%matplotlib qt

1.7. Gestión de entornos virtuales y kernels en jupyter

https://anbasile.github.io/programming/2017/06/25/jupyter-venv/
Se instala todo lo que tenga que ver con jupyter en el entorno base (conda) o en ~/.local (Linux) y se llama siempre jupyter desde ahí
Se crea un kernel por cada entorno virtual que necesites, y el kernel no tiene por qué llamarse igual que el entorno virtual
Para crear un kernel, activamos el entorno virtual y hacemos:

pip install tornado ipykernel
ipython kernel install --user --name=kernelname
# Para borrarlo
jupyter kernelspec uninstall kernelname
# Para listar los kernels
jupyter kernelspec list
# Listar los kernels con info de cada uno
jupyter kernelspec list | awk -n 'NR >= 2 {print $2}' | xargs -I _ /bin/bash -c "ls _/kernel.json && cat _/kernel.json && echo"

1.7.1. Jupyter venv / kernels Windows

En Windows, parece que tienes que instalar Jupyter Lab en el entorno virtual, lanzarlo desde allí y generar un kernel a la vez, porque si lo ejecutas desde el entorno base te falla la importación por ejemplo de numpy porque no encuentra las librerías binarias
https://docs.anaconda.com/anaconda/user-guide/tasks/integration/python-path/ → Para pillar el PATH de Python

source ~/Anaconda/Scripts/activate
source ~/anaconda3/Scripts/activate
# Para convertir los \ de Windows en / de shell
jupyter kernelspec list | sed 's%\\%/%g' | awk -n 'NR >= 2 {print $2}' | xargs -I _ /bin/bash -c "ls _/kernel.json && cat _/kernel.json"

1.8. Alternativas Jupyter notebook

1.8.2. Marimo

https://github.com/marimo-team/marimo
https://news.ycombinator.com/item?id=39552882

1.8.3. Quarto

  • github - Quarto
  • quarto render Untitled.qmd --to org para renderizar a org mode
    Hay que ponerle esto para que no tarde mucho

      org:
        execute:
          eval: false
    

1.8.7. Polynote

“What You Need to Know About Netflix’s ‘Jupyter Killer’: Polynote 📖” por
Michael Li https://link.medium.com/lop9sIcfN8 “Netflix’s Polynote is a New Open
Source Framework to Build Better Data Science Notebooks” por Jesus Rodriguez
https://link.medium.com/BAoJIl2iV8

1.8.8. Deepnote - Data science notebook for teams

1.8.10. Jupyter Launches JupyterLite, A Lightweight Environment For Web Pages That Runs On Any Device With Just A Browser

1.8.12. The KDE Education Project - Cantor

https://edu.kde.org/cantor/ Soporta muchos backends, es más como Sagemath

1.9. Jupyter Notebook

Jupyter Notebook está deprecado como tal

1.9.1. Atajos de teclado estandar de Jupyter Notebook

https://towardsdatascience.com/jypyter-notebook-shortcuts-bf0101a98330

  • Shortcuts in both modes:
    • Shift + Enter run the current cell, select below
    • Ctrl + Enter run selected cells
    • Alt + Enter run the current cell, insert below
    • Ctrl + S save and checkpoint
  • While in command mode (press Esc to activate):
    • Enter take you into edit mode
    • H show all shortcuts
    • Up select cell above
    • Down select cell below
    • Shift + Up extend selected cells above
    • Shift + Down extend selected cells below
    • A insert cell above
    • B insert cell below
    • X cut selected cells
    • C copy selected cells
    • V paste cells below
    • Shift + V paste cells above
    • D, D (press the key twice) delete selected cells
    • Z undo cell deletion
    • S Save and Checkpoint
    • Y change the cell type to Code
    • M change the cell type to Markdown
    • P open the command palette.
    • This dialog helps you run any command by name. It’s really useful if you don’t know some shortcut or when you don’t have a shortcut for the wanted command.
  • Command Palette
    • Shift + Space scroll notebook up
    • Space scroll notebook down
  • While in edit mode (pressEnter to activate)
    • Esc take you into command mode
    • Tab code completion or indent
    • Shift + Tab tooltip
    • Ctrl + ] indent
    • Ctrl + [ dedent
    • Ctrl + A select all
    • Ctrl + Z undo
    • Ctrl + Shift + Z or Ctrl + Y redo
    • Ctrl + Home go to cell start
    • Ctrl + End go to cell end
    • Ctrl + Left go one word left
    • Ctrl + Right go one word right
    • Ctrl + Shift + P open the command palette
    • Down move cursor down
    • Up move cursor up

1.9.2. Añadir atajo de teclado JSON

{
             "command": "runmenu:restart-and-run-all",
             "keys": [
             "Alt R"
             ],
             "selector": "[data-jp-code-runner]",
             "title": "Restart Kernel and Run All",
             "category": "Run Menu"
        }

1.9.3. Temas de Jupyter Notebook

https://github.com/dunovank/jupyter-themes
https://medium.com/@rbmsingh/making-jupyter-dark-mode-great-5adaedd814db
jt -t monokai -f firacode -fs 12 -N -kl -cursw 5 -cursc r -cellw 95% -T -ofs 12 -dfs 12
Para resetear el tema: jt -r
Lo malo es que las imágenes de los notebooks tienen la leyenda en negro y con transparencia y no se ve nada, asumen fondo claro. Después de ejecutarlo ya funciona bien
Luego le pongo el contraste al máximo en el Dark Reader

1.9.4. Jupyter Notebook vim

1.9.4.1. Ctrl+O

Sirve para entrar en modo notebook para el siguiente comando, y luego volver al modo dentro de celda
Es como el Insert Normal Mode en vim

  1. gg, G
  2. zm, zr, zA, zM, zR
  3. zt, zz, zb
  4. -, M

    split, merge cells

  5. o, O
  6. dd, yy, p, P
  7. Otros
    S
    toggle output scrolling
    Ctrl+C
    Interrupt Kernel
1.9.4.2. Ctrl+J,K → Para moverse entre celdas sin salir del modo de inserción en celda
1.9.4.3. Firefox intercepta algunos atajos de teclados
1.9.4.4. atajos de vim en juyter notebook

1.9.5. nbextensions

  pip install jupyter_contrib_nbextensions
  jupyter contrib nbextension install --user
  pip install jupyter_nbextensions_configurator
  jupyter nbextensions_configurator enable --user

1.9.6. trucos

https://medium.com/swlh/some-neat-jupyter-tricks-be0775c3f17

  • ESC+00 Para reiniciar el kernel
  • ESC+Z Para deshacer el borrado de una celda
  from IPython.core.interactiveshell import InteractiveShell
  InteractiveShell.last_node_interactivity = "all" # "last_expr" por defecto
  # Imprimir

Poner notebook al 100% de ancho

  from IPython.core.display import display, HTML
  display(HTML("<style>.container { width:100% !important; }</style>"))

\sigma[TAB] autocompleta y crea variables de python con letras griegas

1.9.7. matplotlib interactivo

breakpoint en jupyter

from IPython.core.debugger import Pdb; Pdb().set_trace()
1.9.7.1. matplotlib interactivo

https://stackoverflow.com/questions/14261903/how-can-i-open-the-interactive-matplotlib-window-in-ipython-notebook

  %matplotlib inline # Plotear imágenes sobre el notebook, no interactivo
  %matplotlib notebook # Interactivo
  %matplotlib tk # Funciona por defecto
  %matplotlib qt # Instalar PyQt5 con pip

1.9.8. ipython

1.9.8.1. importar ipython
  from IPython import start_ipython, embed
  # embed importa lo mínimo para poder salir de IPython sin consecuencias
  # start_ipython empieza una sesión de IPython y cambia el comportamiento de python por debajo
  (pulsar Ctrl+D para salir)
1.9.8.2. line_profiler and memory_profiler

https://github.com/rkern/line_profiler/issues/132,
https://github.com/rkern/line_profiler/issues/133
pip install Cython git+https://github.com/rkern/line_profiler.git
https://jakevdp.github.io/PythonDataScienceHandbook/01.07-timing-and-profiling.html

  %load_ext line_profiler
  %lprun -f func func(x, y, z)

<del>=Ctrl+q, Ctrl+j= Ctrl+J añade nueva línea en ipython Ctrl+Alt+Q,
Ctrl+Shift+J </del> -> Ctrl+O nueva línea debajo, Ctrl+O,Ctrl+N para
añadir una nueva línea y moverse a ella

1.9.8.3. autoreload

https://ipython.readthedocs.io/en/stable/config/extensions/autoreload.html

  %load_ext autoreload
  %autoreload
  run 'loquesea.py'

1.9.9. Cómo exportar en pandoc para colaborar

1.10. Buscar charlas/libros/webs sobre Jupyter en profundidad   someday_20230330

Muchas de las cosas con las que me atasco en jupyter probablemente se resolverían más rápido si investigase sobre cómo funciona internamente
https://jupyter.org/enhancement-proposals/79-notebook-v7/notebook-v7.html

1.12. GitHub - voila-dashboards/voila: Voilà turns Jupyter notebooks into standalone web applications

«Plotly may better for Python right, now, but it supports multiple languages»
Voilà is built upon Jupyter standard formats and protocols, and is agnostic to the programming language of the notebook

1.13. Python Dashboarding Ecosystem and Landscape | by Sophia Yang | Towards Data Science

  • Dash is primarily tied to Plotly. If you are a Plotly user, Dash is your natural choice.
  • Voila supports various libraries with an ipywidgets interface, such as Matplotlib and Plotly. For libraries that are not Jupyter widgets such as Bokeh, Voila does not work well.
  • Streamlit and Panel support many plotting libraries, including Matplotlib, Seaborn, Altair, Plotly, Bokeh, PyDeck, GraphViz and more.
  • Furthermore, if you have a need for interactivity in a plot such as creating interlinked plots, Panel provides rich support for advanced interactivity features, while Streamlit does not allow for interactivity with a plot.

1.13.2. Plotly has plugins for Grafana

1.13.3. Plotly Performance

No va muy bien cuando tienes muchos datos (incluso con ~10000 ya va lento)
En principio instalando orjson va más rápido pero lo he instalado y no he notado nada (también puede ser porque estoy en python 3.8)
https://dash.plotly.com/performance

1.14. Hacer literate programming con Jupyter Notebooks   project

GitHub - davidesarra/jupyter_spaces: Create parallel namespaces in Jupyter Notebooks
Los namespaces son una abstracción de Jupyter
Literate programming es mejor que jupyter notebook porque por defecto sólo puedes acceder a las variables que declaras explícitamente
En Jupyter , por defecto, accedes a todas las variables
Este paquete limita el ámbito de cada celda
Podrías definir una función por cada celda pero es engorroso

  • MAYBE Puede ser un buen proyecto hacer que los namespaces se tengan que declarar explícitamente namespace1.var, namespace2.var para que sea más parecido a literate programming, o también declarar explícitamente al principio de la celda qué variables quieres exportar
  • Hay que ver cómo se aplica eso a las exportaciones con jupytext, lo mismo también hay que tocar algo. Si uso la segunda opción de declarar explícitamente qué variables exportas no hay que tocar nada
  • De hecho una buena exportación puede ser hacer una función por celda y encadenarlas todas. Al final defines una interfaz por cada celda de las variables que devuelve/exporta. No es una encadenación exacta de variables porque el uso en otra celda es implícito, no explícito como con {{ a }} (aunque una manera de exportarlo a script es reemplazar “{{” y “}}” por “”, y así tienes exportación y uso. Enronces ahí sí que se puede exportar a funciones
  • MAYBE Ejecutar la extensión por defecto para que sea transparente

1.14.9. GitHub - ipyflow/ipyflow: Next-generation IPython kernel with reactivity, execution suggestions, syntax extensions, and more.

Formerly nbsafety

1.14.9.1. Reactive mode

Jupyterlab only:
%safety mode reactive
Feature request: automatic update of dependent cells · Issue #87 · nbsafety-project/nbsafety · GitHub
Te crea un kernel global, y luego en función de la carpeta desde la que lo lances te selecciona bien el entorno virtual

1.16. Flujo de trabajo 2022-12-30

1.16.1. Hashable DataFrames + lru_cache + guardar en disco

Por si quieres meter un DataFrame en un diccionario. También está interesante porque te permite guardar resultados en disco

= Para funciones, crea uno por cada archivo, estilo: __main__--home-julian-Work-1projects-chatgpt-chatgpt-prompt, pero no utiliza el de __main__, y copiando/pegando tampoco funciona

1.16.1.1. Hacer un cronjob que me mueva el caché a una carpeta con el nombre bueno

Que cree la carpeta si no existe

find . -regextype posix-extended -regex '.+--tmp-ipykernel-.+'
find . -type d -regextype posix-extended -regex '^.+--tmp-ipykernel-[0-9]+$' 2>/dev/null
# Ordenar por recha para que sobreescriba empezando con los más antiguos
find . -type d -regextype posix-extended -regex '.+--tmp-ipykernel-[0-9]+'  2>/dev/null  -printf "%T@ %p\n" | sort -n | awk '{$1="";print}' | awk '{$1=$1;print}'
# Ejecutar un rsync con una barra al final del directorio fuente para que no cree nuevos directorios sino que haga una especie de merge
find . -type d -regextype posix-extended -regex '.+--tmp-ipykernel-[0-9]+'  2>/dev/null  -printf "%T@ %p\n" | sort -n | awk '{$1="";print}' | awk '{$1=$1;print}' | sed -E 's/(.+)(--tmp-ipykernel-[0-9]+)/rsync -rau "\1\2\/" "\1"/' | bash

# Escapar el % que tiene significado especial en crontab
# https://stackoverflow.com/questions/27123367/percent-sign-not-working-in-crontab
*/5 * * * * ( ps -A | grep jupyter ) || ( find . -type d -regextype posix-extended -regex '.+--tmp-ipykernel-[0-9]+'  2>/dev/null  -printf "\%T@ \%p\n" | sort -n | awk '{$1="";print}' | awk '{$1=$1;print}' | sed -E 's/(.+)(--tmp-ipykernel-[0-9]+)/rsync -rau "\1\2\/" "\1"/' | bash )

El problema es que me interpreta mal y me copia todo recursivamente, tengo que pensar mejor el comando
http://qdosmsq.dunbar-it.co.uk/blog/2013/02/rsync-to-slash-or-not-to-slash/

1.16.1.2. joblib convierte a string cuando no puede utilizar cache

No es lo ideal

1.16.1.3. Cacheo tendría que ir a nivel de fila, no de DataFrame

Así reutilizas lo máximo posible

1.16.2. Resumen

  • %%writefile file.py
    Para escribir a archivos externos desde jupyter (pasar funciones a .py pero tenerlo todo en un mismo notebook, pueden estar ocultos, marcarse como saltarlas por defecto, que no se exporten a JupyText esas celdas)
  • %bookmark <nombre> <dir> → cd -b <nombre>
    Por ejemplo si haces una exportación a un directorio distinto para manterer el tuyo limpio
  • %edit
  • %load → cargar script (embeber)
  • %logon, %logstart → logging
  • %save te ejecuta una celda (expandiendo los comandos % y con rango de líneas opcionales) y guarda stdout en un archivo
  • %%capture → write stdout to file

1.16.3. Cacheo avanzado?   idea

Cachear tablas sql? Se puede incluir fecha de última modificación como parte de los argumentos que toma la función para que si cambia se recalcule automáticamente (esta petición tiene que ser mucho más rápida que hacer la petición entera o si no no estamos ganando nada)
Existe hash de una tabla para detectar si ha cambiado? Con fecha de última modificación debería valer
Parsear sql para sacar las tablas de origen (from, join, …)

1.16.4. De With a Tabla

1.16.5. Librerías que utilizo 2023-02-21

1.17. Framework de exploración de datos

  1. Cómo incluir un flujo para imágenes? Org no es fluido para redimentsionar imágenes, ponerlas una al lado de la otra…
    Aquí estoy un poco bloqueado por Scroll a nivel de línea, cuando tengo una imagen más grande que la pantalla salta y no la puedo ver entera
    Si quiero ver imágenes en paralelo simplemente puedo hacer un split
    Con Jupyter/Matplotlib es muy manual. Latex al final? Aprender/Retomar Latex?
    org-cdlatex es la manera que tiene org de escribir latex con abreviaturas

  2. hay una manera de escribir SQL/Python/Pandas sin tener que tardar tanto e ir tan rápido como en excel? No ir al nivel de texto sino más alto, nivel de métodos encadenados y demás: Autocompletado con esteroides: https://www.visidata.org/ → spreadsheet in terminal
    https://github.com/adamerose/PandasGUI/blob/master/tests/tests.py#L116 → code = pgdf.code_export()

    1. Autocompletado de queries que haces mucho y ya sabes cuales son
    2. Abrir un archivo que utilizas mucho (por ejemplo el csv de cruce de una ALCO con el movil_id)
    3. Autocompletado de syntaxis (quizás cruzar también con tldr, cheat.sh, gpt o como se llame…)
    4. Poner links a las carpetas de espanso a un yaml que está en ~/.config/espanso/*.yaml (pilla todos por defecto)
      https://espanso.org/docs/configuration/include-and-exclude/
      Puede salir un desplegable jerárquico y demás en espanso, que tiene funcionalidades de GUI, o incluso mejor seleccionarlo con una terminal de fzf que se cree al vuelo

    En DBeaver puede ir por conexión o ser global
    vim dbeaver, terminal (sed), postman y jupyter idealmente, o incluso en i3 para que valga en todos sitios

1.17.1. Cuaderno 2023-02-18

Pipeline de exploración de datos

Fuente de datos ↘ ↗ img
función cacheada (joblib/pickle o postgres django) → latex
where, parámetros ↗ ↘ backend interactivo, PyQt, Plotly

→ LAs imgs las tienes etiquetadas y con fecha, listo para poder buscar con fecha, con caché en disco
Te da igual qué utilices mientras guarde una imagen
La estrategia de tags depende del SO
Puedes recalcular de manera rápida, pero lo mejor es una imagen si tienes que enseñar algo rápido
Con Matplotlib tienes muchos widgets (pero no vale para web)
Plotly parece que no aguanta muchos puntos (500,000 le cuesta)
PyQt Agg rasteriza los puntos, y es muy eficiente
%matplotlib widget es interactivo y guarda imagen (pero se sobreescribe)

1.17.2. Plotlyficar Matplotlib

Hacer un checkbox que sea jerárquico (por ejemplo agrupas en función de la zona geográfica y del edificio, y los edificios siempre están contenidos en una misma zona geográfica que activa/desactiva todos los edificios de esa zona), escalable en función de si hay muchos puntos. Plotly utiliza scroll pero eso no lo tienes en matplotlib, aunque sí en PyQt quizás
Esto en Folium es un FeatureGroup
Otra cosa de plotly es datos temporales (fecha en hover)
Los formatos de fecha de matplotlib son complicados d eusar (añadir más granularidad es complicado y fastidia lo que ya está hecho por defecto)
Añádir un folium para resolver casos extraños
Idealmente deberías de tener un mapa que se recarga en notebook (con número fijo de puntos para que no llene la ram), con AntPath para mostrar dirección
Se salta 1 de cada X puntos si hay muchos (asumiendo que están cerca, o quitar los puntos muy cercanos, o combinar los 2 criterios)
Es complicado añadir interactividad a Matplotlib: se asume variables globales que actualizan las cosas, porque se llaman “fuera” de cualquier función. Está pensado para scripts

1.17.3. Marcado de datos

Estilo aplicar de algún color un dataframe en jupyter para que se vea color (html)

1.18. Añadir código al propio jupyter

Por ejemplo quiero generar código que crea celdas y que sea collapsable por defecto cuando lo creo, o con un cierto formato…
Otra es sobreescribir el código de .py o .md o lo que sea si estoy utilizando Jupytext

1.18.1. Código que funciona

from IPython import get_ipython
from IPython.core.magic import Magics, magics_class, line_magic

@magics_class
class MyMagics(Magics):

    @line_magic
    def lmagic(self, line):
        "Replace current line with new output"
        raw_code = 'print("Hello world!")'
        # Comment out this line if you actually want to run the code.
        self.shell.set_next_input('%lmagic\n{}'.format(raw_code), replace=False)
        self.shell.set_next_input('%lmagic\n{}'.format(raw_code), replace=False)
        # Uncomment this line if you want to run the code instead.
        self.shell.run_cell(raw_code, store_history=False)

ip = get_ipython()
ip.register_magics(MyMagics)
  • Si pones replace=True se sobreescribe a sí mismo, si pones replace=False escribe la siguiente celda y puedes hacer múltiples celdas, puede ser un iterable
    Parece que va de abajo arriba
from IPython import get_ipython
from IPython.core.magic import Magics, magics_class, line_magic
a = 1
@magics_class
class MyMagics(Magics):

    @line_magic
    def lmagic(self, line):
        "Replace current line with new output"
        raw_code = 'print("Hello world!")'
        # Comment out this line if you actually want to run the code.
        self.shell.set_next_input(f'a = {a}'.format(raw_code), replace=False)
        self.shell.run_cell('a += 1', store_history=False)
        # Uncomment this line if you want to run the code instead.
        self.shell.run_cell(raw_code, store_history=False)

ip = get_ipython()
ip.register_magics(MyMagics)

Lo que quedaría ahora sería convertir ese tipo de celda en markdown

1.18.2. Código final

Tiene que estar invertido porque es un stack (primero crea la última)
Puedo hacerlo una lista y luego invertirla
Hay que cambiar el tipo a celda de markdown (seleccionar varias con shift) y te queda unas celdas que son colapsables en Jupyter Lab

from IPython import get_ipython
from IPython.core.magic import Magics, magics_class, line_magic
a = 6
@magics_class
class MyMagics(Magics):

    @line_magic
    def lmagic(self, line):
        self.shell.set_next_input('#' * a + f' Headline \nEsto es un headline de nivel {a}', replace=False)
        self.shell.run_cell('a -= 1', store_history=False)

ip = get_ipython()
ip.register_magics(MyMagics)

Lo siguiente que quedaría sería modificarlo para que:

  1. Sea una celda de tipo Markdown
  2. Que tenga jp-MarkdownHeadingCollapsed=true
  3. Meterle tags para ya hacer lo que te da un poco la gana

Lo que saca por percent script es esto:

1.20. Jupyter Notebook + Git

Author: Julian Lopez Carballal

Created: 2024-09-16 Mon 06:17