New Features
Lifespan Tasks
A lifespan task is either a coroutine which runs while the backend server is running and is cancelled when the server stops, or it can be an asynccontextmanager
in which case it will run at server startup until yield
is called, then it will resume as the server is shutting down. In either case, the task is started and stopped during hot reload.
Any keyword arguments passed to app.register_lifespan_task
will be passed to the coroutine/contextmanager. If the coroutine takes a parameter called app
, this will be an instance of the underlying FastAPI object.
import asyncio
from contextlib import asynccontextmanager
def fake_answer_to_everything_ml_model(x: float):
return x * 42
ml_models = {}
@asynccontextmanager
async def setup_model(app: FastAPI):
# Load the ML model
ml_models["answer_to_everything"] = fake_answer_to_everything_ml_model
yield
# Clean up the ML models and release the resources
ml_models.clear()
async def long_running_task(foo, bar):
print(f"Starting {foo} {bar} task")
some_api = SomeApi(foo)
try:
while True:
updates = some_api.poll_for_updates()
other_api.push_changes(updates, bar)
await asyncio.sleep(5) # add some polling delay to avoid running too often
except asyncio.CancelledError:
some_api.close() # clean up the API if needed
print("Task was stopped")
app = rx.App()
app.register_lifespan_task(setup_model)
app.register_lifespan_task(long_running_task, foo=42, bar=os.environ["BAR_PARAM"])
Improvements
- Catch more errors in frontend/backend by @picklelo in #3346
- Use twine environment variables if set by @ericwb in #3353
- add style for orientation=vertical in tabs by @Lendemor in #3332
- Added config for number of gunicorn workers by @Snaipergelka in #3351
- feat: Optionally comparing fields in Var.contains, e.g. on rx.Base based types. by @abulvenz in #3375
Bug Fixes
- fix rx.cond with ComputedVars and use union type by @benedikt-bartscher in #3336
- do not check attribute type for var internals by @benedikt-bartscher in #3357
- [REF-2915] ComponentState subclasses are always treated as mixin by @masenf in #3372
- Fix regression when subclassing a ComponentState subclass.
- set config.deploy_url during AppHarness tests by @benedikt-bartscher in #3359
- [REF-2878] Map fontFamily to fontFamily and --default-font-family by @masenf in #3380
- url quote the str data passed to rx.download by @masenf in #3381
Readme Updates
- update README to use 0.5.0 by @Lendemor in #3313
- #3321: Update Chinese README.md by @seewindcn in #3350
- Update Spanish README.md by @boterop in #3330
- Update Korean README.md by @owlur in #3337
Other Changes
- Add some minimal validation of pyproject.toml by @ericwb in #3339
- Suppress runtime warnings by @picklelo in #3354
New Contributors
Full Changelog: https://github.com/reflex-dev/reflex/compare/v0.5.1..v0.5.2