Skip to content

Commit

Permalink
🦔🇦🇺 ↝ Retrieving planets from supabase tables #16, and then retrievin…
Browse files Browse the repository at this point in the history
…g its TIC stats from Lightkurve in #18
  • Loading branch information
Gizmotronn committed Mar 16, 2023
1 parent 27785b1 commit 1ca6903
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 47 deletions.
17 changes: 17 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from flask import Flask, request, make_response, jsonify, Blueprint
from flask_cors import CORS, cross_origin
from supabase_py import client, create_client

import base64
from io import BytesIO
Expand All @@ -11,6 +13,21 @@
import time

app = Flask(__name__)
CORS(app)

if __name__ == '__main__':
app.run(debug = True)

def createSupabaseClient(): # add to separate file
url: str = os.environ.get("SUPABASE_URL")
key: str = os.environ.get("SUPABASE_ANON_KEY")

url = "https://afwwxlhknelxylrfvexi.supabase.co"
key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImFmd3d4bGhrbmVseHlscmZ2ZXhpIiwicm9sZSI6ImFub24iLCJpYXQiOjE2NjY0MzQ4MTgsImV4cCI6MTk4MjAxMDgxOH0.gk1F8Br9__04cvzqYIeeQ-U08KATiHovAw3r3ofNGAo"

supabase: client = create_client(url, key)

createSupabaseClient()

@app.route('/', methods=['GET', 'POST'])
def index():
Expand Down
90 changes: 43 additions & 47 deletions app/app.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,59 @@
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS, cross_origin
from supabase_py import client, create_client
import os

app = Flask(__name__)
CORS(app)

if __name__ == '__main__':
app.run(debug=True)

app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL')
db = SQLAlchemy(app)
url: str = os.environ.get("SUPABASE_URL")
key: str = os.environ.get("SUPABASE_ANON_KEY")

class Planet(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), unique=True, nullable=False)
content = db.Column(db.String(120), unique=True, nullable=False)
url = "https://afwwxlhknelxylrfvexi.supabase.co"
key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImFmd3d4bGhrbmVseHlscmZ2ZXhpIiwicm9sZSI6ImFub24iLCJpYXQiOjE2NjY0MzQ4MTgsImV4cCI6MTk4MjAxMDgxOH0.gk1F8Br9__04cvzqYIeeQ-U08KATiHovAw3r3ofNGAo"

def __init__(self, title, content):
self.title = title
self.content = content
supabase: client = create_client(url, key)

db.create_all()
# Function to retrieve planet data from supa
def find_all_planets(): # Right now this is just demo data, we'll push this data to Supa from Lightkurve later
data = supabase.table("Planetss").select("*").execute()
return data['data']

# Get a specific planet
@app.route('/planets/<id>', methods=['GET'])
def get_planet(id):
planet = Planet.query.get(id)
del planet.__dict__['_sa_instance_state']
return jsonify(planet.__dict__)
planets = find_all_planets()
print(planets)

# Get & return all planets
@app.route('/planets', methods=['GET'])
# Function to add a new planet to the store/supa
def add_planet_to_DB(title, ticId) -> dict: # See `models/TIC.py`
planet = {
"title": title,
"ticId": ticId,
}
data = supabase.table("Planetss").insert(planet).execute()
planets = find_all_planets()

return data['data']

# GET request -> return all planets in storage/db in JSON format
@app.route('/planets')
def get_planets():
planets = []
for planet in db.session.query(Planet).all():
del planet.__dict__['_sa_instance_state']
planets.append(planet.__dict__)
return jsonify(planets)

# Create a planet
@app.route('/planets', methods=['POST'])
def create_planet():
body = request.get_json()
db.session.add(Planet(body['title'], body['content']))
db.session.commit()
return "planet created"

# Update a specific planet
@app.route('/planets/<id>', methods=['PUT'])
def update_planet(id):
body = request.get_json()
db.session.query(Planet).filter_by(id=id).update(
dict(title=body['title'], content=body['content']))
db.session.commit()
return "planet updated"

# Delete a specific planet
@app.route('/planets/<id>', methods=['DELETE'])
def delete_planet(id):
db.session.query(Planet).filter_by(id=id).delete()
db.session.commit()
return "planet deleted"
planets = find_all_planets()
return jsonify({
'planets': planets, # Then present this on react frontend, port 5000 -> 3000
})

@app.route('/planets/add', methods=['POST'])
def add_planet():
data = request.get_json()
try:
title = data['title']
ticId = data['ticId']
if data:
data = add_planet_to_DB(title, ticId)
planets = find_all_planets()
return jsonify(data), 201
except:
return Response('''{"message": "Bad Request"}''', status=400, mimetype='application/json')
37 changes: 37 additions & 0 deletions database/ansible/generateRadius.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import lightkurve as lk
"""import numpy as np
import pandas as pd
import astropy.units as u
import astropy.io.fits as pf
from glob import glob as glob"""

# from ..app import app

@app.route('/planets/classify', methods=['POST'])
def classify_planet_sector_data():
data = request.get_json()
ticId = data['ticId']

transit_depth = 1 - 0.9989 # this is from the above phase folded figure
R_star = 2.04354 * u.Rsun # this is the radius of the parent star for the specific target. Get value from ExoFOP

def planet_radius (transit_depth, R_star):
r_pl_solar_radius = np.sqrt(transit_depth) * R_star
r_pl_Earth = r_pl_solar_radius.to(u.Rearth).value
print("Radius of the planet: {} Earth radii".format(round(r_pl_Earth, 2)))

planet_radius(transit_depth, R_star)
return planet_radius

@app.route('/planets/classify', methods=['GET'])
def classify_planet_sector_data_radius():
transit_depth = 1 - 0.9989 # this is from the above phase folded figure
R_star = 2.04354 * u.Rsun # this is the radius of the parent star for the specific target. Get value from ExoFOP

def planet_radius (transit_depth, R_star):
r_pl_solar_radius = np.sqrt(transit_depth) * R_star
r_pl_Earth = r_pl_solar_radius.to(u.Rearth).value
print("Radius of the planet: {} Earth radii".format(round(r_pl_Earth, 2)))

planet_radius(transit_depth, R_star)
return planet_radius

0 comments on commit 1ca6903

Please sign in to comment.