Skip to content

Commit

Permalink
πŸͺƒπŸ‘ ↝ Lightkurve files & stats now being returned to client in #32 #29 #…
Browse files Browse the repository at this point in the history
…28, generating planet images and prepping to upload them to IPFS/supa for #18 for planet pages in #16
  • Loading branch information
Gizmotronn committed Mar 17, 2023
1 parent d3bdb1b commit 2f61f42
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 48 deletions.
Binary file modified .DS_Store
Binary file not shown.
15 changes: 5 additions & 10 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,27 @@
import base64
from io import BytesIO

from .datastore import supabase, find_all_planets, add_planet_to_DB
from .datastore import supabase, find_all_planets, add_planet_to_DB, planets
# from Generator import gen_image # add this to a blueprint

from .views import main
from .classify import classify

from .main import gen_image

# Flask application/container initialisation
app = Flask(__name__)

# Flask blueprints
app.register_blueprint(main, url_prefix='/classify')
app.register_blueprint(classify, url_prefix='/classify')
CORS(app)

if __name__ == '__main__':
for seed in range(0, 10):
gen_image(seed)
app.run(debug = True)


@app.route('/', methods=['GET', 'POST'])
def index():
for seed in range(0, 10):
gen_image(seed)
return 'hello world'

@app.route('/get_image')
def get_image():
#for seed in range(0, 10):
#gen_image(seed)
return send_file('out0.png', mimetype='image/png')
63 changes: 63 additions & 0 deletions classify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from flask import Flask, Blueprint, jsonify, request, Response
from .datastore import supabase, find_all_planets, add_planet_to_DB, planets
# from .app import app
# from views.models import Planet
# from .main import gen_image
# views.py
classify = Blueprint('classify', __name__)

# GET request -> return all planets in storage/db in JSON format
@classify.route('/planets')
def get_planets():
planets = find_all_planets()
return jsonify({
'planets': planets, # Then present this on react frontend, port 5000 -> 3000
})

@classify.route('/planets/<planet_id>')
def find_planet_by_id(planet_id):
for planet in planets:
if planet["id"] == int(planet_id):
return jsonify({
"planet": planet,
})

@classify.route('/planets/add', methods=['POST'])
def add_planet():
data = request.get_json()
try:
title = data['title']
ticId = data['ticId']
# Generate a static image
# for seed in range(0, 10):
# gen_image(seed)
# return send_file('static/out0.png', mimetype='image/png')
# Upload this image to supabase
"""axios.post('https://b4c251b4-c11a-481e-8206-c29934eb75da.deepnoteproject.com/planets/add', {
get image from static/out0.png -> https://deepnote.com/workspace/star-sailors-49d2efda-376f-4329-9618-7f871ba16007/project/Star-Sailors-Light-Curve-Plot-b4c251b4-c11a-481e-8206-c29934eb75da/%2Fstatic%2Fout0.png. This is generated on Deepnote then...permanent URL
Lightkurve -> https://deepnote.com/workspace/star-sailors-49d2efda-376f-4329-9618-7f871ba16007/project/Star-Sailors-Light-Curve-Plot-b4c251b4-c11a-481e-8206-c29934eb75da/%2Fstatic%2Flightcurve.fits
}).then(res => {
set this as the url of the image. Still need to determine how to upload it to Supabase storage (could we link an IPFS when it's generated?)
console.log('res', res.data);
}).catch(err => {
console.log('error in request', err);
})
"""
data = add_planet_to_DB(title, ticId)
return jsonify(data), 201
except:
return Response('''{"message": "Bad Request"}''', status=400, mimetype='application/json')

@classify.route('/generator')
def generate():
# Generate the figure **without using pyplot**.
res = 2048 # see generate blueprint above
"""fig = Figure()
ax = fig.subplots()
ax.plot([1, 2])
# Save it to a temporary buffer.
buf = BytesIO()
fig.savefig(buf, format="png")
# Embed the result in the html output.
data = base64.b64encode(buf.getbuffer()).decode("ascii")
return f"<img src='data:image/png;base64,{data}'/>"""#"
33 changes: 16 additions & 17 deletions views.py β†’ database/classify.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
from flask import Flask, Blueprint, jsonify
from flask import Flask, Blueprint, jsonify, request, Response
from .datastore import supabase, find_all_planets, add_planet_to_DB, planets
# from .app import app
# from views.models import Planet

