Skip to content

Commit

Permalink
fix: fix type coercion for dataframe, catch some errors nicely
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed May 16, 2024
1 parent 3e2e6b1 commit 38f01db
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ckanext/charts/chart_builders/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def to_json(self) -> Any:
try:
return px.scatter(self.df, **self.settings).to_json()
except Exception as e:
raise exception.ChartBuildError from e
raise exception.ChartBuildError(f"Error building the chart: {e}")

def to_html(self) -> str:
return px.scatter(self.df, **self.settings).to_html()
Expand Down
2 changes: 1 addition & 1 deletion ckanext/charts/fetchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def fetch_data(self) -> pd.DataFrame:
).drop(columns=["_id", "_full_text"])

# TODO: hack... Convert all columns to numeric if possible
df = cast(pd.DataFrame, df.apply(pd.to_numeric, errors='coerce').fillna(df))
df = cast(pd.DataFrame, df.apply(pd.to_numeric, errors="coerce").fillna(0))

except ProgrammingError as e:
raise exception.DataFetchError(
Expand Down
18 changes: 10 additions & 8 deletions ckanext/charts/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def info(self) -> dict[str, Any]:
"iframed": False,
"filterable": False,
"preview_enabled": False,
"requires_datastore": True
"requires_datastore": True,
}

def can_view(self, data_dict: dict[str, Any]) -> bool:
Expand Down Expand Up @@ -106,13 +106,15 @@ def setup_template_variables(
data.update({"form_builder": form_builder})
# view show
else:
data.update(
{
"chart": utils.build_chart_for_resource(
settings, data_dict["resource"]["id"]
),
},
)
try:
chart = utils.build_chart_for_resource(
settings, data_dict["resource"]["id"]
)
except exception.ChartBuildError as e:
data["error_msg"] = e
return data

data["chart"] = chart

data.update({"settings": settings})

Expand Down
4 changes: 2 additions & 2 deletions ckanext/charts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def update_chart(resource_id: str):
)
except exception.ChartTypeNotImplementedError:
return tk.render("charts/snippets/error_chart.html")
except exception.ChartBuildError:
except exception.ChartBuildError as e:
return tk.render(
"charts/snippets/error_chart.html",
{"error_msg": tk._("Error building chart")},
{"error_msg": tk._(f"Error building chart: {e}")},
)


Expand Down

0 comments on commit 38f01db

Please sign in to comment.