0

I am new to python and I'm trying to implement a rest api in python using flask framework. The goal is when the api is called, I want to implement some logic that will execute a python script and return that output as the API response to the client.

Here what I have for my simple rest service

hello.py

from flask import Flask
app = Flask(__name__)


@app.route("/tasks")
def get_tasks():
  return "return json output"

python script that I would like to be called and executed mypythonscript.py

print("My first simple Python script!")

Ultimately, I'll get the script output whenever I call the rest API. Is this possible? any pointers or documentation/resources or examples of something similar would greatly help!

Daniel Gebrahanus
  • 223
  • 2
  • 4
  • 10
  • Do you have to produce the results via a separate script that writes to stdout, as you've shown? If not, a better design might be to create a function that returns a string holding the result. Then call that function when needed from your Flask application. – Matthias Fripp Dec 04 '17 at 20:08
  • Python scripts can import each other. Just make sure that the script to be imported is modularized so that it does not execute once it is imported. See https://stackoverflow.com/questions/6523791/why-is-python-running-my-module-when-i-import-it-and-how-do-i-stop-it – smac89 Dec 04 '17 at 20:09

0 Answers0