How can I wrap Jupyter output in custom HTML? - jupyter-notebook

I want to be able to take the normal output for an object and insert it into custom HTML components. Specifically, I want to allow things like putting several charts into an accordion UI element, or having hidden dataframes that are shown when a button is clicked. Is there a way to get the HTML that would normally be output, wrap it in my own HTML components, and then output that?
I have tried:
import IPython.display as dp
dp.display(dp.HTML('<div id="mycontainer">')) # Just a simple div,
# but ideally would be e.g. bootstrap component
dp.display(my_obj) # my_obj here could be a (potentially styled) dataframe
# or a plot from matplotlib/altair/etc.
dp.display(dp.HTML('</div>'))
However, the unclosed <div> just gets automatically closed, so my_obj doesn't get inserted into it. Some objects have _repr_html_(), but not all do (particularly charts). Still, Jupyter obviously has some way of extracting HTML from arbitrary objects.
It seems from trying to read the source that nbconvert is used to change the object to HTML, but I'm not sure if A) I'm understanding that right or B) how to get HTML from an arbitrary object that is not in a notebook node object (or how to construct such an object myself).

this seams to work for me :
from IPython.core.display import display, HTML
myString = 'Hello, world!'
prependHtmlTag = '<h1>'
appendHtmlTag = '</h1>'
display(HTML(prependHtmlTag + myString + appendHtmlTag )
just put any html formated string in the display(HTML([yourString]))

Related

TanStack Table with Nuxt3, Cell Formatting

I want to use TanStack table with Nuxt3.
I have tried Cell Formatting with the following link.
https://tanstack.com/table/v8/docs/guide/column-defs
columnHelper.accessor('firstName', {
cell: props => <span>{props.getValue().toUpperCase()}</span>,
})
But I get an error TS2304 cannot find name 'span'.
can you tell me how to use Cell Formatting in Nuxt3 (Vue3)?
I wrote the code in accordance with your question.
Ultimately, I would like to use the html code to put hyperlinks in the cells

Displays the current value at the bottom of the SelectionRangeSlider Jupyter Notebook

I'm creating an interactive notebook by using the library ipywidgets. I'm interested in SelectionRangeSlider widget. In documentation, we have a simple example :
import datetime
dates = [datetime.date(2015, i, 1) for i in range(1, 13)]
options = [(i.strftime('%b'), i) for i in dates]
widgets.SelectionRangeSlider(
options=options,
index=(0, 11),
description='Months (2015)',
disabled=False
)
That creates folowing slider :
I would like to change the position of the current values selected, putting it at the bottom, like that:
I've read documentation but I couldn't find a way to do this. Anyone knows if it's even possible to change this position? Thanks a lot.
Others have done things like this by setting readout=False and then making a separate widget to display the updated values. See these examples:
workaround offered by marcusreaiche for SelectionRangeSlider
TheIdealis' answer to 'Custom formatting for ipywidgets IntSlider'

How does one pump text into a scrolling window in streamlit?

I have seen streamlit display scrolling text windows. In particular, they get displayed for dataframes.
Is there a way to somehow write incrementally to a, for example scrolling info, warning and error text window with the same color codes as the info, warning and error text lines written to the screen with streamlit.info("foo"), streamlit.warning("bar"), && streamlit.error("foobar")?
Not sure exactly what you mean by a scrolling text window, but using the st.empty(), you can continuously write over the same object, which gives the effect of what I think you're asking for:
import streamlit as st
import time
c = st.empty()
for i in range(1, 30):
time.sleep(0.5) #just here so you can see the change
c.text(i)
This example can be extended to anything where you can consistently append/pop data off a list, indexing, etc.
Edit: note that what I'm referring to as c here ends up being the generic Streamlit class that contains most/all of the widgets. So text could be swapped out for dataframe, metric and any number of other Streamlit widgets.

How do I view an image in dotnet interactive?

I would like to view an image interactively in F# jupyter notebook similarly to how I can do this in python:
from PIL import Image
Image.open("image.png")
and that shows an image.
Is there a straightforward way to do this?
EmguCV, OpenCVSharp and ImageSharp all don't work together with the plotting libraries like Plotly.NET or XPlot to provide this functionality so I can't get something like matplotlib's pyplot.imshow.
In a notebook, you can display an image like this:
#!fsharp
// Load iamge
let data = File.ReadAllBytes("E:\\Downloads\\myImage.png");
// Convert so we can display it as HTML
let b64 = Convert.ToBase64String(data);
HTML($"<img src=\"data:image/png;base64,{b64}\"></img>") // last call without ; gets displayed
// Alt, this one has a semicolon:
// display(HTML($"<img src=\"data:image/png;base64,{b64}\"></img>"));

How to get all the content within an HTML tag using XPath in R

I've seen other posts for how to do this in java but sadly, I only know R
I want to get all the content (tags, attributes, values) verbatim contained in a tag, including the content of child tags. I thought I could do something like
a = xpathSApply(html, "//span[#class = 'class name']/node()", ????)
But then I realized I don't know any functions which get the entire content of your path and not just the attributes or just the text. How would I do this?
Not sure whether that is applicable in your use-case, but have you tried to work with the library xml2?
content <- read_xml( html )
nodes <- xml_find_all(content, xpath) # or xml_find_one if you want only the first result
From there you can do all kinds of things using xml_text(), xml_attrs(), xml_name(), xml_children(), ...
To really just retrieve the complete content, a <- paste(nodes[[1]]) would do I guess...

Resources