Invoking Python subprocess in Streamlit to run Linux script file

I’m a bit turned around on how to execute a shell script file in a Linux environment via Python’s subprocess command in Streamlit. Any assistance on what I’m missing is appreciated.

I’m using a shell script called 0_texts.sh to run Pylanguagetool for a grammar check of one text file and return corrections in another text file, like so:

cd /home/user/dir/TXTs/

pylanguagetool text_0.txt > comments_0.txt

This script runs correctly in the Linux terminal, writing a comments_0.txt with appropriate grammar checks from text_0.txt.

I need to create a Python/Streamlit app that runs these shell scripts. In attempting to run this shell script, I’ve written script.py below:

import os
import subprocess
import sys

subprocess.run(['bash','/home/user/dir/Scripts/0_texts.sh'])

I then run script.py in Streamlit via the code below, keeping with Streamlit’s documentation on using subprocess here.

import streamlit as st
import os
import subprocess
import sys

def app():

    button1 = st.button("Click me")
    
    if button1:

        p = subprocess.run([f"{sys.executable}", "/home/user/dir/pages/script.py"])
        st.write(p)

When I execute the script.py via Streamlit, the 0_txts.sh script executes, writing comments_0.txt in the correct directory and providing the following traceback: CompletedProcess(args=['/usr/bin/python3', '/home/user/dir/pages/script.py'], returncode=0). However, the comments_0.txt output contains the error input file is required, as if it can’t properly access or read text_0.txt. I’ve tinkered around trying to find the problem, but have hit a brick wall.

Any suggestions on what I’m missing, or paths forward? Any help greatly appreciated.

I do this kind of stuff all the time successfully, so I am sure this is a path or working directory problem. I can’t quite see it, though.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.