def base64_img_html(im, width): io_buffer = BytesIO() st.experimental_show(type(im)) im.save(io_buffer, format="JPEG") img_str = base64.b64encode(io_buffer.getvalue()).decode("utf-8") st.experimental_show(type(img_str)) img_tag = f'<img style="border: 1px solid #ddd" src="data:image/jpeg;base64,{img_str}" />' # img_tag = f'<img style="width:{width}px" src="https://raw.githubusercontent.com/treuille/img-to-base64/main/screenshot-1-face-gan.png"/>' return img_tag
def test_st_show(self): """Test st.experimental_show. Ideally we could test the order and content of the deltas. But not possible to inject a shared queue in `streamlit._with_dg()` Improvements: - verify markdown is escaped on write delta """ thing = "something" with patch("streamlit.write") as write: with patch("streamlit.markdown") as markdown: st.experimental_show(thing) write.assert_called_once() markdown.assert_called_once() foo_show_bar = "baz" with patch("streamlit.write") as write: with patch("streamlit.markdown") as markdown: st.experimental_show(foo_show_bar) write.assert_called_once() markdown.assert_called_once()
) points = ( alt.Chart(source) .mark_point(filled=True, color="black") .encode( x=alt.X("confidence:Q", aggregate="mean"), y=alt.Y("intent:N"), ) ) st.markdown("## Simple Line View") st.altair_chart(error_bars + points, use_container_width=True) hist = ( alt.Chart(source) .mark_area(opacity=0.3, interpolate="step") .encode( alt.X("confidence:Q", bin=alt.Bin(maxbins=100)), alt.Y("count()", stack=None), alt.Color("intent:N"), ) ) with st.beta_expander("Histogram View"): st.altair_chart(hist, use_container_width=True) with st.beta_expander("Generated Examples"): st.experimental_show(augs)
def advanced(page): '''Advanced Streamlit commands''' st.title(page) st.write(""" Welcome to the Wizard level of Streamlit :mage: The functions on this page will give you even more control and access to unique features, to help guide your aprentice's quest through the trials of your app :motorway: This section is for **anyone** (not just experienced coders), so continue onward to knowledge! :rolling_on_the_floor_laughing: """) # ****************** PROGRESS AND STATUS ****************** st.header('Progress and Status') st.write( """These commands can be very useful for managing a users expectation if you are loading a large data set, running an expensive computation or just wanting to keep your app user updates on whats happening behind the scenes! :eyes: Lets see what cool things you can do to keep your user occupied while your app is updating. """) buff, col1, mid, col2, buff = st.beta_columns([1, 20, 0.5, 20, 1]) with col1: st.subheader('Spinner') st.write( "Use this when you want to let your user know things are happening in the backend" ) st.code(""" with st.spinner('working on it'): time.sleep(5) st.write('done!') """) run_spinner = st.button("Run Spinner code?", key="spinner_run") if run_spinner: with st.spinner('working on it'): time.sleep(3) st.write("done!") st.markdown("---") st.subheader("Error") st.write( "Use this to let your user know something went wrong, without breaking your app!" ) st.code(""" text = st.text_input("Type your name here") if len(text) == 0: st.error('You must enter your name to continue') else: st.write("Hey %s :wave:" %text) """) text = st.text_input("Type your name here") if len(text) == 0: st.error('You must enter your name to continue') else: st.write("Hey %s :wave:" % text) st.markdown("---") st.subheader("Information") st.write("You can use this to display info to your app user") st.code(""" st.info("You need to know this bit of info before continuing!") """) st.info("You need to know this bit of info before continuing!") st.markdown("---") st.subheader("Balloons") st.write( "While this doesn't strictly tell the user information, it is a fun way to let them know something has happened!" ) st.code(""" st.balloons() """) check = st.checkbox("See what balloons do", key="balloon_run") if check: st.balloons() with col2: st.subheader("Success") st.write( "Use this when you want to let your user know that a task as been successful" ) st.code(""" column_names = ['a', 'b'] data = pd.DataFrame(np.random.randn(10,2), columns=column_names) st.success("Created Pandas DataFrame") """) run_success = st.button("Run Success code?", key="success_run") if run_success: column_names = ['a', 'b'] data = pd.DataFrame(np.random.randn(10, 2), columns=column_names) st.success("Created Pandas DataFrame") st.markdown("---") st.subheader("Warning") st.write("Use this to let your user know something could go wrong") st.code(""" st.warning("Something may happen that I can't control....") n = np.random.normal() if n < 0: st.write(n) else: st.write('Yup something happened!') """) st.warning("Something may happen that I can't control....") n = np.random.normal() if n < 0: st.write(n) else: st.write('Yup something happened!') st.markdown("---") st.subheader("Progress") st.write( "This is similar to spinner, but allows you to increment how complete your task is!" ) st.code(""" my_bar = st.progress(0) for amount_complete in range(100): time.sleep(0.1) my_bar.progress(amount_complete +1) """) progress = st.checkbox("Run Progress code", key="progress_run") if progress: my_bar = st.progress(0) for amount_complete in range(100): time.sleep(0.1) my_bar.progress(amount_complete + 1) st.markdown("---") st.subheader("Exception") st.write("") st.code(""" e = RuntimeError('This is an exception of type RuntimeError') st.exception(e) """) e = RuntimeError('This is an exception of type RuntimeError') st.exception(e) # ****************** CONTROL FLOW ****************** st.markdown("---") st.header("Control Flow") # st.cache -> its own section? speed up your app section title # st.stop() # st.empty() # containter could go here # st.help() # ****************** EXPERIMENTAL ****************** st.markdown("---") st.header("Experimental") buff, exp1, mid, exp2, buff = st.beta_columns([1, 20, 0.5, 20, 1]) with exp1: st.subheader("show") st.code(''' dataframe = pd.DataFrame({ 'first column': [1, 2, 3, 4], 'second column': [10, 20, 30, 40], }) st.experimental_show(dataframe) ''') dataframe = pd.DataFrame({ 'first column': [1, 2, 3, 4], 'second column': [10, 20, 30, 40], }) st.experimental_show(dataframe) st.subheader("rerun") with exp2: st.subheader("set query parameters") st.write(""" Query parameters are a set of values attached to the end of a url as an extension. They are used to help define specific content or actions based on the values being passed. You can identify the query parameters because they are the all the values at the end of a URL after the '?'.""") st.code(''' ''') st.experimental_set_query_params() st.subheader("get query parameters") st.code(''' ''') st.write(st.experimental_get_query_params()) return
# Remove the file uploader deprecation warning. st.set_option('deprecation.showfileUploaderEncoding', False) "# Grid Figure Creator" # Sidebar configuration show_config = st.sidebar.checkbox('Show raw config', True) # Load the config information from the user. config = yaml.load(open('table_config.yaml')) new_img_width = config['img-width'] remove_top_pixels = config['remove-top-pixels'] st.experimental_show(new_img_width) st.experimental_show(remove_top_pixels) # Create the images figure_html = "" for img_num, img_info in enumerate(config['imgs']): image = load_image(img_info['filename'], new_img_width, remove_top_pixels) st.image(image) st.experimental_show(type(image)) base64_img = base64_img_html(image, new_img_width) captioned_img = captioned_img_html(img_num + 1, base64_img, img_info['name'], img_info['live-url'], img_info['git-url'], new_img_width) figure_html += captioned_img + '\n'