0

I have a Python script called test.py with lots of functions and loops which outputs some raw data and I'm trying to run it from a webpage.

As an initial step I prepared a very simple code below using flask to see whether my script works or not.

I expected to render the output (some raw data) on the webpage but it seems it doesn't work for me, maybe I don't have a good start for this.

In short, the thing I need is to enter to my server address and see the output of my script.

Yes if every thing is going well I will develop it to get input data too.

from flask import Flask, redirect, url_for, request
import test
app = Flask(__name__)

@app.route('/')

def dynamic_page():
  return test()

if __name__ == '__main__':
  app.run(debug=True)
1
  • import json at the top and return json.dumps(test()) in the dynamic_page function and see if that helps Commented Jun 4, 2019 at 12:02

3 Answers 3

1

Berk, Flask's routes require certain type of responses (strings, tuples, Response instance, etc.) You can read about them here. I'm guessing your test function doesn't return any of those required types. What you can do is json-serialize the result of the test function and return that as a Flask response, and if that isn't possible (because of the kind of value test returns), then simply print it to the console. That way, you're still sure your function call worked. Look at the code below:

from flask import Flask, redirect, url_for, request

import test

# Also import json
import json

app = Flask(__name__)

@app.route('/')

def dynamic_page():
   resp = test()
   try:
      resp = json.dumps(resp)
   except:
      print(resp)
      return "Function executed. Check your terminal"
   else:
      return resp

if __name__ == '__main__':
    app.run(debug=True)
Sign up to request clarification or add additional context in comments.

2 Comments

It still can not run the script (test).
If it doesn't then what are you seeing when you visit the / endpoint? Are you getting an error? If yes, what's the error?
0

What does test exactly do?

It looks like you're trying to call your module when user visits the route, instead, you should call a function.

If you want to execute a specific function when user visits your Flask route, you can do it like this:

import test
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return test.my_function_here()

if __name == '__main__':
    app.run(debug=True)

3 Comments

This assumes that test is a module and not a standalone script. From the description in the question, test is a console script that just outputs some data.
@Rashid'Lee'Ibrahim You're right, my bad!
Yes, test is a scipt including several functions.
0

From what I understand you want to call Python from your browser.

If this is the case you can just send an Ajax GET request to your route and that function will run.

$.ajax({
  url: "/root/example/route/",
  method: "GET",
  success: () => console.log(`Python response: ${response}`)
});

Then add the following route function:

@app.route("/root/example/route/", methods = ["GET"])
def example_route():
  print("This was called from the browser!")
  return "All finished here!"

When you run the JavaScript in your browser Ajax sends a GET request to the Flask route which outputs:

This was called from the browser!

In your terminal, it'll then send:

All finished here!

To the browser where JavaScript will log it to the console.

NOTE: I assume you're using jQuery if not check out youmightnotneedjquery.com.

Good luck.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.