-1

I have a Python program that's designed to allow the user to select points on an image. The points that are clicked are saved to a YML file.

I currently have it set up in a way where, when the Python program is called from the server, it executes the Python GUI in a separate window (as it would if you executed it through the command line) and the user input is taken through the command line I used to run the server. I'd like this to all be internal. As in, everything runs through the server if that makes sense.

How would I go about doing this? Is there a library available that makes something like this possible? The code belows shows how the Python program is currently run within the server. If you go the the /test url, it executes in this case.

Hopefully this image will give you an idea of what I'm trying to do. Essentially, I need to image to be open AND interact-able in the server. I don't need the exact code to do this, just an explanation of how I should go about doing it.

- project
    - data (folder that holds output information from pythonGUI.py)
    - app
        - templates
            - index.html
        - routes.py
        - models.py
        - __init__.py
    - pythonGUI.py
    - config.py
    - server.py

routes.py

from app import app
from flask import Flask, request, render_template, session
import pythonGUI

app.secret_key = app.config['SECRET_KEY']

@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html')

@app.route('/test')
def test():
    return pythonGUI.main()

if __name__ == '__main__':
    app.run(host='0.0.0.0', port='8000', debug=True)

pythonGUI.py

from app import app
import cv2

def main():
#Some code here

if __name__ == '__main__':
    main()

If you need more code, please let me know. I'm not sure what would be relevant to show in this case. Thank you

EllisonC
  • 23
  • 5
  • I have read your question but it is not clear to me what your end goal is. Do you want a web application where a user performs clicks and operations in a web browser and receive feedback? If so you need to utilise `flask` more and integrate with Javascript on your browser end – Attack68 Apr 08 '19 at 18:06
  • All that your flask app currently does is provide a fancy way of executing a python script that could easily be called from terminal without flask at all – Attack68 Apr 08 '19 at 18:07
  • Yes, that is exactly what I'm trying to do. Sorry, I'm having trouble describing the question. Yes, though, I need it to take an input (an image), allow the user to do something with it, and then return an output. I'll look further into Flask libraries and javascript. Thank you! – EllisonC Apr 08 '19 at 19:46

1 Answers1

0

At the moment, I'm not sure you need an image library; you might be fine with just Javascript and Flask.

Here's how I would go about it:

There's questions about getting coordinates from a picture using Javascript. After about 10 seconds of Googling, I found this: get image pixel coordinates relative left top corner

Take that output and send it to another route in flask. Something like this:

from flask import request

def image_coordinate_processing(x,y):
    #Do stuff
    return result

@app.route('/_img_coordinates')
def _img_coordinates():
    x = int(request.args.get('x'))
    y = int(request.args.get('y'))

    result = image_coordinate_processing(x,y)

    return 'completed'

In your template, you would send the data like this:

{{url_for('_img_coordinates', x=x, y=y)}}

Obviously this isn't all the code, but should get you in a decent direction.

Sam
  • 541
  • 1
  • 3
  • 10