hi! Trying to run streamlit apps in pants. How can...
# general
b
hi! Trying to run streamlit apps in pants. How can I do it once it can only run through its CLI?
c
Hi @breezy-apple-27122 ๐Ÿ‘‹ I have no idea what you just asked ๐Ÿ˜‰ --so more context needed or hope others know streamlit to fill you in
b
So you add the commands in a python script and then use the cli to run it
c
this is one way to do it.. but it didnโ€™t seem to work fully..
Copy code
diff --git a/requirements.txt b/requirements.txt
index 979167d..1b0ae39 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -4,4 +4,8 @@
 ansicolors==1.1.8
 setuptools>=56.2.0,<57
 types-setuptools>=56.2.0,<58
-pytest==7.1.3
\ No newline at end of file
+pytest==7.1.3
+streamlit
+numpy
+pandas
+
diff --git a/streamlit/BUILD b/streamlit/BUILD
new file mode 100644
index 0000000..57aee72
--- /dev/null
+++ b/streamlit/BUILD
@@ -0,0 +1,2 @@
+python_sources()
+pex_binary(name="bin", script="streamlit", dependencies=["./demo.py"])
diff --git a/streamlit/BUILD~ b/streamlit/BUILD~
new file mode 100644
index 0000000..db46e8d
--- /dev/null
+++ b/streamlit/BUILD~
@@ -0,0 +1 @@
+python_sources()
diff --git a/streamlit/demo.py b/streamlit/demo.py
new file mode 100644
index 0000000..b891ac3
--- /dev/null
+++ b/streamlit/demo.py
@@ -0,0 +1,36 @@
+import streamlit as st
+import pandas as pd
+import numpy as np
+
+st.title('Uber pickups in NYC')
+
+DATE_COLUMN = 'date/time'
+DATA_URL = ('<https://s3-us-west-2.amazonaws.com/>'
+            'streamlit-demo-data/uber-raw-data-sep14.csv.gz')
+
+@st.cache_data
+def load_data(nrows):
+    data = pd.read_csv(DATA_URL, nrows=nrows)
+    lowercase = lambda x: str(x).lower()
+    data.rename(lowercase, axis='columns', inplace=True)
+    data[DATE_COLUMN] = pd.to_datetime(data[DATE_COLUMN])
+    return data
+
+data_load_state = st.text('Loading data...')
+data = load_data(10000)
+data_load_state.text("Done! (using st.cache_data)")
+
+if st.checkbox('Show raw data'):
+    st.subheader('Raw data')
+    st.write(data)
+
+st.subheader('Number of pickups by hour')
+hist_values = np.histogram(data[DATE_COLUMN].dt.hour, bins=24, range=(0,24))[0]
+st.bar_chart(hist_values)
+
+# Some number in the range 0-23
+hour_to_filter = st.slider('hour', 0, 23, 17)
+filtered_data = data[data[DATE_COLUMN].dt.hour == hour_to_filter]
+
+st.subheader('Map of all pickups at %s:00' % hour_to_filter)
+st.map(filtered_data)
diff --git a/streamlit/demo.py~ b/streamlit/demo.py~
new file mode 100644
index 0000000..e69de29
Copy code
$ pants run streamlit:bin -- run streamlit/demo.py
2023-02-24 09:59:45.409 WARNING streamlit.config:
Warning: the config option 'server.enableCORS=false' is not compatible with 'server.enableXsrfProtection=true'.
As a result, 'server.enableCORS' is being overridden to 'true'.

More information:
In order to protect against CSRF attacks, we send a cookie with each request.
To do so, we must specify allowable origins, which places a restriction on
cross-origin resource sharing.

If cross origin resource sharing is required, please disable server.enableXsrfProtection.


  ๐Ÿ‘‹ Welcome to Streamlit!

  If you're one of our development partners or you're interested in getting
  personal technical support or Streamlit updates, please enter your email
  address below. Otherwise, you may leave the field blank.

  Email:

  Privacy Policy:
  As an open source project, we collect usage statistics. We cannot see and do
  not store information contained in Streamlit apps. You can find out more by
  reading our privacy policy at: <https://streamlit.io/privacy-policy>

  If you'd like to opt out of usage statistics, add the following to
  ~/.streamlit/config.toml, creating that file if necessary:

    [browser]
    gatherUsageStats = false

