Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,76 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def process_inputs(file, url):
|
| 10 |
global df_global
|
| 11 |
-
if url:
|
| 12 |
-
df_global = fetch_file_from_url(url)
|
| 13 |
-
elif file:
|
| 14 |
-
df_global = load_data(file)
|
| 15 |
-
else:
|
| 16 |
-
# No file or URL: clear everything
|
| 17 |
-
return (
|
| 18 |
-
pd.DataFrame(),
|
| 19 |
-
gr.Dropdown.update(choices=[], value=None),
|
| 20 |
-
gr.Dropdown.update(choices=[], value=None),
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
all_cols = get_all_columns(df_global)
|
| 24 |
-
numeric_cols = get_numeric_columns(df_global)
|
| 25 |
-
|
| 26 |
-
default_x = all_cols[0] if all_cols else None
|
| 27 |
-
default_y = numeric_cols[0] if numeric_cols else None
|
| 28 |
-
|
| 29 |
-
return (
|
| 30 |
-
df_global.head(),
|
| 31 |
-
gr.Dropdown.update(choices=all_cols, value=default_x),
|
| 32 |
-
gr.Dropdown.update(choices=numeric_cols, value=default_y),
|
| 33 |
-
)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def update_plot(chart_type, x_col, y_col):
|
| 36 |
global df_global
|
| 37 |
if df_global.empty or x_col is None or y_col is None:
|
| 38 |
return None
|
| 39 |
|
| 40 |
try:
|
| 41 |
-
import plotly.express as px
|
| 42 |
if chart_type == "Bar Chart":
|
| 43 |
fig = px.bar(df_global, x=x_col, y=y_col)
|
| 44 |
elif chart_type == "Line Chart":
|
|
@@ -53,42 +85,45 @@ def update_plot(chart_type, x_col, y_col):
|
|
| 53 |
return None
|
| 54 |
return fig
|
| 55 |
except Exception as e:
|
| 56 |
-
print("
|
| 57 |
return None
|
| 58 |
-
|
| 59 |
|
|
|
|
| 60 |
with gr.Blocks() as demo:
|
| 61 |
-
gr.Markdown("
|
| 62 |
|
| 63 |
with gr.Row():
|
| 64 |
-
file_input = gr.File(file_types=[".csv", ".xlsx", ".json"], label="Upload
|
| 65 |
url_input = gr.Textbox(label="Or paste a file URL")
|
| 66 |
-
dataframe_output = gr.Dataframe(label="Data Preview", interactive=False)
|
| 67 |
-
|
| 68 |
-
with gr.Row():
|
| 69 |
-
chart_type = gr.Dropdown(["Bar Chart", "Line Chart", "Scatter Plot", "Pie Chart", "Box Plot"], label="Chart Type")
|
| 70 |
-
x_col = gr.Dropdown(label="X-axis Column")
|
| 71 |
-
y_col = gr.Dropdown(label="Y-axis Column")
|
| 72 |
|
| 73 |
load_button = gr.Button("Load Data")
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
generate_button = gr.Button("Generate Chart")
|
| 76 |
plot_output = gr.Plot(label="Generated Chart")
|
| 77 |
|
|
|
|
| 78 |
load_button.click(
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
)
|
| 83 |
|
| 84 |
generate_button.click(
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
)
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
file_input.change(fn=process_inputs, inputs=file_input, outputs=[dataframe_output, x_col, y_col])
|
| 92 |
-
gr.Button("Generate Chart").click(fn=update_plot, inputs=[chart_type, x_col, y_col], outputs=plot_output)
|
| 93 |
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
import requests
|
| 5 |
+
import io
|
| 6 |
|
| 7 |
+
# Global DataFrame to store loaded data
|
| 8 |
+
df_global = pd.DataFrame()
|
| 9 |
|
| 10 |
+
# Load data from uploaded file
|
| 11 |
+
def load_data(file):
|
| 12 |
+
if file is None:
|
| 13 |
+
return pd.DataFrame()
|
| 14 |
+
ext = file.name.split('.')[-1]
|
| 15 |
+
if ext == 'csv':
|
| 16 |
+
return pd.read_csv(file.name)
|
| 17 |
+
elif ext in ['xls', 'xlsx']:
|
| 18 |
+
return pd.read_excel(file.name)
|
| 19 |
+
elif ext == 'json':
|
| 20 |
+
return pd.read_json(file.name)
|
| 21 |
+
return pd.DataFrame()
|
| 22 |
+
|
| 23 |
+
# Load data from URL
|
| 24 |
+
def load_url_data(url):
|
| 25 |
+
try:
|
| 26 |
+
response = requests.get(url)
|
| 27 |
+
response.raise_for_status()
|
| 28 |
+
if url.endswith(".csv"):
|
| 29 |
+
return pd.read_csv(io.StringIO(response.text))
|
| 30 |
+
elif url.endswith(".json"):
|
| 31 |
+
return pd.read_json(io.StringIO(response.text))
|
| 32 |
+
elif url.endswith(".xlsx") or url.endswith(".xls"):
|
| 33 |
+
return pd.read_excel(io.BytesIO(response.content))
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Error loading from URL: {e}")
|
| 36 |
+
return pd.DataFrame()
|
| 37 |
+
|
| 38 |
+
# Process the uploaded file or URL
|
| 39 |
def process_inputs(file, url):
|
| 40 |
global df_global
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
try:
|
| 43 |
+
if url:
|
| 44 |
+
df = load_url_data(url)
|
| 45 |
+
elif file:
|
| 46 |
+
df = load_data(file)
|
| 47 |
+
else:
|
| 48 |
+
return pd.DataFrame(), gr.update(choices=[], value=None), gr.update(choices=[], value=None)
|
| 49 |
+
|
| 50 |
+
if df.empty:
|
| 51 |
+
return pd.DataFrame(), gr.update(choices=[], value=None), gr.update(choices=[], value=None)
|
| 52 |
+
|
| 53 |
+
df_global = df
|
| 54 |
+
|
| 55 |
+
all_cols = list(df.columns)
|
| 56 |
+
numeric_cols = list(df.select_dtypes(include='number').columns)
|
| 57 |
+
|
| 58 |
+
default_x = all_cols[0] if all_cols else None
|
| 59 |
+
default_y = numeric_cols[0] if numeric_cols else None
|
| 60 |
+
|
| 61 |
+
return df.head(), gr.update(choices=all_cols, value=default_x), gr.update(choices=numeric_cols, value=default_y)
|
| 62 |
+
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print("Error processing inputs:", e)
|
| 65 |
+
return pd.DataFrame(), gr.update(choices=[], value=None), gr.update(choices=[], value=None)
|
| 66 |
+
|
| 67 |
+
# Create the plot
|
| 68 |
def update_plot(chart_type, x_col, y_col):
|
| 69 |
global df_global
|
| 70 |
if df_global.empty or x_col is None or y_col is None:
|
| 71 |
return None
|
| 72 |
|
| 73 |
try:
|
|
|
|
| 74 |
if chart_type == "Bar Chart":
|
| 75 |
fig = px.bar(df_global, x=x_col, y=y_col)
|
| 76 |
elif chart_type == "Line Chart":
|
|
|
|
| 85 |
return None
|
| 86 |
return fig
|
| 87 |
except Exception as e:
|
| 88 |
+
print("Plot error:", e)
|
| 89 |
return None
|
|
|
|
| 90 |
|
| 91 |
+
# Gradio UI
|
| 92 |
with gr.Blocks() as demo:
|
| 93 |
+
gr.Markdown("## 📊 Data Visualization App")
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
+
file_input = gr.File(file_types=[".csv", ".xlsx", ".json"], label="Upload File")
|
| 97 |
url_input = gr.Textbox(label="Or paste a file URL")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
load_button = gr.Button("Load Data")
|
| 100 |
+
|
| 101 |
+
dataframe_output = gr.Dataframe(label="Data Preview", interactive=False)
|
| 102 |
+
|
| 103 |
+
chart_type = gr.Dropdown(
|
| 104 |
+
["Bar Chart", "Line Chart", "Scatter Plot", "Pie Chart", "Box Plot"],
|
| 105 |
+
label="Chart Type"
|
| 106 |
+
)
|
| 107 |
+
x_col = gr.Dropdown(label="X-axis")
|
| 108 |
+
y_col = gr.Dropdown(label="Y-axis")
|
| 109 |
+
|
| 110 |
generate_button = gr.Button("Generate Chart")
|
| 111 |
plot_output = gr.Plot(label="Generated Chart")
|
| 112 |
|
| 113 |
+
# Button functionality
|
| 114 |
load_button.click(
|
| 115 |
+
fn=process_inputs,
|
| 116 |
+
inputs=[file_input, url_input],
|
| 117 |
+
outputs=[dataframe_output, x_col, y_col]
|
| 118 |
)
|
| 119 |
|
| 120 |
generate_button.click(
|
| 121 |
+
fn=update_plot,
|
| 122 |
+
inputs=[chart_type, x_col, y_col],
|
| 123 |
+
outputs=plot_output
|
| 124 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
# Run the app
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
demo.launch()
|
| 129 |
+
|