Trying to trigger script when a button is clicked from React UI but it keeps giving no such file or directory

Paarthvi Sharma 15 Reputation points
2023-06-21T20:35:49.2+00:00

How do I specify a script that needs to be triggered when a button is pressed. This code works locally but after deploying it does not and I get the below error :

{"error":"[Errno 2] No such file or directory: '../../scripts/fetchdocs.sh'"}

So my question would be where does these files get stored in azure or what would be correct way to trigger the script:

Fronted snip:

When button is clicked:

const handleTrain = () => {
        fetch('/train', {method : "POST"})
    };
return(
...
...
<div>
 <TrainButton className={styles.commandButton} onClick={handleTrain} />
 </div>
...
)};

Flask application:
(The commented out part is being used when using local flask application which works)

@app.route('/train', methods = ['POST'])
def train():
    try:
        # fetchpath = r'../../scripts/required_script.ps1'
        # subprocess.run(['powershell','-File', fetchpath])
        script_path = r'../../scripts/required_script.sh'
        subprocess.run([script_path])
        return jsonify({"Success" : "Script executed"}), 200
    except Exception as e:
            logging.exception("Exception in /train")
            return jsonify({"error": str(e) }), 500
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,930 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Grmacjon-MSFT 19,151 Reputation points Moderator
    2023-06-28T21:54:33.9833333+00:00

    Hello, @Paarthvi Sharma thanks for the question.

    The error message you are seeing indicates that the script file fetchdocs.sh cannot be found. This could be because the file is not being deployed to the correct location in Azure.

    When you deploy your application to Azure, you need to make sure that all the required files are included in the deployment package. This includes any scripts that your application needs to run.

    One way to include the script file in your deployment package is to add it to your project directory and include it in your deployment package. You can do this by adding the following lines to your requirements.txt file:

    scripts/required_script.sh
    

    This will include the required_script.sh file in your deployment package.

    Once the script file is included in your deployment package, you can modify your Flask application to use the correct path to the script file. You can use the os module to get the absolute path to the script file. Here is an example:

    import os
    
    @app.route('/train', methods = ['POST'])
    def train():
        try:
            script_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'scripts', 'required_script.sh'))
            subprocess.run([script_path])
            return jsonify({"Success" : "Script executed"}), 200
        except Exception as e:
            logging.exception("Exception in /train")
            return jsonify({"error": str(e) }), 500
    

    This code uses the os.path.abspath function to get the absolute path to the script file. The os.path.join function is used to join the path to the script file with the path to the parent directory of the Flask application. This ensures that the correct path to the script file is used, regardless of where the application is deployed.

    hope this helps. Let me know if you have any further questions

    -Grace

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.