I am trying to connect my database to a simple line chart using the streamlit-echarts. I know the axis’ I want to use for the chart but having a hard time figuring out the syntax for the line chart code.
Steps to reproduce
Code snippet:
mydb = connector.connect(host="00.000.000.000", database = 'FreeWeibo',user="abc", passwd="abc",use_pure=True)
query = "Select * from FreeWeiboPosts"
data = pd.read_sql(query,mydb)
st.dataframe(data)
mydb.close() #close the connection
import inspect
import textwrap
import streamlit as st
from streamlit_echarts import st_echarts
option = {
"xAxis": {
"type": Time_Created,
data: [Time_Created],
},
"yAxis": {"type": censored},
"series": [{data: [0,1], "type": "line"}],
}
st_echarts(
options=option, height="400px",
)
If applicable, please provide the steps we should take to reproduce the error or specified behavior.
Just running this code.
Explain what you expect to happen when you run the code above.
I am trying to have the bar chart display in the x axis based of the column from the database that is called "censored" and the y axis to display the column called "time_created". When I run the code I wanted it to display in a bar chart the columns I explained above where the x-axis is the censored column pulled from the database it should display in one bar censored which in the database is represented as "1" and not censored as a "0" and the y-axis is the column time created to show over what period of time the posts were censored.
Explain the undesired behavior or error you see when you run the code above.
If you're seeing an error message, share the full contents of the error message here.
NameError: name 'Time_Created' is not defined
Traceback:
File “C:\Users\avida\anaconda3\envs\FreeWeibo_working\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py”, line 564, in _run_script
exec(code, module.dict)File “C:\Users\avida\FreeWeibo\FreeWeibo.py”, line 39, in
“type”: Time_Created,
- Streamlit version: 1.15.1
- Python version: 3.9.15
- Using Conda
- OS version: Windows 10
- Browser version: Chrome
### Requirements file
Using Conda? PipEnv? PyEnv? Pex? Share the contents of your requirements file here.
Not sure what a requirements file is? Check out [this doc](https://docs.streamlit.io/streamlit-cloud/get-started/deploy-an-app/app-dependencies) and add a requirements file to your app.
### Links
* Link to your GitHub repo:
* Link to your deployed app:
### Additional information
If needed, add any other context about the problem here.
Hi there @Avidan_Rothman, make sure you are “quoting” your variable names (such as “Time_Created”, “censored”) otherwise python will look for these objects in the namespace but you have no defined them anywhere. Try following the specs shown here.
ok thank you for that tip I did try and follow the specs that you linked but I am having a hard time distinguishing where I am mapping into the echart the data being pulled from the database as in the “time_created” and “censored” are columns in the database that I am trying to define in the echart.
File "C:\Users\avida\anaconda3\envs\FreeWeibo_working\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 564, in _run_script
exec(code, module.__dict__)File "C:\Users\avida\FreeWeibo\FreeWeibo.py", line 38, in <module>
"xAxis": {
I got this error: MarshallComponentException: (‘Could not convert component args to JSON’, TypeError(‘Object of type Series is not JSON serializable’))
Traceback:
File "C:\Users\avida\FreeWeibo\FreeWeibo.py", line 40, in <module>
st_echarts(File "C:\Users\avida\anaconda3\envs\FreeWeibo_working\lib\site-packages\streamlit_echarts\__init__.py", line 80, in st_echarts
return _component_func(
still have an issue with the “st_echarts(” line getting this error. MarshallComponentException: (‘Could not convert component args to JSON’, TypeError(‘Object of type Timestamp is not JSON serializable’))
Traceback:
File "C:\Users\avida\FreeWeibo\FreeWeibo.py", line 40, in <module>
st_echarts(File "C:\Users\avida\anaconda3\envs\FreeWeibo_working\lib\site-packages\streamlit_echarts\__init__.py", line 80, in st_echarts
return _component_func(
Check out this issue on Stack Overflow. I would recommend Googling the problems when you encounter them to see if there is a solution already posted somewhere.
Here is the full code:
json_str = json.dumps({‘time_created’ : str(data)})
option = {
“xAxis”: {
“type”: “category”,
“data”: data[‘time_created’].to_list(),
So now I got a different error where there are cells in the column time created that have broken data I tried creating a line to clean the code but I am getting this error:
AttributeError: ‘Series’ object has no attribute ‘remove’
Traceback:
File "C:\Users\avida\anaconda3\envs\FreeWeibo_working\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 564, in _run_script
exec(code, module.__dict__)File "C:\Users\avida\FreeWeibo\FreeWeibo.py", line 35, in <module>
data['time_created'].remove('N', 'NaN',"2013"),File "C:\Users\avida\anaconda3\envs\FreeWeibo_working\lib\site-packages\pandas\core\generic.py", line 5902, in __getattr__
return object.__getattribute__(self, name)
Here is the code I used:
data.dropna()
data[‘time_created’].remove(‘N’, ‘NaN’,“2013”),
option = {
“xAxis”: {
“type”: “category”,
“data”: data[‘time_created’].dt.strftime(‘%y-%m-%d %h:%m:%s’).to_list(),
},
“yAxis”: {“type”: “value”},
“series”: [{“data”: data[‘censored’].to_list(), “type”: “line”}],
}
st_echarts(
options=option, height=“400px”,
)
I realized I do not need the .drop line but when I remove that line and rerun I get this error: SyntaxError: Unexpected token ‘N’, …“9:52:39”, NaN, “2013”… is not valid JSON.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.