Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the executor function to provide support for plotly that fixes #36 #44

Merged
merged 6 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion lida/components/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import matplotlib.pyplot as plt
import pandas as pd
import plotly.io as pio

from lida.datamodel import ChartExecutorResponse, Summary

Expand Down Expand Up @@ -235,7 +236,47 @@ def execute(
)
)
return charts

elif library == "plotly":
for code in code_specs:
try:
ex_locals = get_globals_dict(code, data)
exec(code, ex_locals)
chart = ex_locals["chart"]

if pio:
chart_bytes = pio.to_image(chart, 'png')
plot_data = base64.b64encode(chart_bytes).decode('utf-8')

charts.append(
ChartExecutorResponse(
spec=None,
status=True,
raster=plot_data,
code=code,
library=library,
)
)
except Exception as exception_error:
print(code)
print(traceback.format_exc())
if return_error:
charts.append(
ChartExecutorResponse(
spec=None,
status=False,
raster=None,
code=code,
library=library,
error={
"message": str(exception_error),
"traceback": traceback.format_exc(),
},
)
)
return charts

else:
raise Exception(
f"Unsupported library. Supported libraries are altair, matplotlib, seaborn, ggplot. You provided {library}"
f"Unsupported library. Supported libraries are altair, matplotlib, seaborn, ggplot, plotly. You provided {library}"
)
17 changes: 17 additions & 0 deletions lida/components/scaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ def plot(data: pd.DataFrame):
return chart
chart = plot(data) # data already contains the data to be plotted. Always include this line. No additional code beyond this line..
"""

elif library == "plotly":
instructions = {
"role": "system",
"content": f"If calculating metrics such as mean, median, mode, etc. ALWAYS use the option 'numeric_only=True' when applicable and available, AVOID visualizations that require nbformat library, ALWAYS return the chart object {mpl_pre}",
}
template = \
"""
import plotly.express as px
<imports>
def plot(data: pd.DataFrame):
<stub> # only modify this section
fig.show()
return chart
chart = plot(data) # data already contains the data to be plotted. Always include this line. No additional code beyond this line..
"""

else:
raise ValueError(
"Unsupported library. Choose from 'matplotlib', 'seaborn', 'plotly', 'bokeh', 'ggplot', 'altair'."
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ dependencies = [
"networkx",
"geopandas",
"matplotlib-venn",
"wordcloud",
"wordcloud",
"kaleido"
]
optional-dependencies = {web = ["fastapi", "uvicorn"], transformers = ["llmx[transformers]"], tools=["geopy", "basemap", "basemap-data-hires"], infographics=["peacasso"]}

Expand Down