def pyplot(dg, fig=None, clear_figure=None, **kwargs): """Display a matplotlib.pyplot figure. Parameters ---------- fig : Matplotlib Figure The figure to plot. When this argument isn't specified, this function will render the global figure (but this is deprecated, as described below) clear_figure : bool If True, the figure will be cleared after being rendered. If False, the figure will not be cleared after being rendered. If left unspecified, we pick a default based on the value of `fig`. * If `fig` is set, defaults to `False`. * If `fig` is not set, defaults to `True`. This simulates Jupyter's approach to matplotlib rendering. **kwargs : any Arguments to pass to Matplotlib's savefig function. Example ------- >>> import matplotlib.pyplot as plt >>> import numpy as np >>> >>> arr = np.random.normal(1, 1, size=100) >>> fig, ax = plt.subplots() >>> ax.hist(arr, bins=20) >>> >>> st.pyplot(fig) .. output:: https://share.streamlit.io/0.25.0-2JkNY/index.html?id=PwzFN7oLZsvb6HDdwdjkRB height: 530px Notes ----- .. note:: Deprecation warning. After December 1st, 2020, we will remove the ability to specify no arguments in `st.pyplot()`, as that requires the use of Matplotlib's global figure object, which is not thread-safe. So please always pass a figure object as shown in the example section above. Matplotlib support several different types of "backends". If you're getting an error using Matplotlib with Streamlit, try setting your backend to "TkAgg":: echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc For more information, see https://matplotlib.org/faq/usage_faq.html. """ if not fig and config.get_option("deprecation.showPyplotGlobalUse"): dg.exception(PyplotGlobalUseWarning()) # type: ignore image_list_proto = ImageListProto() marshall(dg._get_coordinates, image_list_proto, fig, clear_figure, **kwargs) # type: ignore return dg._enqueue("imgs", image_list_proto) # type: ignore
def image( dg, image, caption=None, width=None, use_column_width=False, clamp=False, channels="RGB", output_format="auto", **kwargs, ): """Display an image or list of images. Parameters ---------- image : numpy.ndarray, [numpy.ndarray], BytesIO, str, or [str] Monochrome image of shape (w,h) or (w,h,1) OR a color image of shape (w,h,3) OR an RGBA image of shape (w,h,4) OR a URL to fetch the image from OR an SVG XML string like `<svg xmlns=...</svg>` OR a list of one of the above, to display multiple images. caption : str or list of str Image caption. If displaying multiple images, caption should be a list of captions (one for each image). width : int or None Image width. None means use the image width. Should be set for SVG images, as they have no default image width. use_column_width : bool If True, set the image width to the column width. This takes precedence over the `width` parameter. clamp : bool Clamp image pixel values to a valid range ([0-255] per channel). This is only meaningful for byte array images; the parameter is ignored for image URLs. If this is not set, and an image has an out-of-range value, an error will be thrown. channels : 'RGB' or 'BGR' If image is an nd.array, this parameter denotes the format used to represent color information. Defaults to 'RGB', meaning `image[:, :, 0]` is the red channel, `image[:, :, 1]` is green, and `image[:, :, 2]` is blue. For images coming from libraries like OpenCV you should set this to 'BGR', instead. output_format : 'JPEG', 'PNG', or 'auto' This parameter specifies the format to use when transferring the image data. Photos should use the JPEG format for lossy compression while diagrams should use the PNG format for lossless compression. Defaults to 'auto' which identifies the compression type based on the type and format of the image argument. Example ------- >>> from PIL import Image >>> image = Image.open('sunrise.jpg') >>> >>> st.image(image, caption='Sunrise by the mountains', ... use_column_width=True) .. output:: https://static.streamlit.io/0.61.0-yRE1/index.html?id=Sn228UQxBfKoE5C7A7Y2Qk height: 630px """ format = kwargs.get("format") if format != None: # override output compression type if specified output_format = format if config.get_option("deprecation.showImageFormat"): dg.exception(ImageFormatWarning(format)) # type: ignore if use_column_width: width = -2 elif width is None: width = -1 elif width <= 0: raise StreamlitAPIException("Image width must be positive.") image_list_proto = ImageListProto() marshall_images( dg._get_coordinates(), # type: ignore image, caption, width, image_list_proto, clamp, channels, output_format, ) return dg._enqueue("imgs", image_list_proto) # type: ignore
def image( self, image, caption=None, width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto", ): """Display an image or list of images. Parameters ---------- image : numpy.ndarray, [numpy.ndarray], BytesIO, str, or [str] Monochrome image of shape (w,h) or (w,h,1) OR a color image of shape (w,h,3) OR an RGBA image of shape (w,h,4) OR a URL to fetch the image from OR a path of a local image file OR an SVG XML string like `<svg xmlns=...</svg>` OR a list of one of the above, to display multiple images. caption : str or list of str Image caption. If displaying multiple images, caption should be a list of captions (one for each image). width : int or None Image width. None means use the image width, but do not exceed the width of the column. Should be set for SVG images, as they have no default image width. use_column_width : 'auto' or 'always' or 'never' or bool If 'auto', set the image's width to its natural size, but do not exceed the width of the column. If 'always' or True, set the image's width to the column width. If 'never' or False, set the image's width to its natural size. Note: if set, `use_column_width` takes precedence over the `width` parameter. clamp : bool Clamp image pixel values to a valid range ([0-255] per channel). This is only meaningful for byte array images; the parameter is ignored for image URLs. If this is not set, and an image has an out-of-range value, an error will be thrown. channels : 'RGB' or 'BGR' If image is an nd.array, this parameter denotes the format used to represent color information. Defaults to 'RGB', meaning `image[:, :, 0]` is the red channel, `image[:, :, 1]` is green, and `image[:, :, 2]` is blue. For images coming from libraries like OpenCV you should set this to 'BGR', instead. output_format : 'JPEG', 'PNG', or 'auto' This parameter specifies the format to use when transferring the image data. Photos should use the JPEG format for lossy compression while diagrams should use the PNG format for lossless compression. Defaults to 'auto' which identifies the compression type based on the type and format of the image argument. Example ------- >>> from PIL import Image >>> image = Image.open('sunrise.jpg') >>> >>> st.image(image, caption='Sunrise by the mountains') .. output:: https://share.streamlit.io/streamlit/docs/main/python/api-examples-source/charts.image.py height: 710px """ if use_column_width == "auto" or (use_column_width is None and width is None): width = -3 elif use_column_width == "always" or use_column_width == True: width = -2 elif width is None: width = -1 elif width <= 0: raise StreamlitAPIException("Image width must be positive.") image_list_proto = ImageListProto() marshall_images( self.dg._get_delta_path_str(), image, caption, width, image_list_proto, clamp, channels, output_format, ) return self.dg._enqueue("imgs", image_list_proto)