# Dynamically mount a Gradio \`Block\` from within a FastAPI function

**URL:** <https://discuss.huggingface.co/t/dynamically-mount-a-gradio-block-from-within-a-fastapi-function/48983>\
**Category:** 🔒 Gradio\
**Created:** [August 1, 2023, 1:27pm UTC](https://discuss.huggingface.co/t/dynamically-mount-a-gradio-block-from-within-a-fastapi-function/48983 "2023-08-01T13:27:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![at123](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@at123](https://discuss.huggingface.co/u/at123)\
**Post date:** [August 1, 2023, 1:27pm UTC](https://discuss.huggingface.co/t/dynamically-mount-a-gradio-block-from-within-a-fastapi-function/48983/1 "2023-08-01T13:27:42Z")

</div>

I’d like to dynamically create and mount Gradio blocks in a FastAPI app. This doesn’t seem to work too well though…what am I missing? Here’s a minimal example that doesn’t execute as expected:

```python
app = FastAPI()

@app.get('/test')
def test(procId: int):
    with gr.Blocks() as demo:
        chatbot = gr.Chatbot()
        msg = gr.Textbox()
        clear = gr.Button("Clear")

        def user(user_message, history):
            return "", history + [[user_message, None]]
        
        def bot(history):
            bot_message = random.choice(["I really hope this works", "Gradio is fun"])
            history[-1][1] = bot_message

        msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False)\
            .then(bot, chatbot, chatbot)

    global app 
    demo.queue()
    app = gr.mount_gradio_app(app, demo, f'/gradio/{procId}')

    return f'Gradio app mounted at /gradio/{procId}'

if __name__ == ' __main__':
    uvicorn.run('test:app')

```

I can call `/test/?procId=100` and it runs fine. However, when I go to `/gradio/100` the chatbot UI is there, but it doesn’t appear like any of the functionality works. I can type into the message box, but when I press enter the chatbot doesn’t respond with anything. Does this have something to do with `test()` being called on a separate Thread and terminating before I can load the gradio endpoint?

---

<div class="post-metadata">

**Author:** ![at123](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@at123](https://discuss.huggingface.co/u/at123)\
**Post date:** [August 1, 2023, 6:32pm UTC](https://discuss.huggingface.co/t/dynamically-mount-a-gradio-block-from-within-a-fastapi-function/48983/2 "2023-08-01T18:32:10Z")

</div>

Ok it seems like the main problem is with demo.queue(). That is, gradio can’t mount a [streaming output interface](https://www.gradio.app/guides/key-features/#iterative-outputs) without the functionality breaking. I’m hoping there’s a way around this though.

---

<div class="post-metadata">

**Author:** ![freddyaboulton](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/freddyaboulton/32/32732_2.png) [@freddyaboulton](https://discuss.huggingface.co/u/freddyaboulton)\
**Post date:** [August 1, 2023, 7:10pm UTC](https://discuss.huggingface.co/t/dynamically-mount-a-gradio-block-from-within-a-fastapi-function/48983/3 "2023-08-01T19:10:31Z")

</div>

> [@at123](#):
>
> ```auto
> app = FastAPI()
> 
> @app.get('/test')
> def test(procId: int):
> with gr.Blocks() as demo:
> chatbot = gr.Chatbot()
> msg = gr.Textbox()
> clear = gr.Button("Clear")
> 
> def user(user_message, history):
> return "", history + [[user_message, None]]
>         
> def bot(history):
> bot_message = random.choice(["I really hope this works", "Gradio is fun"])
> history[-1][1] = bot_message
> 
> msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False)\
> .then(bot, chatbot, chatbot)
> 
> global app 
> demo.queue()
> app = gr.mount_gradio_app(app, demo, f'/gradio/{procId}')
> 
> return f'Gradio app mounted at /gradio/{procId}'
> 
> if __name__ == ' __main__':
> uvicorn.run('test:app')
> 
> ```

What version are you using? I get a different error when running your code
