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 |
| root: ShadowRoot; | ||
| wrapper: HTMLDivElement; | ||
| _id: number; | ||
| theme: string; |
There was a problem hiding this comment.
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.
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.
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.
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.
| const config = create_config(darkmode); | ||
| spec['config'] = config; | ||
| switch (value['chart'] || '') { | ||
| case "scatter": |
There was a problem hiding this comment.
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.
| const _autoscroll = autoscroll === "true" ? true : false; | ||
|
|
||
| this.wrapper.style.minHeight = initial_height || "300px"; | ||
| console.log(this.theme); |
| $: 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.
| spec['config'] = config; | |
| spec.config = config; |
This is more idiomatic.
|
|
||
| $: darkmode = theme == "dark" | ||
|
|
||
| $: if(value && value['type'] == "altair") { |
There was a problem hiding this comment.
| $: if(value && value['type'] == "altair") { | |
| $: 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.
| switch (value['chart'] || '') { | |
| switch (value.chart || '') { |
| 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.
I won't suggest but use dot notation for property access: object.prop1.prop2.prop3 etc.
| 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.
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.
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.
pngwn
left a comment
There was a problem hiding this comment.
Code looks good. Not sure about the pending design questions but I think we can solve them in a follow-up, if needed.
|
|
||
| ### 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.
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! |
| 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.
gr.ScatterPlot should also be able to take in a function for the value, no?
abidlabs
left a comment
There was a problem hiding this comment.
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.ScatterPlotcomponent. My plan is to have a separate component for each type of plot but they all inherit forgr.Plot. They should only differ fromPlotin 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
valueparameter must be passed in order to be able to update the plot.Additional features added:
.style(container=False)added toPlotcomponentcaptioncan 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)