sheet notebook itemize commands cheat ipython-notebook

ipython-notebook - itemize - jupyter notebook markdown list



Hacer celdas de salida como Markdown (3)

Me gustan las celdas Markdown de IPython por incorporar HTML y otro contenido enriquecido dentro de cuadernos. Me gustaría saber si una salida de comando puede formatearse de manera similar, en celdas de salida.

Aquí está una de mis funciones de salida de HTML:

print_html(): print """ <h2>Matplotlib''s chart gallery (Click a chart to see the code to create it)</h2><br> <div align="center"> <iframe title="Matplotlib Gallery" width="950" height="250" src="http://matplotlib.org/gallery.html#api" frameborder="0" allowfullscreen></iframe></div> """

El código HTML anterior, si se coloca en la celda de reducción (entrada), produce un buen enlace a la biblioteca Matplotlib. Pero en la celda de salida es solo texto plano. ¿Alguna forma de hacerlo rico en contenido?


Encontré una solución aquí: http://mail.scipy.org/pipermail/ipython-user/2012-April/009838.html

Citando la solución aquí para ref:

Brian Granger:

"Haga que la función devuelva el HTML sin procesar envuelto en un objeto HTML:

from IPython.core.display import HTML ... ... def foo(): raw_html = "<h1>Yah, rendered HTML</h1>" return HTML(raw_html)

"

Ahora llamar a foo () da html con formato enriquecido como quería.


Simplemente agregando alguna característica adicional a su código de ejemplo

htmlContent = '''' def header(text): raw_html = ''<h1>'' + str(text) + ''</h1>'' return raw_html def box(text): raw_html = ''<div style="border:1px dotted black;padding:2em;">''+str(text)+''</div>'' return raw_html def addContent(raw_html): global htmlContent htmlContent += raw_html # Example addContent( header("This is a header") ) addContent( box("This is some text in a box") ) from IPython.core.display import HTML HTML(htmlContent)

te da esto:


Una solución más avanzada se publicó recientemente en una entrada de blog aquí:

http://guido.vonrudorff.de/ipython-notebook-code-output-as-markdown/

Crea y registra un nuevo IPython magic %%asmarkdown . La salida de cada celda de código que antepones con este comando se procesará como celdas de reducción de valor pura. Usando el contenido de la pregunta original, lo siguiente se comportaría como se esperaba:

%%asmarkdown print """ <h2>Matplotlib''s chart gallery (Click a chart to see the code to create it)</h2><br> <div align="center"> <iframe title="Matplotlib Gallery" width="950" height="250" src="http://matplotlib.org/gallery.html#api" frameborder="0" allowfullscreen></iframe></div> """