Skip to content

Commit 0bc8fba

Browse files
authored
feat: DataFrame supported (#12)
1 parent a516c57 commit 0bc8fba

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def read(*names, **kwargs):
5959
python_requires=">=3.6",
6060
install_requires=[
6161
"streamlit >= 0.63",
62+
"pandas>=1.0.0",
6263
],
6364
cmdclass={"upload": UploadCommand},
6465
)

streamlit_g2/__init__.py

+39-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import streamlit.components.v1 as components
3+
import spec
34

45
# Create a _RELEASE constant. We'll set this to False while we're developing
56
# the component, and True when we're ready to package and distribute it.
@@ -66,6 +67,8 @@ def g2(options, style=None, key=None):
6667
#
6768
# "default" is a special argument that specifies the initial return
6869
# value of the component before the user has interacted with it.
70+
# Loop to change pd.DataFrame to JSON Object.
71+
spec.normalize_options(options)
6972
component_value = _component_func(options=options, style=style, key=key)
7073
return component_value
7174

@@ -80,22 +83,45 @@ def st_g2(options, style=None, key=None):
8083
# `$ streamlit run streamlit_g2/__init__.py`
8184
if not _RELEASE:
8285
import streamlit as st
86+
import pandas as pd
87+
88+
df = pd.DataFrame(
89+
[["Sports", 275], ["Strategy", 115], ["Action", 120], ["Shooter", 350], ["Other", 150]],
90+
columns=["genre", "sold"],
91+
)
8392

8493
options = {
8594
"autoFit": True,
86-
"type": "interval",
87-
"data": [
88-
{ "genre": "Sports", "sold": 275 },
89-
{ "genre": "Strategy", "sold": 115 },
90-
{ "genre": "Action", "sold": 120 },
91-
{ "genre": "Shooter", "sold": 350 },
92-
{ "genre": "Other", "sold": 150 },
93-
],
94-
"encode": {
95-
"x": "genre",
96-
"y": "sold",
97-
"color": "genre",
98-
}
95+
"type": "spaceFlex",
96+
"children": [
97+
{
98+
"type": "interval",
99+
"data": [
100+
{ "genre": "Sports", "sold": 275 },
101+
{ "genre": "Strategy", "sold": 115 },
102+
{ "genre": "Action", "sold": 120 },
103+
{ "genre": "Shooter", "sold": 350 },
104+
{ "genre": "Other", "sold": 150 },
105+
],
106+
"encode": {
107+
"x": "genre",
108+
"y": "sold",
109+
"color": "genre",
110+
},
111+
},
112+
{
113+
"type": "line",
114+
"data": df,
115+
"encode": {
116+
"x": "genre",
117+
"y": "sold",
118+
"shape": "smooth"
119+
},
120+
"style": {
121+
"lineWidth": 2,
122+
}
123+
}
124+
]
99125
}
100126

101127
g2(options=options, style={ "height": "400px" }, key="streamlit-g2")

streamlit_g2/spec.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pandas as pd
2+
3+
# Loop to process dataFrame to data dict.
4+
def normalize_options(options):
5+
if isinstance(options, dict):
6+
for k, v in options.items():
7+
if k == "data" and isinstance(v, pd.DataFrame):
8+
options["data"] = v.to_dict(orient='records')
9+
else:
10+
normalize_options(v)
11+
elif isinstance(options, list):
12+
for v in options:
13+
normalize_options(v)

0 commit comments

Comments
 (0)