Virtualitics Predict has internal support for generating plots, infographics, and dashboards.
In some Flows, rather than using the Virtualitics Predict built-in plots, you may want to generate your own plots using Python libraries such as seaborn
or matplotlib
.
You may also need to display an image in a Flow, such as one from an external URL.
Getting Images into a Flow
For both external URL images and seaborn
/ matplotlib
plots, you will need to add the following import statement into your flow:
from PIL import Image as PILImage
from io import BytesIO
import requests # external images only!
External URL Images
In order to display an Image from an external URL in your Flow, you will need to add import requests
to the top of the file, as shown above.
At this point, in the part of the Step where you would like to display the image, add the following lines:
url = 'https://virtualitics.com/wp-content/uploads/2022/03/genericblogpost.png'
response = requests.get(url)
img = PILImage.open(BytesIO(response.content))
my_image_element = Image(content=img, title="Virtualitics Header",
description="External URL Image.")
Seaborn/Matplotlib
At the top of your Flow’s Python file (e.g. projects/my-flow.py
), add the following lines:
from PIL import Image as PILImage
def fig2img(fig):
"""Convert a Matplotlib figure to a PIL Image and return it"""
buf = io.BytesIO()
fig.savefig(buf)
buf.seek(0)
img = PILImage.open(buf)
return img
Display the Plot
Inside of the Step where you would like to display the plot, add the following lines:
# Most likely you aren't using clustermap but it should work the same for other plot types
g = sns.clustermap(data, method='ward', figsize=(12, 8))
plot_as_bytes = fig2img(g.fig)
my_image_element = Image(content=plot_as_bytes, title="My Title Here",
description="Describe the stuff.")
What to Expect
The image will be displayed in your Flow.
Previous Article |
Next Article |