Skip to main content
Solved

Help with AttributeError: module 'streamlit.components.v1' has no attribute 'components' in Streamlit-AgGrid

  • November 20, 2024
  • 4 replies
  • 130 views

Forum|alt.badge.img+1

Hi,
I am encountering the following error while using Streamlit-AgGrid in Cognite’s Streamlit:

This link explains the workaround, but I’m not sure how to do the fixing in Cognite.

Could anyone help me and is there a specific version of Streamlit or Streamlit-AgGrid I need to use to avoid this problem?

Thanks in advance for your assistance! 

Best answer by Anders Hafreager

 

Hi,

Sorry for the late reply. What you can do is open the file in the top of your Streamlit app and perform that fix with a .replace. The way I found the path was to first have a very small app with the `streamlit-aggrid` package installed.

Then you can have as the only piece of code

import st_aggrid
st.write(st_aggrid.__file__)

which will show that the path is `/lib/python3.12/site-packages/st_aggrid/init.py`.

We will use this to know where to find the file, but we have to patch it before we import it, so take a note of that path and replace the code with

with open('/lib/python3.12/site-packages/st_aggrid/__init__.py') as f:
    contents = f.read()
contents = contents.replace('components.components.MarshallComponentException', 'components.custom_component.MarshallComponentException')
with open('/lib/python3.12/site-packages/st_aggrid/__init__.py', 'w') as f:
    f.write(contents)

This will first read the file (see GitHub solution), then replace every occurrence with the wrong line with the correct one and then write the file. Now you can safely import it after.

with open('/lib/python3.12/site-packages/st_aggrid/__init__.py') as f:
    contents = f.read()
contents = contents.replace('components.components.MarshallComponentException', 'components.custom_component.MarshallComponentException')
with open('/lib/python3.12/site-packages/st_aggrid/__init__.py', 'w') as f:
    f.write(contents)

# Now we can import
import st_aggrid

You may have to refresh the page between the first step (extracting the path) and the second since we cannot have imported the code before patching.

Let me know if this works or if it doesn’t!

View original
Did this topic help you find an answer to your question?

4 replies

Mithila Jayalath
Seasoned Practitioner
Forum|alt.badge.img

@norzarifah I’ll check on this with the engineering team and get back to you with an update.


Anders Hafreager
Practitioner

 

Hi,

Sorry for the late reply. What you can do is open the file in the top of your Streamlit app and perform that fix with a .replace. The way I found the path was to first have a very small app with the `streamlit-aggrid` package installed.

Then you can have as the only piece of code

import st_aggrid
st.write(st_aggrid.__file__)

which will show that the path is `/lib/python3.12/site-packages/st_aggrid/init.py`.

We will use this to know where to find the file, but we have to patch it before we import it, so take a note of that path and replace the code with

with open('/lib/python3.12/site-packages/st_aggrid/__init__.py') as f:
    contents = f.read()
contents = contents.replace('components.components.MarshallComponentException', 'components.custom_component.MarshallComponentException')
with open('/lib/python3.12/site-packages/st_aggrid/__init__.py', 'w') as f:
    f.write(contents)

This will first read the file (see GitHub solution), then replace every occurrence with the wrong line with the correct one and then write the file. Now you can safely import it after.

with open('/lib/python3.12/site-packages/st_aggrid/__init__.py') as f:
    contents = f.read()
contents = contents.replace('components.components.MarshallComponentException', 'components.custom_component.MarshallComponentException')
with open('/lib/python3.12/site-packages/st_aggrid/__init__.py', 'w') as f:
    f.write(contents)

# Now we can import
import st_aggrid

You may have to refresh the page between the first step (extracting the path) and the second since we cannot have imported the code before patching.

Let me know if this works or if it doesn’t!


Forum|alt.badge.img+1
  • Author
  • Committed
  • 2 replies
  • December 2, 2024

Hello, 

The workaround works perfectly! My goal is to use JsCode object for custom rendering of URLs in the AgGrid component. Now everything functions as expected and thank you so much!

 


Forum|alt.badge.img+1
  • Author
  • Committed
  • 2 replies
  • January 23, 2025

Hi ​@Anders Hafreager ,

Unfortunately, the same issue reoccurs after some time. For your information, I added the suggested 'workaround' code in <table.py> as this is where I import the <st_aggrid> library. Not sure what went wrong.

 


Reply


Cookie Policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie Settings