最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to show generated imagechart saved in temp folder in public Gradio app? - Stack Overflow

programmeradmin0浏览0评论

I'm using Gradio with share=True to launch a public app (*.gradio.live).

During the app's execution, I generate charts (e.g., Plotly) and save them as .html files inside a temporary folder like temp/session_id/chart.html.

I want to render those charts in the UI using an <iframe>, but the iframe fails to load the file — it either says:

localhost refused to connect

or

gradio.livefile=chart_xxx.html’s server IP address could not be found.

The .html file exists and is saved correctly. The demo.launch() call includes allowed_paths=["temp/"].


How can I display or link to a saved HTML file (e.g., chart.html) in a public Gradio app launched with share=True?

Is there a correct way to build a public URL to access such a file?

my code -

def bar_chart_generation_func(
    x_column: str,
    y_column: str,
    session_hash,
    data: List[dict] = [{}],
    layout: List[dict] = [{}],
    category: str = "",
    facet_row: str = "",
    facet_col: str = "",
):
    try:
        dir_path = TEMP_DIR / str(session_hash)
        chart_path = f"{dir_path}/chart.html"
        csv_query_path = f"{dir_path}/query.csv"

        df = pd.read_csv(csv_query_path)

        function_args = {"data_frame": df, "x": x_column, "y": y_column}

        if category:
            function_args["color"] = category
        if facet_row:
            function_args["facet_row"] = facet_row
        if facet_col:
            function_args["facet_col"] = facet_col

        initial_graph = px.bar(**function_args)

        fig = initial_graph.to_dict()

        data_dict, layout_dict = llm_chart_data_scrub(data, layout)

        print(data_dict)
        print(layout_dict)

        # Applying stylings and settings generated from LLM
        if layout_dict:
            fig["layout"] = layout_dict

        for key, value in data_dict.items():
            if key not in ["x", "y", "type"]:
                for data_item in fig["data"]:
                    data_item[key] = value

        print(fig)

        pio.write_html(fig, chart_path, full_html=False)

        chart_url = f"{root_url}/gradio_api/file/temp/{session_hash}/chart.html"

        iframe = (
            '<div style=overflow:auto;><iframe\n    scrolling="yes"\n    width="1000px"\n    height="500px"\n    src="'
            + chart_url
            + '"\n    frameborder="0"\n    allowfullscreen\n></iframe>\n</div>'
        )

        return {"reply": iframe}

    except Exception as e:
        print("BAR CHART ERROR")
        print(e)
        reply = f"""There was an error generating the Plotly Bar Chart from {x_column}, {y_column}, {data}, and {layout}
            The error is {e},
            You should probably try again.
            """
        return {"reply": reply}

I have basically a chatbot which reads the question and generate the chart and save in temp folder now question is how to display it to the user on gradio ?

发布评论

评论列表(0)

  1. 暂无评论