2023-02-24 09:59:56.323 DEBUG   streamlit.web.bootstrap: Setting up signal handler
2023-02-24 09:59:56.323 DEBUG   asyncio: Using selector: KqueueSelector
2023-02-24 09:59:56.324 DEBUG   streamlit.web.server.server: Starting server...
2023-02-24 09:59:56.324 DEBUG   streamlit.web.server.server: Serving static content from the Node dev server
2023-02-24 09:59:56.367 DEBUG   streamlit.web.server.server: Server started on port 8501
2023-02-24 09:59:56.367 DEBUG   streamlit.web.server.server: Server state: State.INITIAL -> State.WAITING_FOR_FIRST_SESSION
2023-02-24 09:59:56.726 DEBUG   git.cmd: Popen(['git', 'version'], cwd=/Users/andreas.stenius/src/github/kaos/example-python, universal_newlines=False, shell=None, istream=None)
2023-02-24 09:59:56.744 DEBUG   git.cmd: Popen(['git', 'version'], cwd=/Users/andreas.stenius/src/github/kaos/example-python, universal_newlines=False, shell=None, istream=None)
2023-02-24 09:59:56.755 DEBUG   git.util: Failed checking if running in CYGWIN due to: FileNotFoundError(2, 'No such file or directory')
2023-02-24 09:59:56.848 DEBUG   git.cmd: Popen(['git', 'version'], cwd=/Users/andreas.stenius/src/github/kaos/example-python, universal_newlines=False, shell=None, istream=None)
2023-02-24 09:59:56.859 DEBUG   git.cmd: Popen(['git', 'rev-parse', '--show-toplevel'], cwd=/Users/andreas.stenius/src/github/kaos/example-python, universal_newlines=False, shell=None, istream=None)

  You can now view your Streamlit app in your browser.

  Local URL: <http://localhost:3000>
  Network URL: <http://10.0.0.62:3000>

  For better performance, install the Watchdog module:

  $ xcode-select --install
  $ pip install watchdog

2023-02-24 09:59:56.893 DEBUG   streamlit.version: Skipping PyPI version check
^C  Stopping...
2023-02-24 10:02:23.039 DEBUG   streamlit.web.server.server: Server state: State.WAITING_FOR_FIRST_SESSION -> State.STOPPING
2023-02-24 10:02:23.040 DEBUG   streamlit.web.server.server: Server state: State.STOPPING -> State.STOPPED
Interrupted by user.
b
thanks! will check anyway! I didn't know about the
script="streamlit"
to use a cli command!
c
b
still lots of things for me to learn. I couldn't find it in examples....
Thanks a lot!
c
youโ€™re welcome. and feel free to keep asking questions. ๐Ÿ‘
in pants 2.16 it will be possible to streamline this even more with providing the
run streamline/demo.py
directly in the BUILD file..
b
I'm still getting issues on running. Don't know why actually
Copy code
rhuan@Rhuans-Laptop python % ./pants run src/streamlit/template:bin -- run streamlit/main.py
pyenv: python3.11: command not found

The `python3.11' command exists in these Python versions:
  3.11.1

Note: See 'pyenv help global' for tips on allowing both
      python2 and python3 to be found.
The issue is that pants can only run on python 3.9 and I want to run streamlit in python 3.11
is v2.15 compatible with python 3.11?
I solved the issue by installing pants through homebrew
but as you said it's not working properly
g
We are on pants 2.15, and this is working using pants cli. Question is, how to run the packaged pex_binary? Seems like it is not possible to point streamlit to the script file within the pex?
p
FWIW, I got my streamlit app working with this config:
Copy code
pex_binary(
    name="dashboard",
    execution_mode="venv",
    script="streamlit",
    args=["run", "ux/dashboard.py"],
    dependencies=["//ux/dashboard.py", "//:root#streamlit"],    
)
And then I can run
./dist/ux/dashboard.pex