-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
gr.ScatterPlot component #2764
gr.ScatterPlot component #2764
Conversation
The demo notebooks don't match the run.py files. Please run this command from the root of the repo and then commit the changes: pip install nbformat && cd demo && python generate_notebooks.py |
The demo notebooks don't match the run.py files. Please run this command from the root of the repo and then commit the changes: pip install nbformat && cd demo && python generate_notebooks.py |
All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-2764-all-demos |
The demo notebooks don't match the run.py files. Please run this command from the root of the repo and then commit the changes: pip install nbformat && cd demo && python generate_notebooks.py |
@@ -192,6 +192,7 @@ function create_custom_element() { | |||
root: ShadowRoot; | |||
wrapper: HTMLDivElement; | |||
_id: number; | |||
theme: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the changes here are so that the Plot
component has access to the current theme
export let theme: string; | ||
export let caption: string; | ||
|
||
function get_color(index: number) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I yanked this from Chart.svelte
. Ideally this would be consolidated but it relies on the global colors
prop so I wasn't sure what the best way to refactor was.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should produce the same colors for qualitative scales as the Chart.svelte
(e.g. TimeSeries
) component which is why I used it here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll probably want to update the color schemes for visualisations in the future with a few pre-baked options for users. Data-viz colour schemes have some specific requirements/ demands that fall outside of typical 'design' issues. We can address that (and the duplication) later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Completely agree! Filed #2780
const config = create_config(darkmode); | ||
spec['config'] = config; | ||
switch (value['chart'] || '') { | ||
case "scatter": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My guess is that we'll have a separate case for each plot type. I'd expect this code to change in the future though as we add more types.
ui/packages/app/src/main.ts
Outdated
@@ -260,6 +262,7 @@ function create_custom_element() { | |||
const _autoscroll = autoscroll === "true" ? true : false; | |||
|
|||
this.wrapper.style.minHeight = initial_height || "300px"; | |||
console.log(this.theme); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove.
ui/packages/plot/src/Plot.svelte
Outdated
|
||
$: if(value && value['type'] == "altair") { | ||
spec = JSON.parse(value['plot']) | ||
const config = create_config(darkmode); | ||
spec['config'] = config; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spec['config'] = config; | |
spec.config = config; |
This is more idiomatic.
ui/packages/plot/src/Plot.svelte
Outdated
} | ||
} | ||
|
||
$: darkmode = theme == "dark" | ||
|
||
$: if(value && value['type'] == "altair") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$: if(value && value['type'] == "altair") { | |
$: if(value && value.type == "altair") { |
ui/packages/plot/src/Plot.svelte
Outdated
|
||
$: if(value && value['type'] == "altair") { | ||
spec = JSON.parse(value['plot']) | ||
const config = create_config(darkmode); | ||
spec['config'] = config; | ||
switch (value['chart'] || '') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switch (value['chart'] || '') { | |
switch (value.chart || '') { |
ui/packages/plot/src/Plot.svelte
Outdated
if (spec['encoding']['color'] && spec['encoding']['color']['type'] == 'nominal') { | ||
spec['encoding']['color']['scale']['range'] = spec['encoding']['color']['scale']['range'].map((e, i) => get_color(i)); | ||
} | ||
else if (spec['encoding']['color'] && spec['encoding']['color']['type'] == 'quantitative') { | ||
spec['encoding']['color']['scale']['range'] = ['#eff6ff', '#1e3a8a']; | ||
spec['encoding']['color']['scale']['interpolate'] = "hsl"; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I won't suggest but use dot notation for property access: object.prop1.prop2.prop3
etc.
//@ts-nocheck | ||
import Plotly from "plotly.js-dist-min"; | ||
import { Plot as PlotIcon } from "@gradio/icons"; | ||
import { colors as color_palette, ordered_colors } from "@gradio/theme"; | ||
import { get_next_color } from "@gradio/utils"; | ||
import { Vega } from "svelte-vega"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll probably want to dynamically load the SDK for the specific plot in the future but this is fine for now. Just mentioning it here. No changes required.
export let theme: string; | ||
export let caption: string; | ||
|
||
function get_color(index: number) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll probably want to update the color schemes for visualisations in the future with a few pre-baked options for users. Data-viz colour schemes have some specific requirements/ demands that fall outside of typical 'design' issues. We can address that (and the duplication) later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good. Not sure about the pending design questions but I think we can solve them in a follow-up, if needed.
CHANGELOG.md
Outdated
@@ -2,6 +2,41 @@ | |||
|
|||
## New Features: | |||
|
|||
### Scatter plot component | |||
|
|||
It is now possible to create a scatter plot without knowledge of a plotting library! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a suggestion, feel free to ignore
It is now possible to create a scatter plot without knowledge of a plotting library! | |
It is now possible to create a scatter plot natively in Gradio! |
@@ -294,7 +294,9 @@ def test_slider_random_value_config(self): | |||
assert not any([dep["queue"] for dep in demo.config["dependencies"]]) | |||
|
|||
def test_io_components_attach_load_events_when_value_is_fn(self, io_components): | |||
io_components = [comp for comp in io_components if not (comp == gr.State)] | |||
io_components = [ | |||
comp for comp in io_components if comp not in [gr.State, gr.ScatterPlot] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gr.ScatterPlot
should also be able to take in a function for the value
, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Powerhouse of a PR @freddyaboulton! I only had a suggestion for the order of the parameters in the class, but otherwise LGTM. Tested and works great
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
Thank you all so much for the help and reviews! |
Description
Overview
Implements a
gr.ScatterPlot
component. My plan is to have a separate component for each type of plot but they all inherit forgr.Plot
. They should only differ fromPlot
in their init signatures and postprocess methods. This saves us from creating a different ui component for each plot subtype.Proposed API
1. Create a static scatter plot
2. Update an existing plot
The
value
parameter must be passed in order to be able to update the plot.Additional features added:
.style(container=False)
added toPlot
componentcaption
can be added belowgr.Scatterplot
(and future plot types)Open questions
1. Position of caption relative to the plot
The caption and plot are technically centered but it may look off-centered if there is a legend on the right of the plot
We can make it look more centered by placing all legends in the bottom of the plot. Wondering what people think and any other possible solutions.
2. Make labels + titles bold font or not
Using bold makes this info stand out but it looks different from the other fonts on the app even though they are technically the same font. For comparison
Bold (current):
No Bold:
3. General styling/look of the plots
Please send any feedback on the look of the plots. Tried my best to make sure they match the look of gradio but there could still be more to do. In particular:
To test
python demo/native_plots/run.py
(also deployed to spaces)