Skip to content

Instantly share code, notes, and snippets.

@HelloMorrisMoss
Created November 3, 2022 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HelloMorrisMoss/9438231ffb9665c75aba0c4551d24937 to your computer and use it in GitHub Desktop.
Save HelloMorrisMoss/9438231ffb9665c75aba0c4551d24937 to your computer and use it in GitHub Desktop.
"""Example flask endpoint for receiving http requests from Ignition (or whatever).
Based on https://github.com/HelloMorrisMoss/mahlo_popup
Python version == 3.8.4
Flask==2.0.2
Flask-RESTful==0.3.9
"""
from flask import Flask
from flask_restful import reqparse, Resource, Api
action_dict = { # shortened actions dictionary
'shrink':
{'debug_message': 'shrinking popup', # this is used in logging
'action_params': {'action': 'shrink'}, # this gets passed to the window
'return_result': ({'popup_result': 'Shrinking popup window.'}, 200), # sent back to post request
'description': 'Shrink the window to button size, no change if already button.', # just documentation atm
},
'show':
{'debug_message': 'showing popup',
'action_params': {'action': 'show'},
'return_result': ({'popup_result': 'Showing popup window.'}, 200),
'description': 'Show the full window if there are defects active, no change if already full.',
}
}
def action_function_dummy(action_data: dict):
"""The actual thing done here is to add the json->dict to a collections.deque for the tkinter
window to check and act on."""
print(f"I don't do anything! Here's my action data: {action_data=}")
class Popup(Resource):
"""This is used by flask-restful to provide end-point functionality."""
def post(self):
parser = reqparse.RequestParser()
parser.add_argument('action', type=str, required=True, help='You must provide a command action.')
data = parser.parse_args()
action_to_take = action_dict.get(data['action']) # get the action info from the dictionary above
if action_to_take is not None: # if the action data matches an action in the dict, do it
action_function_dummy(action_to_take['action_params'])
return action_to_take['return_result']
else:
return {'popup_result': 'No valid request.'}, 400 # or the action requested doesn't match, return that
def get(self):
"""Can also have other methods on the same endpoint."""
raise NotImplementedError('Build something here!')
# instantiate the flask app
app = Flask(__name__)
# add the restful endpoints
api = Api(app)
api.add_resource(Popup, '/popup')
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment