mirror of
https://github.com/gradio-app/gradio.git
synced 2025-01-06 10:25:17 +08:00
state work
This commit is contained in:
parent
04a7199502
commit
651d67a0c6
@ -1609,7 +1609,8 @@ class State(InputComponent):
|
||||
default (Any): the initial value of the state.
|
||||
optional (bool): this parameter is ignored.
|
||||
"""
|
||||
|
||||
warnings.warn("The State input component will be deprecated. Please use the "
|
||||
"new Stateful component.")
|
||||
self.default = default
|
||||
super().__init__(label)
|
||||
|
||||
@ -1621,6 +1622,40 @@ class State(InputComponent):
|
||||
return {
|
||||
"state": {},
|
||||
}
|
||||
|
||||
|
||||
class Stateful(InputComponent):
|
||||
"""
|
||||
Special hidden component that stores state across runs of the interface.
|
||||
Input type: Any
|
||||
Demos: chatbot
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
label: str = None,
|
||||
default: Any = None,
|
||||
optional: bool = False,
|
||||
):
|
||||
"""
|
||||
Parameters:
|
||||
label (str): component name in interface (not used).
|
||||
default (Any): the initial value of the state.
|
||||
optional (bool): this parameter is ignored.
|
||||
"""
|
||||
|
||||
self.default = default
|
||||
super().__init__(label)
|
||||
|
||||
def get_template_context(self):
|
||||
return {"default": self.default, **super().get_template_context()}
|
||||
|
||||
@classmethod
|
||||
def get_shortcut_implementations(cls):
|
||||
return {
|
||||
"stateful": {},
|
||||
}
|
||||
|
||||
|
||||
|
||||
def get_input_instance(iface: Interface):
|
||||
|
@ -1,2 +1,25 @@
|
||||
"""Implements a State class to store state in the backend."""
|
||||
"""Implements a StateHolder class to store state in the backend."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict
|
||||
|
||||
|
||||
class StateHolder:
|
||||
state_dict: Dict[str, Any] = {}
|
||||
|
||||
def __init__(self, id):
|
||||
self.id = id
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
if name == "state":
|
||||
StateHolder.state_dict[self.id] = value
|
||||
else:
|
||||
self.__dict__[name] = value
|
||||
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name == "state":
|
||||
return StateHolder.state_dict.get(self.id, None)
|
||||
else:
|
||||
return self.__dict__[name]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user