Giter Club home page Giter Club logo

Comments (3)

abidlabs avatar abidlabs commented on July 23, 2024

Hi @coolboyqu you can achieve something like this using the load() and unload() events of Blocks(). This isn't meant to be a production-grade auth solution, but:

import gradio as gr

logged_in_users = set()

def add(request: gr.Request):
    username = request.username
    print("logged_in_users", logged_in_users)
    if username in logged_in_users:
        return gr.Markdown(f"Only 1 session at a time, {username}")
    logged_in_users.add(username)
    return gr.Markdown(f"Welcome, {username}")
    
def remove(request: gr.Request):
    logged_in_users.pop(request.username)


with gr.Blocks() as demo:
    m = gr.Markdown()
    demo.load(add, None, m)
    demo.unload(remove)
    
demo.launch(auth=[("a", "a"), ("b", "b")])

from gradio.

delonleo avatar delonleo commented on July 23, 2024

Hi @abidlabs

Thank you for the explanation. Would u know if we can use block instead ?

import gradio as gr

logged_in_users = set()

def add(request: gr.Request):
    username = request.username
    print("logged_in_users", logged_in_users)
    if username in logged_in_users:
        with gr.Blocks() as block:
            gr.Markdown(f"Only 1 session at a time, {username}")
        return block
    logged_in_users.add(username)
    with gr.Blocks() as block:
        gr.Markdown(f"Welcome, {username}")
    return block

def remove(request: gr.Request):
    if request.username in logged_in_users:
        logged_in_users.remove(request.username)

with gr.Blocks() as demo:
    demo.load(add, None, gr.Blocks())
    demo.unload(remove)

demo.launch(auth=[("a", "a"), ("b", "b")])
  File "/usr/local/lib/python3.10/dist-packages/gradio/queueing.py", line 532, in process_events
    response = await route_utils.call_process_api(
  File "/usr/local/lib/python3.10/dist-packages/gradio/route_utils.py", line 276, in call_process_api
    output = await app.get_blocks().process_api(
  File "/usr/local/lib/python3.10/dist-packages/gradio/blocks.py", line 1938, in process_api
    data = await self.postprocess_data(block_fn, result["prediction"], state)
  File "/usr/local/lib/python3.10/dist-packages/gradio/blocks.py", line 1739, in postprocess_data
    prediction_value = prediction_value.constructor_args.copy()
  File "/usr/local/lib/python3.10/dist-packages/gradio/blocks.py", line 157, in constructor_args
    return self._constructor_args[0] if self._constructor_args else {}
AttributeError: 'Blocks' object has no attribute '_constructor_args'. Did you mean: 'constructor_args'?

Gradio V4.36.1

from gradio.

freddyaboulton avatar freddyaboulton commented on July 23, 2024

Hi @delonleo - you can't return a gr.Blocks from an event. It has to be a single (or list of) components.

from gradio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.