I understand if this is not possible, but is there a way I can log a click on a link within a HTML table. I know I can use a onclick event but can I write this somehow to a csv? For example, can I log it into the console and then retrieve this within Gradio?
Have you look at the Gradio flagging feature? Using Flagging, you can store a dataset on our hub collecting data from user.
What kind of data you want to store? number of clicks?
Iβm wanting to store the url clicked by a user which is an image with a href inside a table. Iβm currently using the gr.Blocks() for the base of the app and the HTML table sits inside a gr.HTML().
Hereβs an example piece of code which is the same as my use case:
import pandas as pd
import gradio as gr
lst = [['<a href = "https://www.google.com/"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Google_Images_2015_logo.svg/1200px-Google_Images_2015_logo.svg.png"></a>']]
df = pd.DataFrame(lst, columns = ['img_code'])
df_html = df.to_html(escape = False, render_links = True, index = False, header = False)
with gr.Blocks() as app:
gr.HTML(df_html)
app.launch(debug=True,
share=False)
In that scenario, you might need to implement a custom tracker and attach it to your links.
Luckily, you can do that by mounting the gradio app within your FastAPI app.
Hereβs a complete demo including syncing the flagged data with a dataset on Hugging Face hub:
Hereβs a simple implementation
import pandas as pd
import gradio as gr
from fastapi import FastAPI, Request
import uvicorn
from pathlib import Path
FLAG_DIR = Path("./flagged_data")
logger = gr.CSVLogger()
logger.setup([gr.Text(label="URL"), gr.Text(label="Host")], FLAG_DIR)
# dummy data
websites = [
["https://www.google.com/"],
["https://www.youtube.com/"],
["https://www.facebook.com/"]
]
# add simple get request for tracking
websites = [
[f"<a href={x[0]} target='_blank' onclick='fetch(\"/track?url={x[0]}\")'>{x[0]}</a>"] for x in websites]
df = pd.DataFrame(websites, columns=['img_code'])
df_html = df.to_html(escape=False, render_links=False,
index=False, header=False)
# create a FastAPI app
app = FastAPI()
# gradio app
with gr.Blocks() as block:
gr.HTML(df_html)
# custom get request handler with params to flag clicks
@ app.get("/track")
async def track(url: str, request: Request):
# host disable for privacy reasons
# host = request.headers.get("host")
logger.flag([url, "ip"])
return {"message": "ok"}
# mount Gradio app to FastAPI app
app = gr.mount_gradio_app(app, block, path="/")
# serve the app
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)