Using Python pandas to render charts

Hi,
I just wonder if there would be possible to render charts via integray + python pandas? The idea was to give some data on input and return chart on output as base64 and than in next step render it as html.

My questions are:

  • would that be even possible?
  • how to convert image in base 64 in html connector? Or how else get chart from python-step to html-step?
  • for rendering charts with pandas, you also need to import matplotlib.pyplot, but that is not allowed in integray. Would that be possible in future? (Chart visualization — pandas 2.2.0 documentation)

Thanks for any hint.

Hello,

currently, we do not offer the matplotlib package as part of the Python processor connector. However, it will be added in the near future. Once matplotlib is available, we will update you in this thread.

Assuming we have the matplotlib package available, the solution can be implemented as follows:

  1. Using the Python processor connector, prepare the necessary plot and then convert it to base64 before returning it.
import io
import base64

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

# Sample DataFrame
data = {
  "Category": ["A", "B", "C", "D"],
  "Value": [10, 25, 15, 30],
}
df = pd.DataFrame(data)

# Generate a bar plot
plt.bar(df["Category"], df["Value"])
plt.xlabel("Category")
plt.ylabel("Value")
plt.title("Bar Plot")

# Save the plot to a BytesIO object
image_stream = io.BytesIO()
plt.savefig(image_stream, format="png", bbox_inches="tight")
image_stream.seek(0)

# Convert the plot to base64
base64_image = base64.b64encode(image_stream.read()).decode("utf-8")

return [{ "img": base64_image }]
  1. Using the HTML Creator connector, assemble an HTML where the output from the previous step is inserted into an img HTML element, specifying that the data is of type base64.
<img src="data:image/png;base64,${input.img}" alt="Bar Plot">

As you mentioned, direct import of modules from packages, such as import a.b, is not allowed. However, importing specific elements using from an import b is permitted. Importing a.b.c and so on is allowed, but only for allowed modules.

1 Like

Thank you for your answer.

That would be great! This library is really powerful in data visualisation and can give integray very nice and interesting possibilities.

Hi Petr,

thanks for the idea. We will consider adding this library in the service in one of the next releases. Will keep you posted on the status.

Tomas.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

Hi Petr,

we have added this lib to Chopok release. Please check our linkedin profile to get more information about upcoming release.

Thanks Tomas

1 Like