Skip to content

Commit

Permalink
🧪🤼‍♂️ ↝ Adding post method to get TIC data, sending it to Deepnote/ju…
Browse files Browse the repository at this point in the history
…pyter for matplotlib manipulations

https://www.notion.so/skinetics/January-Week-3-8dcc344a601842959a025940a90c1cc4#6b761b8a793041e1972ad2ecba5b93f0

NFT integration:
#18
#6
#1
Signal-K/Silfur#28 -> contract calls

Frontend API interaction:
#16
Signal-K/Silfur#29 -> handled with Supabase in Gizmotronn/comments@9e70ab0
Signal-K/Silfur#26
Signal-K/Silfur#25 -> starting to implement proposals board in Next with Supabase, then will build in contract calls for web3/eth interaction
Signal-K/Silfur#24
Signal-K/Silfur#22 -> minting will now be handled by a minimal UI & the API wrapper in Server/thirdweb_handler & moralis_handler
Signal-K/Silfur#21 -> custom post types and fields are in Signal-K/client@4cff28c & Signal-K/client@5ce80a5 & Signal-K/client@c950d66, so we're starting off with building it on postgres before adding Lens interactions
Signal-K/Silfur#3
  • Loading branch information
Gizmotronn committed Jan 20, 2023
1 parent 230fce4 commit 2039fd2
Show file tree
Hide file tree
Showing 10 changed files with 1,103 additions and 12 deletions.
3 changes: 3 additions & 0 deletions Server/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ flask_cors = "*"
thirdweb-sdk = "*"
python-dotenv = "*"
psycopg2 = "*"
matplotlib = "*"
lightkurve = "*"
nbformat = "*"

[dev-packages]
889 changes: 887 additions & 2 deletions Server/Pipfile.lock

Large diffs are not rendered by default.

Binary file added Server/ansible/.tic_classify.py.swp
Binary file not shown.
82 changes: 82 additions & 0 deletions Server/ansible/classify.ipynb

Large diffs are not rendered by default.

Binary file added Server/ansible/lightcurve.fits
Binary file not shown.
79 changes: 79 additions & 0 deletions Server/ansible/tic_classify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from flask import Blueprint, request
from thirdweb import ThirdwebSDK
import matplotlib.pyplot as plt
import lightkurve as lk
from io import BytesIO
import base64
import os
from dotenv import load_dotenv
import psycopg2
from storage3 import create_client

tic_classify = Blueprint('tic_classify', __name__)

# Global variables (anomaly base stats)
tic = ''

# Database setup
load_dotenv()
url = os.getenv("DATABASE_URL")
storage_url = os.getenv("STORAGE_URL")
key = os.getenv("ANON_KEY")
headers = {"apiKey": key, 'Authorization': f"Bearer {key}"}
storage_client = create_client(storage_url, headers, is_async=False)
connection = psycopg2.connect(url)
# PostgreSQL queries -> see more at database/connection.py
CREATE_PLANETS_TABLE = (
"""CREATE TABLE IF NOT EXISTS planets (id SERIAL PRIMARY KEY, user_id INTEGER, temperature REAL, date TIMESTAMP, FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE);"""
)
INSERT_PLANET = (
'INSERT INTO planets (user_id, temperature, date) VALUES (%s, %s, %s);'
)
"""CREATE_TIC_TABLE = ( # Link this with the main planets table -> create a new table with a foreign key ref
#CREATE TABLE IF NOT EXISTS tic (tic_id SERIAL PRIMARY KEY, tic TEXT, planet_id INTEGER, FOREIGN KEY(planet_id) REFERENCES planets(id) ON DELETE CASCADE);
#)"""
CREATE_TIC_TABLE = (
'CREATE TABLE IF NOT EXISTS tic (id SERIAL PRIMARY KEY, tic_id TEXT);'
)
INSERT_TIC = (
'INSERT INTO tic (tic_id) VALUES (%s);'
)

@tic_classify.route('/ticId', methods=['GET', "POST"])
def ticId():
TIC = 'TIC 284475976'
sector_data = lk.search_lightcurve(TIC, author = 'SPOC', sector = 23)

buf = BytesIO()
sector_data.savefig(buf, format='png')
data = base64.b64encode(buf.getbuffer()).decode("ascii") # embed result in html output
return f"<img src='data:image/png;base64,{data}'/>"

@tic_classify.route('/search', methods=['GET', 'POST'])
def search():
data = request.get_json()
tic = data['ticid']

sector_data = lk.search_lightcurve(tic, author = 'SPOC', sector = 18)
# post tic & other request data to https://deepnote.com/workspace/star-sailors-49d2efda-376f-4329-9618-7f871ba16007/project/lightkurvehandler-dca7e16c-429d-42f1-904d-43898efb2321/

