Skip to content

Instantly share code, notes, and snippets.

@JakubAndrysek
Last active September 10, 2024 18:26
Show Gist options
  • Save JakubAndrysek/b5dcb2c55186c6857acbb82072e67534 to your computer and use it in GitHub Desktop.
Save JakubAndrysek/b5dcb2c55186c6857acbb82072e67534 to your computer and use it in GitHub Desktop.
ChatGPT - plugin actions + Python Flask backend

ChatGPT - plugin actions + Python Flask

ChatGPT description:

You will generate HTML code with CSS and JS in one file After each question, call the /generate action to get the number and print the ID

Then answer

Finally, call the /set POST action with number = request.json.get('number') value = request.json.get('value') And send the entire html page code with JS and CSS in one file to the value parameter

Then send the user the link https:///getHtml/ Print the link in text form as well Show also the number ID

from flask import Flask, request, jsonify
import random
app = Flask(__name__)
data = {}
@app.route('/')
def home():
return "Welcome to the ChatGPT API!"
@app.route('/generate', methods=['GET'])
def get_random_number():
random_number = random.randint(1, 1000)
return jsonify({'number': random_number})
@app.route('/set', methods=['POST'])
def set_data():
try:
number = request.json.get('number')
value = request.json.get('value')
if number is not None and value is not None:
data[number] = value
return jsonify({'status': 'success'})
else:
return jsonify({
'status': 'error',
'message': 'Missing number or value'
}), 400
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)}), 500
@app.route('/get/<int:number>', methods=['GET'])
def get_data(number):
if number in data:
return jsonify({'value': data[number]})
else:
return jsonify({'status': 'error', 'message': 'Number not found'}), 404
@app.route('/getHtml/<int:number>', methods=['GET'])
def get_html(number):
if number in data:
return data[number]
else:
return jsonify({'status': 'error', 'message': 'Number not found'}), 404
if __name__ == '__main__':
app.run(debug=True)
openapi: 3.1.0
info:
title: Random API
description: A simple API.
version: 1.0.0
servers:
- url: <URL>
description: Server
paths:
/:
get:
operationId: getHome
summary: Home route with a welcome message.
responses:
'200':
description: Welcome message
content:
text/plain:
schema:
type: string
example: "Welcome to the Random Number API!"
/generate:
get:
operationId: getRandomNumber
summary: Generate a random number between 1 and 1000.
responses:
'200':
description: A JSON object containing a randomly generated number.
content:
application/json:
schema:
type: object
properties:
number:
type: integer
example: 857
/set:
post:
operationId: setData
summary: Set a value for a specific number.
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
number:
type: integer
description: The number to set a value for.
example: 123
value:
type: string
description: The value to associate with the number.
example: "test_value"
responses:
'200':
description: Success response when data is successfully set.
content:
application/json:
schema:
type: object
properties:
status:
type: string
example: success
'400':
description: Error response when 'number' or 'value' is missing.
content:
application/json:
schema:
type: object
properties:
status:
type: string
example: error
message:
type: string
example: "Missing number or value"
'500':
description: Server error response.
content:
application/json:
schema:
type: object
properties:
status:
type: string
example: error
message:
type: string
example: "Internal server error"
/get/{number}:
get:
operationId: getData
summary: Retrieve the value associated with a specific number.
parameters:
- name: number
in: path
required: true
description: The number to retrieve the associated value for.
schema:
type: integer
example: 123
responses:
'200':
description: The value associated with the provided number.
content:
application/json:
schema:
type: object
properties:
value:
type: string
example: "Sample Value"
'404':
description: Error response when the number is not found.
content:
application/json:
schema:
type: object
properties:
status:
type: string
example: error
message:
type: string
example: "Number not found"
/getHtml/{number}:
get:
operationId: getHtml
summary: Retrieve the value associated with a specific number in plain text.
parameters:
- name: number
in: path
required: true
description: The number to retrieve the associated value for.
schema:
type: integer
example: 123
responses:
'200':
description: The value associated with the provided number in plain text.
content:
text/plain:
schema:
type: string
example: "Sample Value"
'404':
description: Error response when the number is not found.
content:
application/json:
schema:
type: object
properties:
status:
type: string
example: error
message:
type: string
example: "Number not found"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment