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)
Malekai
  • 4,765
  • 5
  • 25
  • 60
Berk
  • 263
  • 1
  • 14

3 Answers3

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)
olumidesan
  • 111
  • 4
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)
vremes
  • 654
  • 2
  • 7
  • 10
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.

Malekai
  • 4,765
  • 5
  • 25
  • 60
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194443/discussion-between-logicalbranch-and-berk). – Malekai Jun 04 '19 at 14:10