# Connect to the database
with connection:
with connection.cursor() as cursor:
cursor.execute(CREATE_PLANETS_TABLE)
cursor.execute(CREATE_TIC_TABLE)
cursor.execute(INSERT_TIC, (tic,))
#tic_id = cursor.fetchone()[0]
# Get the id of inserted row returned, send to Supabase


sector_data = lk.search_lightcurve(tic, author = 'SPOC', sector = 18)
#available_data_all = lk.search_lightcurve(tic, author = 'SPOC')
#select_sectors = available_data_all[0:4]
#lc_collection = select_sectors.download_all()

lc = lk.search_lightcurve(tic, author='SPOC', sector=18).download()
#lc = lc.remove_nans().remove_outliers()
#lc.to_fits(path='lightcurve.fits')

return(storage_client.list_buckets())
19 changes: 12 additions & 7 deletions Server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@
from thirdweb.types import LoginPayload
from thirdweb import ThirdwebSDK
from datetime import datetime, timedelta
import os

"""# Jupyter interaction
import io, os, sys, types # https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Importing%20Notebooks.html
from IPython import get_ipython
from nbformat import read
from IPython.core.interactiveshell import Interactiveshell"""

# Flask blueprints/routes
from auth.moralisHandler import moralis_handler
from auth.thirdwebHandler import thirdweb_handler
from contracts.planetDrop import planet_drop
from database.connection import database_connection
from ansible.tic_classify import tic_classify
#from ansible.classify import lightkurve_handler

app = Flask(__name__)
app.register_blueprint(moralis_handler, url_prefix='/moralis-auth')
app.register_blueprint(thirdweb_handler, url_prefix='/auth')
app.register_blueprint(planet_drop, url_prefix='/planets')
app.register_blueprint(database_connection, url_prefix='/database')
app.register_blueprint(tic_classify, url_prefix='/lightkurve')
#app.register_blueprint(lightkurve__handler, url_prefix='/lightkurve-handle')

@app.route('/')
def index():
return "Hello World"

# Getting proposals route
#@app.route('/proposals')
#def getProposals():
# return classifications;
return "Hello World"
36 changes: 35 additions & 1 deletion Server/contracts/planetDrop.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,38 @@ def create_planet():
sdk = ThirdwebSDK(network)
contract = sdk.get_contract("0x766215a318E2AD1EbdC4D92cF2A3b70CBedeac31")
#data = contract.call("lazyMint", _amount, _baseURIForTokens, _data) (POST data)
# Interaction flow -> https://www.notion.so/skinetics/Sample-Planets-Contract-4c3bdcbca4b9450382f9cc4e72e081f7
# Interaction flow -> https://www.notion.so/skinetics/Sample-Planets-Contract-4c3bdcbca4b9450382f9cc4e72e081f7

"""
# Flask/api routes
@app.route('/planet')
def planet():
return jsonify({'planet' : 'planet'})
@app.post('/select_planet')
def select_planet():
data = request.get_json()
planetId = data['planetId']
planetName = data['planetName']
planetTic = data['planetTic']
sector_data = lk.search_lightcurve(planetTic, author = 'SPOC', sector = 23)
#lc = sector_data.download()
#lc.plot()
return sector_data
# Show planet data on frontend
@app.post('/show_planet') # Can we do some calculation for nft revealing using this (i.e. mint nft after classification)
def show_tic():
lc = sector_data.plot()
return lc
@app.post('/mint-planet')
def mint_planet():
data = request.get_json()
_receiver = data['profileAddress']
_tokenId = data['tokenId']
_quantity = 1
data = contract.call("claim", _receiver, _tokenId, _quantity)
app.run(host='0.0.0.0', port=8080)
"""
3 changes: 2 additions & 1 deletion Server/database/connection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import Blueprint
from flask import Blueprint, request
from dotenv import load_dotenv
import psycopg2
import os

database_connection = Blueprint('database_connection', __name__)

Expand Down
4 changes: 3 additions & 1 deletion Server/docs/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Make sure to initialise the Flask app with `pipenv` and run the command `export FLASK_APP=app.py`

We'll have a simple wrapper on Deepnote that will communicate with the multi-layered (aka file) Flask app here, until Deepnote supports multiple files in a Flask container
We'll have a simple wrapper on Deepnote that will communicate with the multi-layered (aka file) Flask app here, until Deepnote supports multiple files in a Flask container

[Public Deepnote collection](https://deepnote.com/workspace/star-sailors-49d2efda-376f-4329-9618-7f871ba16007/projects/Star-Sailors-Interactions-58526396-6398-4022-8c99-f2bc8aa41e97)

0 comments on commit 2039fd2

Please sign in to comment.