# views.py
main = Blueprint('main', __name__)

@main.route('/planet', methods=['GET', 'POST'])
def index():
return jsonify(planets)

@main.route('/time')
def get_current_time():
return {'time': time.time()}
classify = Blueprint('classify', __name__)

# GET request -> return all planets in storage/db in JSON format
@main.route('/planets')
@classify.route('/planets')
def get_planets():
planets = find_all_planets()
return jsonify({
'planets': planets, # Then present this on react frontend, port 5000 -> 3000
})

@main.route('/planets/<planet_id>')
@classify.route('/planets/<planet_id>')
def find_planet_by_id(planet_id):
for planet in planets:
if planet["id"] == int(planet_id):
return jsonify({
"planet": planet,
})

@main.route('/planets/add', methods=['POST'])
@classify.route('/planets/add', methods=['POST'])
def add_planet():
data = request.get_json()
try:
title = data['title']
if title:
data = add_planet_to_DB(title)
return jsonify(data), 201
ticId = data['ticId']
# Generate a static image
# for seed in range(0, 10):
# gen_image(seed)
# return send_file('static/out0.png', mimetype='image/png')
# Upload this image to supabase
data = add_planet_to_DB(title, ticId)
return jsonify(data), 201
except:
return Response('''{"message": "Bad Request"}''', status=400, mimetype='application/json')

@main.route('/generator')
@classify.route('/generator')
def generate():
# Generate the figure **without using pyplot**.
res = 2048 # see generate blueprint above
Expand Down
5 changes: 3 additions & 2 deletions database/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ def find_all_planets(): # Right now this is just demo data, we'll push this data
print(planets)

# Function to add a new planet to the store/supa
def add_planet_to_DB(title) -> dict: # See `models/TIC.py`
def add_planet_to_DB(title, ticId) -> dict: # See `models/TIC.py`
planet = {
"title": title,
"ticId": ticId,
}
data = supabase.table("Planets").insert(planet).execute()
data = supabase.table("Planetss").insert(planet).execute() # Planetsss table on "Testing Playground" project

return data['data']
5 changes: 3 additions & 2 deletions datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ def find_all_planets(): # Right now this is just demo data, we'll push this data
print(planets)

# Function to add a new planet to the store/supa
def add_planet_to_DB(title) -> dict: # See `models/TIC.py`
def add_planet_to_DB(title, ticId) -> dict: # See `models/TIC.py`
planet = {
"title": title,
"ticId": ticId,
}
data = supabase.table("Planets").insert(planet).execute()
data = supabase.table("Planetss").insert(planet).execute() # Planetsss table on "Testing Playground" project

return data['data']
Binary file added static/out0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 11 additions & 17 deletions views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
from flask import Flask, Blueprint, jsonify
from flask import Flask, Blueprint, jsonify, request, Response
from .datastore import supabase, find_all_planets, add_planet_to_DB, planets
# from .app import app
# from views.models import Planet

# views.py
main = Blueprint('main', __name__)

@main.route('/planet', methods=['GET', 'POST'])
def index():
return jsonify(planets)

@main.route('/time')
def get_current_time():
return {'time': time.time()}
classify = Blueprint('classify', __name__)

# GET request -> return all planets in storage/db in JSON format
@main.route('/planets')
@classify.route('/planets')
def get_planets():
planets = find_all_planets()
return jsonify({
'planets': planets, # Then present this on react frontend, port 5000 -> 3000
})

@main.route('/planets/<planet_id>')
@classify.route('/planets/<planet_id>')
def find_planet_by_id(planet_id):
for planet in planets:
if planet["id"] == int(planet_id):
return jsonify({
"planet": planet,
})

@main.route('/planets/add', methods=['POST'])
@classify.route('/planets/add', methods=['POST'])
def add_planet():
data = request.get_json()
try:
title = data['title']
if title:
data = add_planet_to_DB(title)
return jsonify(data), 201
ticId = data['ticId']
data = add_planet_to_DB(title, ticId)
return jsonify(data), 201
except:
return Response('''{"message": "Bad Request"}''', status=400, mimetype='application/json')

@main.route('/generator')
@classify.route('/generator')
def generate():
# Generate the figure **without using pyplot**.
res = 2048 # see generate blueprint above
Expand Down

0 comments on commit 2f61f42

Please sign in to comment.