OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Running Streamlit scripts through Systemd on a Raspberry Pi

  • Thread starter Thread starter keshmaster81
  • Start date Start date
K

keshmaster81

Guest
I want to run my Streamlit app on boot with my Raspberry Pi to make my web server fully autonomous and accessible on my local network when I plug in the pi. This way, I never have to manually run the Python script through the terminal.

To do this, I’ve gone through a few steps, the first being setting the IP of my pi to be static.

The second step I went through was creating a new script that can run my Streamlit app (a different Python file) from within the new Python script. The code for that is shown below.

Code:
from streamlit.web import cli as stcli
import sys

if __name__ == "__main__":
  script_path = "/home/pi/daqhats/examples/python/mcc128/daqhat_burster_plot.py"
    sys.argv = ["streamlit", "run", script_path]
    stcli.main()

Up to this point, everything works. I am able to execute my Streamlit apps directly through the run_streamlit_scripts.py file in VS code, rather than having to manually type Streamlit run app.py in the terminal.

From here, my intention was to make the run_Streamlit_scripts file execute automatically when the Raspberry Pi is powered on, so I could access the web server without ever having to type manually into the terminal.

To do this, I went through the process of creating a service file in my systemd directory. I want to work with Systemd as from what I’ve read, its the most robust way to run files on startup compared to RC local and autostart.

Code:
[Unit]
Description=Run streamlit scripts automatically
After=network.target

[Service]
ExecStart=/usr/bin/python3 /home/pi/daqhats/examples/python/mcc128/run_streamlit_scripts.py
WorkingDirectory=/home/pi
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target

This is where I am running into issues. What I believe should occur is that after a network connection is established, my run_streamlit_scripts python file should be executed meaning I can access my streamlit server at my static IP address. However this is not what occurs. When checking the status of my service using sudo systemctl status streamlit_app.service, I receive this error log:

Code:
run_streamlit_scripts.service - Run streamlit scripts automatically
     Loaded: loaded (/lib/systemd/system/run_streamlit_scripts.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Mon 2024-06-24 15:47:25 EDT; 3min 13s ago
   Duration: 68ms
    Process: 1037 ExecStart=/usr/bin/python3 /home/pi/daqhats/examples/python/mcc128/run_streamlit_scripts.py (code=exited, status=1/FAILURE)
   Main PID: 1037 (code=exited, status=1/FAILURE)
        CPU: 68ms

Jun 24 15:47:25 raspberrypi systemd[1]: run_streamlit_scripts.service: Scheduled restart job, restart counter is at 5.
Jun 24 15:47:25 raspberrypi systemd[1]: Stopped run_streamlit_scripts.service - Run streamlit scripts automatically.
Jun 24 15:47:25 raspberrypi systemd[1]: run_streamlit_scripts.service: Start request repeated too quickly.
Jun 24 15:47:25 raspberrypi systemd[1]: run_streamlit_scripts.service: Failed with result 'exit-code'.
Jun 24 15:47:25 raspberrypi systemd[1]: Failed to start run_streamlit_scripts.service - Run streamlit scripts automatically.

I’m not sure how to interpret this, any help would be appreciated, thanks!
<p>I want to run my Streamlit app on boot with my Raspberry Pi to make my web server fully autonomous and accessible on my local network when I plug in the pi. This way, I never have to manually run the Python script through the terminal.</p>
<p>To do this, I’ve gone through a few steps, the first being setting the IP of my pi to be static.</p>
<p>The second step I went through was creating a new script that can run my Streamlit app (a different Python file) from within the new Python script. The code for that is shown below.</p>
<pre><code>from streamlit.web import cli as stcli
import sys

if __name__ == "__main__":
script_path = "/home/pi/daqhats/examples/python/mcc128/daqhat_burster_plot.py"
sys.argv = ["streamlit", "run", script_path]
stcli.main()
</code></pre>
<p>Up to this point, everything works. I am able to execute my Streamlit apps directly through the run_streamlit_scripts.py file in VS code, rather than having to manually type Streamlit run app.py in the terminal.</p>
<p>From here, my intention was to make the run_Streamlit_scripts file execute automatically when the Raspberry Pi is powered on, so I could access the web server without ever having to type manually into the terminal.</p>
<p>To do this, I went through the process of creating a service file in my systemd directory. I want to work with Systemd as from what I’ve read, its the most robust way to run files on startup compared to RC local and autostart.</p>
<pre><code>[Unit]
Description=Run streamlit scripts automatically
After=network.target

[Service]
ExecStart=/usr/bin/python3 /home/pi/daqhats/examples/python/mcc128/run_streamlit_scripts.py
WorkingDirectory=/home/pi
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
</code></pre>
<p>This is where I am running into issues. What I believe should occur is that after a network connection is established, my run_streamlit_scripts python file should be executed meaning I can access my streamlit server at my static IP address. However this is not what occurs. When checking the status of my service using sudo systemctl status streamlit_app.service, I receive this error log:</p>
<pre><code>run_streamlit_scripts.service - Run streamlit scripts automatically
Loaded: loaded (/lib/systemd/system/run_streamlit_scripts.service; enabled; preset: enabled)
Active: failed (Result: exit-code) since Mon 2024-06-24 15:47:25 EDT; 3min 13s ago
Duration: 68ms
Process: 1037 ExecStart=/usr/bin/python3 /home/pi/daqhats/examples/python/mcc128/run_streamlit_scripts.py (code=exited, status=1/FAILURE)
Main PID: 1037 (code=exited, status=1/FAILURE)
CPU: 68ms

Jun 24 15:47:25 raspberrypi systemd[1]: run_streamlit_scripts.service: Scheduled restart job, restart counter is at 5.
Jun 24 15:47:25 raspberrypi systemd[1]: Stopped run_streamlit_scripts.service - Run streamlit scripts automatically.
Jun 24 15:47:25 raspberrypi systemd[1]: run_streamlit_scripts.service: Start request repeated too quickly.
Jun 24 15:47:25 raspberrypi systemd[1]: run_streamlit_scripts.service: Failed with result 'exit-code'.
Jun 24 15:47:25 raspberrypi systemd[1]: Failed to start run_streamlit_scripts.service - Run streamlit scripts automatically.
</code></pre>
<p>I’m not sure how to interpret this, any help would be appreciated, thanks!</p>
 

Online statistics

Members online
0
Guests online
2
Total visitors
2
Top