import json import gradio as gr import pandas as pd import plotly.express as px from notebook import Notebook # Height of the Tabs Text Area TAB_LINES = 8 # Custom CSS styling custom_css = """ .gradio-container { background-color: #f0f4f8; } .logo { max-width: 300px; margin: 20px auto; display: block; .gr-button { background-color: #4a90e2 !important; } .gr-button:hover { background-color: #3a7bc8 !important; } } """ def read_data(path): with open(path, "r") as f: data = json.load(f) return data data = read_data(path="output.json") def build_chart(fig_data, chart_config): chart_type = chart_config.get("type") if chart_type == "bar": fig = px.bar(fig_data, x='x', y='y', title=chart_config['title'], labels={'y': chart_config['y_axis_label'], 'x': chart_config['x_axis_label']}, color='x', template='plotly_white') if chart_type == "line": fig = px.line(fig_data, x='x', y='y', title=chart_config['title'], labels={'y': chart_config['y_axis_label'], 'x': chart_config['x_axis_label']}, template='plotly_white' ) if chart_type == "pie": fig = px.pie(fig_data, values='y', names='x', title=chart_config['title'], labels={'y': chart_config['y_axis_label']},template='plotly_white', hole=0.3 ) fig.update_traces( textposition='inside', textfont_size=12, ) if chart_type == "hist": fig_data['bin_center'] = (fig_data['bin_start'] + fig_data['bin_end']) / 2 fig = px.bar(fig_data, x='bin_center', y='frequency', title=chart_config['title'], labels={'frequency': chart_config['y_axis_label'], 'bin_center': chart_config['x_axis_label']}, template='plotly_white') fig.update_layout(showlegend=False) return fig def display_data(data_item): body = json.loads(data_item.get("body")) sql_config = body.get("sql_config") sql_query, query_description = sql_config.get("sql_query"), sql_config.get("explanation") table_data = body.get("table_data") table_data = pd.DataFrame(table_data) chart_data = body.get("chart_data") config = body.get("chart_config") chart_type = config.get("type") fig_data = pd.DataFrame(chart_data.get(chart_type).get("data")) fig = build_chart(fig_data=fig_data, chart_config=config) return sql_query, query_description, table_data, fig def export_notebook(): global data notebook = Notebook(data=data) notebook_path = notebook.export_notebook() return notebook_path with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", secondary_hue="indigo")) as demo: gr.Image("logo.png", label=None, show_label=False, container=False, height=100) gr.HTML("""
Automate Export Multiple Tabs to Jupyter Notebook.