Comments (3)
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.
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.
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)
- Gradio's server memory resources cannot be automatically released HOT 1
- Gr.Microphone uses the microphone at all time even when you're not recording.
- A Bug in Document HOT 3
- website formatting bug - wandB integration page HOT 1
- Add ability to save prompts with chatinterface HOT 11
- Issue with gr.ImageEditor in Gradio Application
- Concurrent users face the mixing of sessions HOT 2
- How to get the value about creating dynamic component HOT 1
- gradio_client with remote HF private instance HOT 2
- Make placeholder a variable in ChatInterface class HOT 1
- Request to install PyCUDA in gradio Demo HOT 1
- Time out error while running gradio app HOT 7
- First time loading performance issue HOT 2
- drop to replace for Image component HOT 2
- Running MusicGen on huggingspace on older version of gradio works, but breaks on the current suggested version HOT 1
- Image component raises ValueError when it is updated with a callable value HOT 2
- Gradio reload mode does not work with Chatbot based app HOT 2
- 🔴 Real-Time Streaming like TextStreamer from Transformers HOT 1
- After upgrading from 3.45.1 to 4.36.1, fn in the button.click event cannot be fired HOT 7
- Unable to get gr.Request in the function with `every` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gradio.