• Stars
    star
    263
  • Rank 155,624 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 4 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ‘€ Track & visualize user interactions with your streamlit app

streamlit-analytics Β πŸ‘€

PyPi

Track & visualize user interactions with your streamlit app.

This is a small extension for the fantastic streamlit framework. With just one line of code, it counts page views, tracks all widget interactions across users, and visualizes the results directly in your browser. Think Google Analytics but for streamlit.

Alpha version, use with care.


🎈 Live Demo 🎈


Installation

pip install streamlit-analytics

How to use it

import streamlit as st
import streamlit_analytics

with streamlit_analytics.track():
    st.text_input("Write something")
    st.button("Click me")

That's it! 🎈 All page views and user inputs are now tracked and counted. Of course, you can also use any other streamlit widget in the with block (both from st. and st.sidebar.).

Note: One thing that doesn't work (yet) is tracking widgets created directly from containers, expanders, or columns (e.g. st.expander().button("foo")). Instead, please use a with statement, e.g. with st.expander(): st.button("foo").

To view the results, open your app like normal and append ?analytics=on to the URL (e.g. http://localhost:8501/?analytics=on). The results are then shown directly below your app (see image above).

More options

  • If you don't want a huge with block, you can also do:

    streamlit_analytics.start_tracking()
    # your streamlit code here
    streamlit_analytics.stop_tracking()
  • You can password-protect your analytics results with:

    streamlit_analytics.track(unsafe_password="test123")
    # or pass the same arg to `stop_tracking`

    The app will then ask for this password before showing any results. Do not choose an important password here, it's not encrypted. If you push your code to Github, you should probably store the password in a .env file (which is in .gitignore) and load it via dotenv.

  • If you don't want the results to get reset after restarting streamlit (e.g. during deployment), you can sync them to a Firestore database. Follow this blogpost to set up the database and pass the key file and collection name:

    streamlit_analytics.track(firestore_key_file="firebase-key.json", firestore_collection_name="counts")
    # or pass the same args to `start_tracking` AND `stop_tracking`
  • You can store analytics results as a json file with:

    streamlit_analytics.track(save_to_json="path/to/file.json")
    # or pass the same arg to `stop_tracking`

    And load with:

    streamlit_analytics.track(load_from_json="path/to/file.json")
    # or pass the same arg to `start_tracking`

    (Thanks to @Uranium2 for implementing loading!)

    You can also combine both args to persist data to a json file. Note that this file might get deleted when doing a fresh deploy on a cloud service. Use Firestore instead for persistence, see above. Also note that load_from_json will fail silently if the JSON file does not exist. Writing to JSON may lead to problems with concurrency if many users access the site at the same time.

TODO

PRs are welcome! If you want to work on any of these things, please open an issue to coordinate.

  • Pass all settings args in start_tracking and not in stop_tracking

  • Do not track default values for selectbox, text_input etc. This can probably be done easily if I switch to using on_change.

  • Track unique users -> best way is to use cookies (e.g. with react-cookies) but this probably requires to show a consent form (could also build this in with react-cookie-consent)

  • Enable tracking on widgets created directly from container, expander, columns

  • Make a demo gif for the readme

  • Persist results after re-starting app (e.g. database or file, but where should this be saved/hosted)

  • Find an easier alternative to Firestore for saving the data

  • Track time the user spent in a session and show as "complete time spent on your app"

  • Implement A/B testing, e.g. by choosing one option for a new user randomly, storing it in session object, and then returning the correct bool value for below, and tracking & visualizing stats separately for both options:

    if streamlit_analytics.split_test("option a", 2):
        st.button("Is this button text better?")
    
    if streamlit_analytics.split_test("option b", 2):
        st.button("...or this one?")
  • Enable tracking to Google Analytics, e.g. via custom component with react-ga. Widget interactions could also be tracked via events.

  • Add a button to reset analytics results (see issue #2, this should probably show another prompt for confirmation, similar to if you delete a Github repo)

More Repositories

1

traingenerator

πŸ§™ A web app to generate template code for machine learning
Python
1,364
star
2

best-of-streamlit

πŸ† A ranked gallery of awesome streamlit apps built by the community
1,139
star
3

awesome-machine-learning-startups-berlin

πŸ€– A curated list of machine learning & artificial intelligence startups in Berlin (Germany)
Python
275
star
4

shape-detection

🟣 Object detection of abstract shapes with neural networks
Jupyter Notebook
218
star
5

cnn-interpretability

πŸ₯ Visualizing Convolutional Networks for MRI-based Diagnosis of Alzheimer’s Disease
Jupyter Notebook
162
star
6

year-on-github

πŸ™ Share your Github stats for 2020 on Twitter
Python
132
star
7

streamlit-image-select

πŸ–ΌοΈ An image select component for Streamlit
Python
103
star
8

components-hub

An automated hub of Streamlit components
Python
79
star
9

streamlit-pills

πŸ’Š A Streamlit component to show clickable pills/badges
TypeScript
77
star
10

fastapi-csv

πŸ—οΈ Create APIs from CSV files within seconds, using fastapi
Python
77
star
11

streamlit-profiler

πŸ„πŸΌ Runtime profiler for Streamlit, powered by pyinstrument
Python
39
star
12

streamlit-theme-generator

πŸ‘©β€πŸŽ¨οΈ Generate beautiful color themes for Streamlit, powered by colormind.io
Jupyter Notebook
16
star
13

timeseries-rnn

⏱️ char-rnn for time series data
Python
13
star
14

traintool

πŸ”§ Train off-the-shelf machine learning models in one line of code
Python
12
star
15

readme-template

My template for Github readmes
10
star
16

streamlit-inspector

πŸ•΅οΈ Streamlit component to inspect Python objects during development
Python
8
star
17

reinforcement-maze

Solving a maze with reinforcement learning
Jupyter Notebook
7
star
18

DiffusionLimitedAggregation

Simulation of 3D cluster formation through diffusion using Cython, NumPy and VPython
Python
7
star
19

evolution-learning

🐣 Code for my master thesis "Biologically Plausible Deep Learning through Neuroevolution"
Jupyter Notebook
6
star
20

awstrainer

πŸ› οΈ Command line tool for machine learning on AWS
Python
4
star
21

airbnb-sanity

🧳 Chrome extension to hide Airbnb listings you don't like
JavaScript
4
star
22

CrisisInYourCity

Web visualization of housing market data during the financial crisis
JavaScript
3
star
23

lstm-biology

Neural networks with LSTM to classify and predict biological cell movement
Jupyter Notebook
3
star
24

awesome-python-utils

⭐ Python packages that make dev life easier
2
star
25

drosophila-dynamics

Investigation of a Drosophila motoneuron model for my bachelor thesis
Jupyter Notebook
1
star
26

streamlit-cache

Little exploration of what st.cache could look like
Python
1
star
27

jrieke.github.io

My personal website
CSS
1
star
28

lightshapes

Party lightshow where animations are projected on cardboard shapes
Python
1
star
29

machine-intelligence

Code for the class "Machine Intelligence 1" at TU Berlin
Jupyter Notebook
1
star
30

stcode

Python
1
star
31

NeuroSim

Simple neuron simulator using NumPy and Matplotlib
Python
1
star
32

ioiometer

Android app to measure voltages on the IOIO board
Java
1
star
33

webb-compare-streamlit

Reproducing the amazing WebbCompare app in Streamlit
Python
1
star