GithubHelp home page GithubHelp logo

yoh1496 / streamlit-autorefresh Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kmcgrady/streamlit-autorefresh

0.0 0.0 0.0 11 KB

An autorefresh component for Streamlit apps

License: MIT License

Shell 2.47% Python 69.53% TypeScript 22.44% HTML 5.57%

streamlit-autorefresh's Introduction

Streamlit Autorefresh

Streamlit component to force a refresh without tying up a script.

Overview

Streamlit apps are scripts that a server runs based on interactions. When a user interacts with the web app, the script reruns.

One Streamlit use case is a dashboard or realtime stats application. The purpose is to regularly display stats on some interval. Currently, the best way to support this is an infinite loop in the script, but that is not a great practice and has led to a less desirable developer experience in shutting down scripts and servers.

How does this component help?

This component provides a timer on the frontend to regularly ping the Streamlit server to rerun. This effectively allows the script to successfully execute and finish properly and avoid tying up server resources. It effectively puts a little more work on the user's browser than on the server.

Installation

pip install streamlit-autorefresh

Example Usage

import streamlit as st
from streamlit_autorefresh import st_autorefresh

# Run the autorefresh about every 2000 milliseconds (2 seconds) and stop
# after it's been refreshed 100 times.
count = st_autorefresh(interval=2000, limit=100, key="fizzbuzzcounter")

# The function returns a counter for number of refreshes. This allows the
# ability to make special requests at different intervals based on the count
if count == 0:
    st.write("Count is zero")
elif count % 3 == 0 and count % 5 == 0:
    st.write("FizzBuzz")
elif count % 3 == 0:
    st.write("Fizz")
elif count % 5 == 0:
    st.write("Buzz")
else:
    st.write(f"Count: {count}")

Caveats

This is a rather simplistic implementation and feature requests are welcome!

  • The Frontend timer is not a perfect system, so the refresh interval is a rough estimate. Feel free to adjust the interval to a limit that's practical
  • Just like an infinite loop, a small interval, will constantly ping and make server do more work and should be treated with caution.
  • We recommend a key be added. It can be a string literal, but it will help in maintaining the refresh rate and count.
  • We recommend NOT calling st_autorefresh multiple times in a script. It will effectively create multiple timers and refresh at weird rates. It's best to use one function call and utilize the counter to better adjust different refresh rates

streamlit-autorefresh's People

Contributors

kmcgrady avatar

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.