Hugging Face and Gradio URL Paramiters

I’m developing a project for a space on Hugging Face that uses Gradio. The project first sends the user to Twitter with an API request and after validating they are then retuned to my Hugging Face space with their oauth_verifier and oauth_token in the url header. When running Gradio locally on my machine I was able to use the below code to access the URL headers, however, after uploading the project to Hugging Face the function returns two empty strings (when the parameters are in the URL).

Does anyone know if there is a more reliable way of retrieving the current URL parameters when running on Hugging Face/ when using Gradio?

Any help would be appreciated,

block = gr.Blocks()
def get_oath_headers():
    oauth_verifier = None
    oauth_token = None
    did_find = False
    if hasattr(block, "server"):
        for connection in block.server.server_state.connections:
            if connection.headers != None:
                for header in connection.headers:
                    header = header[1].decode()
                    if "oauth_verifier" in header:
                        oauth_verifier ="oauth_verifier=(.+)", header).group(1)
                        oauth_token ="oauth_token=(.+)&", header).group(1)
                        if oauth_token and oauth_verifier:
                            did_find = True
                if did_find:
    return oauth_verifier, oauth_token

hi @User1342

Currently if you’re deploying via Spaces and Gradio, we have one hack way to get the URL Params, it uses a hidden _js callback to interact with the UI before handling it to predict function. The idea is to grab the and pass it to a hidden JSON state variable.

import gradio as gr

block = gr.Blocks()

def predict(text, url_params):
    return ["Hello " + text + "!!", url_params]

get_window_url_params = """
    function(text_input, url_params) {
        console.log(text_input, url_params);
        const params = new URLSearchParams(;
        url_params = Object.fromEntries(params);
        return [text_input, url_params];
with block:
    url_params = gr.JSON({}, visible=True, label="URL Params")
    text_input = gr.Text(label="Input")
    text_output = gr.Text(label="Output")

    btn = gr.Button("Run"), inputs=[text_input, url_params],
              outputs=[text_output, url_params], _js=get_window_url_params)


test: Gradio Url Params - a Hugging Face Space by radames

Another important issue, our Spaces are served under an iframe on, and currently the URL params are not passed to the child iframe (we have an open PR to fix it). So if you want to use the app you have to use via see example bellow:

via doesn’t work
via it works


Hi @radames! This helped so much, thanks for taking the time to respond and for the such detailed answer. Cheers!

1 Like