Skip to content

Commit

Permalink
feat: DataFrame supported (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc authored Dec 27, 2023
1 parent a516c57 commit 0bc8fba
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def read(*names, **kwargs):
python_requires=">=3.6",
install_requires=[
"streamlit >= 0.63",
"pandas>=1.0.0",
],
cmdclass={"upload": UploadCommand},
)
52 changes: 39 additions & 13 deletions streamlit_g2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import streamlit.components.v1 as components
import spec

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

Expand All @@ -80,22 +83,45 @@ def st_g2(options, style=None, key=None):
# `$ streamlit run streamlit_g2/__init__.py`
if not _RELEASE:
import streamlit as st
import pandas as pd

df = pd.DataFrame(
[["Sports", 275], ["Strategy", 115], ["Action", 120], ["Shooter", 350], ["Other", 150]],
columns=["genre", "sold"],
)

options = {
"autoFit": True,
"type": "interval",
"data": [
{ "genre": "Sports", "sold": 275 },
{ "genre": "Strategy", "sold": 115 },
{ "genre": "Action", "sold": 120 },
{ "genre": "Shooter", "sold": 350 },
{ "genre": "Other", "sold": 150 },
],
"encode": {
"x": "genre",
"y": "sold",
"color": "genre",
}
"type": "spaceFlex",
"children": [
{
"type": "interval",
"data": [
{ "genre": "Sports", "sold": 275 },
{ "genre": "Strategy", "sold": 115 },
{ "genre": "Action", "sold": 120 },
{ "genre": "Shooter", "sold": 350 },
{ "genre": "Other", "sold": 150 },
],
"encode": {
"x": "genre",
"y": "sold",
"color": "genre",
},
},
{
"type": "line",
"data": df,
"encode": {
"x": "genre",
"y": "sold",
"shape": "smooth"
},
"style": {
"lineWidth": 2,
}
}
]
}

g2(options=options, style={ "height": "400px" }, key="streamlit-g2")
13 changes: 13 additions & 0 deletions streamlit_g2/spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pandas as pd

# Loop to process dataFrame to data dict.
def normalize_options(options):
if isinstance(options, dict):
for k, v in options.items():
if k == "data" and isinstance(v, pd.DataFrame):
options["data"] = v.to_dict(orient='records')
else:
normalize_options(v)
elif isinstance(options, list):
for v in options:
normalize_options(v)

0 comments on commit 0bc8fba

Please sign in to comment.