feat: add visualization layer with altair#5
Conversation
- Add altair for chart generation - Recommend chart based on dataframe schema - Allow user to customize chart - Add tests for visualization module
Summary of ChangesHello @ravishan16, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant enhancement to the Streamlit application by adding a robust data visualization layer. Leveraging Altair, users can now interactively generate various charts directly from their dataframes, with intelligent recommendations for chart types and axes. This greatly improves the app's capability to present and analyze data visually. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5 +/- ##
==========================================
+ Coverage 22.07% 22.87% +0.80%
==========================================
Files 14 24 +10
Lines 956 1893 +937
Branches 149 330 +181
==========================================
+ Hits 211 433 +222
- Misses 729 1415 +686
- Partials 16 45 +29
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Code Review
This pull request introduces a visualization layer using Altair, which is a great addition for data exploration within the app. The implementation is well-structured, with a new module for visualization logic and corresponding unit tests. I've identified a high-severity bug that could cause the application to crash under certain conditions, along with a couple of medium-severity suggestions to improve user experience and code maintainability. Overall, this is a solid feature addition.
src/visualization.py
Outdated
| selected_x_axis = st.selectbox("X-axis", x_axis_options, index=x_axis_options.get_loc(x_axis) if x_axis else 0) | ||
|
|
||
| y_axis_options = cols | ||
| selected_y_axis = st.selectbox("Y-axis", y_axis_options, index=y_axis_options.get_loc(y_axis) if y_axis else 1) |
There was a problem hiding this comment.
This line can cause the application to crash. If y_axis is None (when no recommendation is available) and the result DataFrame has only one column, y_axis_options will have a length of 1. The code then attempts to use index=1, which is out of bounds and raises an exception. The default index should be handled safely to prevent this.
selected_y_axis = st.selectbox("Y-axis", y_axis_options, index=y_axis_options.get_loc(y_axis) if y_axis else min(1, len(y_axis_options) - 1))
src/visualization.py
Outdated
| if chart_type == "Bar": | ||
| chart = alt.Chart(df).mark_bar().encode( | ||
| x=x, | ||
| y=y, | ||
| ) | ||
| elif chart_type == "Line": | ||
| chart = alt.Chart(df).mark_line().encode( | ||
| x=x, | ||
| y=y, | ||
| ) | ||
| elif chart_type == "Scatter": | ||
| chart = alt.Chart(df).mark_circle().encode( | ||
| x=x, | ||
| y=y, | ||
| ) | ||
| elif chart_type == "Histogram": | ||
| chart = alt.Chart(df).mark_bar().encode( | ||
| x=alt.X(x, bin=True), | ||
| y='count()', | ||
| ) | ||
| elif chart_type == "Heatmap": | ||
| chart = alt.Chart(df).mark_rect().encode( | ||
| x=x, | ||
| y=y, | ||
| ) | ||
| else: | ||
| st.error("Invalid chart type") | ||
| return None |
There was a problem hiding this comment.
This if/elif/else chain for creating charts works, but it can be refactored into a more maintainable and extensible structure using a dictionary. Mapping chart types to their configurations would make the code cleaner and simplify adding new chart types in the future.
chart_configs = {
"Bar": {"mark": "bar", "encode": {"x": x, "y": y}},
"Line": {"mark": "line", "encode": {"x": x, "y": y}},
"Scatter": {"mark": "circle", "encode": {"x": x, "y": y}},
"Histogram": {"mark": "bar", "encode": {"x": alt.X(x, bin=True), "y": 'count()'}},
"Heatmap": {"mark": "rect", "encode": {"x": x, "y": y}},
}
if chart_type in chart_configs:
config = chart_configs[chart_type]
chart = alt.Chart(df).mark(**config["mark"]).encode(**config["encode"])
else:
st.error("Invalid chart type")
return None
src/visualization.py
Outdated
| selected_chart_type = st.selectbox("Chart type", chart_type_options, index=chart_type_options.index(chart_type) if chart_type else 0) | ||
|
|
||
| x_axis_options = cols | ||
| selected_x_axis = st.selectbox("X-axis", x_axis_options, index=x_axis_options.get_loc(x_axis) if x_axis else 0) | ||
|
|
||
| y_axis_options = cols | ||
| selected_y_axis = st.selectbox("Y-axis", y_axis_options, index=y_axis_options.get_loc(y_axis) if y_axis else 1) |
There was a problem hiding this comment.
When a user selects "Histogram" as the chart type, the UI still prompts for a "Y-axis". This can be confusing, as the make_chart function correctly ignores this selection and uses a count for the y-axis.
To improve the user experience, I recommend conditionally hiding the Y-axis selector when the chart type is 'Histogram'. This would make the interface more intuitive.
This PR resolves #3 by adding a visualization layer to the Streamlit app using Altair.