Solved

Limitations on package installation (indsl library) when making Streamlit apps in CDF

  • 10 June 2024
  • 5 replies
  • 73 views

we're trying to build an app in streamlit (integrated in cognite). We will be having some calculations on timeseries using indsl library and building some dashboards on top of it in this app
 

When we try to install indsl package in streamlit we're getting the below error:

"

Error during booting up

Traceback (most recent call last):

  File "/lib/python3.11/site-packages/micropip/_commands/install.py", line 146, in install

    raise ValueError(

ValueError: Can't find a pure Python 3 wheel for: 'pandas<2.3.0,>=2.2.0'

See: https://pyodide.org/en/stable/usage/faq.html#why-can-t-micropip-find-a-pure-python-wheel-for-a-package

"

same issue has already been raised: 
https://hub.cognite.com/streamlit-early-adopter-285/using-indsl-library-in-streamlit-3536


 

we understand in general there is some limitation when using streamlit in cognite apps due to it being based on pyodide for packages as mentioned in below topic:

https://hub.cognite.com/developer-and-user-community-134/python-with-puodide-2323


This is what we're planning to do instead of having everything in streamlit: Use Cognite functions to compute things using indsl library and write result somewhere and use it in streamlit to make dashboards

We wanted confirmation if there is any better way than this

icon

Best answer by Sangavi M 24 June 2024, 15:07

View original

5 replies

Userlevel 4

Hi Sangavi,

As you've noted, importing InDSL into Pyodide-based environments (like Streamlit) is challenging due to the dependencies that are not readily available on Pyodide. We're aware of this limitation and understand the inconvenience it causes.

Your proposed solution to use Cognite Functions to handle computations with the InDSL library and then use the results in Streamlit for dashboarding is a valid approach. This method utilizes Cognite Functions as a backend service, where you can perform your calculations and then visualize the results in Streamlit.

That being said, there are some workarounds to bypass the import limitations directly within Streamlit, although they are not very elegant. Here’s an example code snippet that manages to import indsl into streamlit (you’ll have to add plotly as a dependency before trying this out):

import micropip
await micropip.install("indsl==8.2.1", deps=False)
await micropip.install("https://files.pythonhosted.org/packages/24/21/7d397a4b7934ff4028987914ac1044d3b7d52712f30e2ac7a2ae5bc86dd0/typing_extensions-4.8.0-py3-none-any.whl")
await micropip.install("typeguard", deps=False)
await micropip.install("scipy")
await micropip.install("matplotlib")
await micropip.install("fluids")

import streamlit as st
import plotly.graph_objects as go
from cognite.client import CogniteClient
import indsl

st.title("InDSL in Streamlit")
client = CogniteClient()

ts_list = client.time_series.list(
external_id_prefix="EVE",
unit="C",
limit=5
)
ts = st.selectbox(
label="Select a time series",
options=ts_list,
format_func=lambda x: f"{x.external_id} - {x.name}"
)

dps = client.time_series.data.retrieve_dataframe(
external_id=ts.external_id,
start="2w-ago",
end="now",
aggregates=["average"],
granularity="1h",
include_aggregate_name=False,
limit=None
)

# run an InDSL method
dps_smooth = indsl.smooth.sg(data=dps.iloc[:,0])

fig = go.Figure(
data=[
go.Scatter(
x=dps.index,
y=dps.iloc[:,0],
name="Sensor data",
mode="lines",
showlegend=True
),
go.Scatter(
x=dps_smooth.index,
y=dps_smooth.values,
name="Smoothed data",
mode="lines",
showlegend=True
),
],
layout={
"xaxis": {"showspikes": True, "spikemode": "across"},
"legend": {"orientation": "h"},
"hovermode": "x",
"spikedistance": -1,
"margin": {"l": 20, "r": 20, "b": 10, "t": 20, "pad": 0},
"height": 600
}
)

st.plotly_chart(fig, use_container_width=True)

You might need to adapt the time series filter to the particular CDF project where you are testing this in. Here's a screenshot of how this looks like:

I hope this information helps with your use case.

Userlevel 4
Badge +2

Hi @Sangavi M,

Did the above information was helpful ? 

yes, thank you!

.

Userlevel 2
Badge +3

@Lars Moastuen fyi 

Reply