-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* plotly + matplotlib component * update plot demos and plotly component * fix gray bg * format * pnpm lock file * add bokeh * update plot demo * add bokeh support * ignore plot file * fixed demo * fixed sorting * update image-plot deprecation warning Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
- Loading branch information
1 parent
b17afde
commit 7552e1e
Showing
9 changed files
with
203 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
numpy | ||
matplotlib | ||
matplotlib | ||
bokeh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,72 @@ | ||
from math import sqrt | ||
|
||
import matplotlib | ||
matplotlib.use('Agg') | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import plotly.express as px | ||
import pandas as pd | ||
import bokeh.plotting as bk | ||
from bokeh.models import ColumnDataSource | ||
from bokeh.embed import json_item | ||
|
||
import gradio as gr | ||
|
||
|
||
def outbreak(r, month, countries, social_distancing): | ||
def outbreak(plot_type, r, month, countries, social_distancing): | ||
months = ["January", "February", "March", "April", "May"] | ||
m = months.index(month) | ||
start_day = 30 * m | ||
final_day = 30 * (m + 1) | ||
x = np.arange(start_day, final_day + 1) | ||
day_count = x.shape[0] | ||
pop_count = {"USA": 350, "Canada": 40, "Mexico": 300, "UK": 120} | ||
r = sqrt(r) | ||
if social_distancing: | ||
r = sqrt(r) | ||
for i, country in enumerate(countries): | ||
series = x ** (r) * (i + 1) | ||
plt.plot(x, series) | ||
plt.title("Outbreak in " + month) | ||
plt.ylabel("Cases") | ||
plt.xlabel("Days since Day 0") | ||
plt.legend(countries) | ||
return plt | ||
df = pd.DataFrame({'day': x}) | ||
for country in countries: | ||
df[country] = ( x ** (r) * (pop_count[country] + 1)) | ||
|
||
|
||
if plot_type == "Matplotlib": | ||
fig = plt.figure() | ||
plt.plot(df['day'], df[countries]) | ||
plt.title("Outbreak in " + month) | ||
plt.ylabel("Cases") | ||
plt.xlabel("Days since Day 0") | ||
plt.legend(countries) | ||
return fig | ||
elif plot_type == "Plotly": | ||
fig = px.line(df, x='day', y=countries) | ||
fig.update_layout(title="Outbreak in " + month, | ||
xaxis_title="Cases", | ||
yaxis_title="Days Since Day 0") | ||
return fig | ||
else: | ||
source = ColumnDataSource(df) | ||
p = bk.figure(title="Outbreak in " + month, x_axis_label="Cases", y_axis_label="Days Since Day 0") | ||
for country in countries: | ||
p.line(x='day', y=country, line_width=2, source=source) | ||
item_text = json_item(p, "plotDiv") | ||
return item_text | ||
|
||
|
||
|
||
iface = gr.Interface( | ||
outbreak, | ||
[ | ||
gr.inputs.Dropdown( | ||
["Matplotlib", "Plotly", "Bokeh"], label="Plot Type" | ||
), | ||
gr.inputs.Slider(1, 4, default=3.2, label="R"), | ||
gr.inputs.Dropdown( | ||
["January", "February", "March", "April", "May"], label="Month" | ||
), | ||
gr.inputs.CheckboxGroup(["USA", "Canada", "Mexico", "UK"], label="Countries"), | ||
gr.inputs.CheckboxGroup(["USA", "Canada", "Mexico", "UK"], label="Countries", | ||
default=["USA", "Canada"]), | ||
gr.inputs.Checkbox(label="Social Distancing?"), | ||
], | ||
"plot", | ||
gr.outputs.Plot(type="auto"), | ||
) | ||
|
||
if __name__ == "__main__": | ||
iface.launch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
packages/app/public/** | ||
pnpm-workspace.yaml | ||
packages/app/dist/** | ||
pnpm-lock.yaml | ||
pnpm-lock.yaml | ||
packages/app/src/components/output/Plot/Plot.svelte |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<svelte:head> | ||
<!-- Loading Bokeh from CDN --> | ||
<script src="https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.min.js" on:load={handleBokehLoaded} ></script> | ||
{#if bokehLoaded} | ||
<script src="https://cdn.pydata.org/bokeh/release/bokeh-widgets-2.4.2.min.js" on:load={() => initializeBokeh(1)} ></script> | ||
<script src="https://cdn.pydata.org/bokeh/release/bokeh-tables-2.4.2.min.js" on:load={() => initializeBokeh(2)}></script> | ||
<script src="https://cdn.pydata.org/bokeh/release/bokeh-gl-2.4.2.min.js" on:load={() => initializeBokeh(3)}></script> | ||
<script src="https://cdn.pydata.org/bokeh/release/bokeh-api-2.4.2.min.js" on:load={() => initializeBokeh(4)}></script> | ||
<script src="https://cdn.pydata.org/bokeh/release/bokeh-api-2.4.2.min.js" on:load={() => initializeBokeh(5)} ></script> | ||
{/if} | ||
</svelte:head> | ||
|
||
<script lang="ts"> | ||
export let value: string; | ||
export let theme: string; | ||
import { afterUpdate, onMount} from "svelte"; | ||
import Plotly from "plotly.js-dist-min"; | ||
// Bokeh | ||
let bokehLoaded = false | ||
const resolves = [] | ||
const bokehPromises = Array(6).fill(0).map((_, i) => createPromise(i)) | ||
const initializeBokeh = (index) => { | ||
if (value["type"] == "bokeh") { | ||
console.log(resolves) | ||
resolves[index]() | ||
} | ||
} | ||
function createPromise(index) { | ||
return new Promise((resolve, reject) => { | ||
resolves[index] = resolve | ||
}) | ||
} | ||
function handleBokehLoaded() { | ||
initializeBokeh(0) | ||
bokehLoaded = true | ||
} | ||
Promise.all(bokehPromises).then(() => { | ||
let plotObj = JSON.parse(value["plot"]); | ||
window.Bokeh.embed.embed_item(plotObj, "plotDiv"); | ||
}) | ||
// Plotly | ||
afterUpdate(() => { | ||
if (value["type"] == "plotly") { | ||
let plotObj = JSON.parse(value["plot"]); | ||
let plotDiv = document.getElementById("plotDiv"); | ||
Plotly.newPlot(plotDiv, plotObj["data"], plotObj["layout"]); | ||
} else if (value["type"] == "bokeh") { | ||
let plotObj = JSON.parse(value["plot"]); | ||
window.Bokeh.embed.embed_item(plotObj, "plotDiv"); | ||
} | ||
}); | ||
</script> | ||
|
||
{#if value["type"] == "plotly" || value["type"] == "bokeh" } | ||
<div id="plotDiv" /> | ||
{:else} | ||
<div | ||
class="output-image w-full h-80 flex justify-center items-center dark:bg-gray-600 relative" | ||
{theme} | ||
> | ||
<!-- svelte-ignore a11y-missing-attribute --> | ||
<img class="w-full h-full object-contain" src={value["plot"]} /> | ||
</div> | ||
{/if} | ||
|
||
<style lang="postcss"> | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Component from "./Plot.svelte"; | ||
|
||
export default { | ||
component: Component | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.