How does one pump text into a scrolling window in streamlit? - 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.

Related

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'

bokeh: How to export a grid to png with given size?

I prepared some bokeh plots to display as html.
To this end I prepared a gridplot containing the subplots, the legends and some headings. This all displays extremely nice in HTML and with sizing_mode='stretch_width' it's even kind of responsive.
webpage = gridplot([[header_col],[panel_grid_plot]], toolbar_location=None, sizing_mode='stretch_width')
show(webpage)
Now I also want to export this "webpage" to a PNG. To this end, I use
export_png(webpage, filename= png_filename, width=1800)
Unfortunately, the width parameter is ignored as the webpage is an object of type gridbox and not of type Plot. (This is checked in the bokeh/io/export.py in the method def get_layout_html())
The output is a png of a width of 800px which is kind of useless as the actual information is crushed (while the legends are nicely scaled):
Any ideas how to set the width of my PNG export to useful values?
Is there a way to convert a gridboxto a Plot?
Thanx!
You should've received a warning saying that the width argument will be ignored since you're passing into export_png something that's not a plot.
A way of achieving what you want:
webpage = gridplot(..., sizing_mode='stretch_width')
webpage.width = 1800
export_png(webpage)

How can I wrap Jupyter output in custom HTML?

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]))

Designing dialog box in R

How can we a design a dialog box in R?
In my mind I have something in which an alert message is displayed or where we can write a certain value and then clicking a button it performs some computation... is that possible within the R workspace?
Something like this should work
library("tcltk")
button <- tkmessageBox(title='Message',message='Error x!',type='ok')
button <- tclvalue(button)
if(button == 'ok'){
#do something
}
winDialog also works:
library(utils)
answer<-winDialog("yesno", "was the suggestion useful?")
if (answer=='YES') {print('good!')} else {print('sorry')}
Just try to copy the full code and paste it into your R console: a dialog box will come out and a final output ('good!' or 'sorry') will appear depending on the answer you click.
You could try:
utils: select.list if you are choosing from pre-set alternatives. edit is more general, but less polished.
gWidgets: Interfaces with either tcltk or RGtk2 to produce dialogues. gmessage and ginput are probably what you are looking for.

Making a series of plots that proceed by a click

In the example below I would like to be able to control when I go to the next plot by a using mouse click (or keyboard entry)
for (i in 1:5){
plot(1:i)
Sys.sleep(1)
#add something here that requests mouse click to proceed
}
Is this possible? There is a setting in the X11() help file caled 'clickToConfirm' but I can't work out what that does.
It would also be helpful to me to be to be able to scroll back and forth through plots using the arrow keys. Is this possible?
Currently if I need to look at lots of plots I output them into a big .pdf file and scroll though them all there, but that is a bit cumbersome.
Thanks
Tom
In R, that would be done by setting par(ask=TRUE). Try the following code, which shows how to reset the par when exiting the function :
op <- par(ask=TRUE)
for (i in 1:5){
plot(1:i)
}
par(op)
If you want to keep a history to browse through, you can either open a window and click on recording in the History menu, or you can open the window yourself with the history on. Demonstrated in a function :
plot.fun <- function(){
windows(record=TRUE) # opens a window and starts recording
op <- par(ask=TRUE)
on.exit(par(op))
for (i in 1:5){
plot(1:i)
}
windows.options(record=FALSE) #stops recording.
}
plot.fun()
This will however keep all previous plots in the history for browsing as well, so if you run this code 3 times you'll have 15 plots in the plot history. Also note that the open plot window will keep on recording until you turn off the recording in the menu.
You can play with the plot history, as you'll have a variable .SavedPlots which contains the saved plot history. It can be cleared using the menu History > clear history in the plot window. If you want to clear the history from the console, you could hack that by
.SavedPlots <- NULL
But I advise you not to do this, as changing the .SavedPlots variable can cause R to crash.
See also ?windows and ?recordPlot for a bit more information. But as you're getting close to the internal code of R, be warned that you can get pretty awkward behaviour if you start playing around with these things.
For scrolling back and forth between plots using the arrow keys: it depends on the platform/R interface.
Windows: there is a recording function (see Q5 of the R for Windows FAQ) which uses Page Up/Page Down
MacOS: under the standard GUI, the Quartz window has Apple-left and Apple-right arrow
under the standard Unix (no-GUI) interface, things are more limited. You can use RStudio (which has a lot of buzz right now) ... I would have thought that JGR would have plot history as well, but it doesn't seem to ...
You can use locator - now plots change on click
for (i in 1:5){
plot(1:i)
locator(1)
}

Resources