Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test/feature tracking stchat input #48

Merged
merged 6 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ verify_ssl = true
name = "pypi"

[packages]
streamlit = ">=1.30"
streamlit = ">=1.31"
altair = "*"
pandas = "*"
google-cloud-firestore = "*"
Expand Down
241 changes: 104 additions & 137 deletions Pipfile.lock

Large diffs are not rendered by default.

20 changes: 17 additions & 3 deletions examples/minimal.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import platform
import sys
import os

# Get the directory of the current script
current_script_path = os.path.dirname(os.path.abspath(__file__))

# Construct the path to the 'src' directory
src_directory_path = os.path.join(current_script_path, '..', 'src')

# Add the 'src' directory to sys.path at the first position
sys.path.insert(0, src_directory_path)

import streamlit as st
import streamlit_analytics2 as streamlit_analytics

Expand All @@ -19,6 +31,8 @@
st.text_input("Write your name")
st.selectbox("Select your favorite", ["cat", "dog", "flower"])
st.button("Click me")


st.title("A [link]()")
prompt = st.chat_input("Send a prompt to the bot")
if prompt:
st.write(f"User has sent the following prompt: {prompt}")

st.title("A [link]()")
5 changes: 4 additions & 1 deletion examples/pages/all-widgets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import streamlit as st
import streamlit_analytics2 as streamlit
import streamlit_analytics2 as streamlit_analytics
from datetime import datetime

with streamlit_analytics.track(verbose=True):
Expand All @@ -19,6 +19,9 @@
st.time_input("time_input")
st.file_uploader("file_uploader")
st.color_picker("color_picker")
prompt = st.chat_input("Send a prompt to the bot")
if prompt:
st.write(f"User has sent the following prompt: {prompt}")

st.sidebar.checkbox("sidebar_checkbox")
st.sidebar.button("sidebar_button")
Expand Down
2 changes: 1 addition & 1 deletion examples/pages/sharing-demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import streamlit as st

try:
import streamlit_analytics2 as streamlit
import streamlit_analytics2 as streamlit_analytics
except ImportError:
# Install streamlit-analytics on first run (not included in requirements.txt).
import subprocess
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "streamlit_analytics2"
version = "0.5.3"
version = "0.6.1"
description = "Track & visualize user interactions with your streamlit app."
authors = [{ name = "444B", email = "[email protected]" }]
license = { file = "LICENSE" }
Expand All @@ -26,7 +26,7 @@ classifiers = [
]
requires-python = ">=3.8"
dependencies = [
"streamlit >= 1.30.0",
"streamlit >= 1.31.0",
"pandas",
"altair",
"google-cloud-firestore"
Expand Down
2 changes: 1 addition & 1 deletion src/streamlit_analytics2/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .main import counts, start_tracking, stop_tracking, track
from .main import counts, start_tracking, stop_tracking, track
65 changes: 65 additions & 0 deletions src/streamlit_analytics2/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def reset_counts():
_orig_time_input = st.time_input
_orig_file_uploader = st.file_uploader
_orig_color_picker = st.color_picker
# new elements, testing
# _orig_download_button = st.download_button
# _orig_link_button = st.link_button
# _orig_page_link = st.page_link
# _orig_toggle = st.toggle
# _orig_camera_input = st.camera_input
_orig_chat_input = st.chat_input

_orig_sidebar_button = st.sidebar.button
_orig_sidebar_checkbox = st.sidebar.checkbox
Expand All @@ -62,6 +69,13 @@ def reset_counts():
_orig_sidebar_time_input = st.sidebar.time_input
_orig_sidebar_file_uploader = st.sidebar.file_uploader
_orig_sidebar_color_picker = st.sidebar.color_picker
# new elements, testing
# _orig_sidebar_download_button = st.sidebar.download_button
# _orig_sidebar_link_button = st.sidebar.link_button
# _orig_sidebar_page_link = st.sidebar.page_link
# _orig_sidebar_toggle = st.sidebar.toggle
# _orig_sidebar_camera_input = st.sidebar.camera_input



def _track_user():
Expand Down Expand Up @@ -225,6 +239,30 @@ def new_func(label, *args, **kwargs):

return new_func

def _wrap_chat_input(func):
"""
Wrap a streamlit function that returns a single value (str/int/float/datetime/...),
e.g. st.slider, st.text_input, st.number_input, st.text_area, st.date_input,
st.time_input, st.color_picker.
"""

def new_func(placeholder, *args, **kwargs):
value = func(placeholder, *args, **kwargs)
if placeholder not in counts["widgets"]:
counts["widgets"][placeholder] = {}

formatted_value = str(value)

if formatted_value not in counts["widgets"][placeholder]:
counts["widgets"][placeholder][formatted_value] = 0

if formatted_value != st.session_state.state_dict.get(placeholder):
counts["widgets"][placeholder][formatted_value] += 1
st.session_state.state_dict[placeholder] = formatted_value
return value

return new_func


def start_tracking(
verbose: bool = False,
Expand Down Expand Up @@ -290,6 +328,13 @@ def start_tracking(
st.time_input = _wrap_value(_orig_time_input)
st.file_uploader = _wrap_file_uploader(_orig_file_uploader)
st.color_picker = _wrap_value(_orig_color_picker)
# new elements, testing
# st.download_button = _wrap_value(_orig_download_button)
# st.link_button = _wrap_value(_orig_link_button)
# st.page_link = _wrap_value(_orig_page_link)
# st.toggle = _wrap_value(_orig_toggle)
# st.camera_input = _wrap_value(_orig_camera_input)
st.chat_input = _wrap_chat_input(_orig_chat_input)

st.sidebar.button = _wrap_button(_orig_sidebar_button)
st.sidebar.checkbox = _wrap_checkbox(_orig_sidebar_checkbox)
Expand All @@ -305,6 +350,13 @@ def start_tracking(
st.sidebar.time_input = _wrap_value(_orig_sidebar_time_input)
st.sidebar.file_uploader = _wrap_file_uploader(_orig_sidebar_file_uploader)
st.sidebar.color_picker = _wrap_value(_orig_sidebar_color_picker)
# new elements, testing
# st.sidebar.download_button = _wrap_value(_orig_sidebar_download_button)
# st.sidebar.link_button = _wrap_value(_orig_sidebar_link_button)
# st.sidebar.page_link = _wrap_value(_orig_sidebar_page_link)
# st.sidebar.toggle = _wrap_value(_orig_sidebar_toggle)
# st.sidebar.camera_input = _wrap_value(_orig_sidebar_camera_input)


# replacements = {
# "button": _wrap_bool,
Expand Down Expand Up @@ -364,6 +416,13 @@ def stop_tracking(
st.time_input = _orig_time_input
st.file_uploader = _orig_file_uploader
st.color_picker = _orig_color_picker
# new elements, testing
# st.download_button = _orig_download_button
# st.link_button = _orig_link_button
# st.page_link = _orig_page_link
# st.toggle = _orig_toggle
# st.camera_input = _orig_camera_input
st.chat_input = _orig_chat_input

st.sidebar.button = _orig_sidebar_button
st.sidebar.checkbox = _orig_sidebar_checkbox
Expand All @@ -379,6 +438,12 @@ def stop_tracking(
st.sidebar.time_input = _orig_sidebar_time_input
st.sidebar.file_uploader = _orig_sidebar_file_uploader
st.sidebar.color_picker = _orig_sidebar_color_picker
# new elements, testing
# st.sidebar.download_button = _orig_sidebar_download_button
# st.sidebar.link_button = _orig_sidebar_link_button
# st.sidebar.page_link = _orig_sidebar_page_link
# st.sidebar.toggle = _orig_sidebar_toggle
# st.sidebar.camera_input = _orig_sidebar_camera_input

# Save count data to firestore.
# TODO: Maybe don't save on every iteration but on regular intervals in a background
Expand Down
Loading