I created a fairly simple Webapp using Flask. It calculates the current distance of all planets by using Skyfield.
The Server and the Python code work fine when i run them locally in my terminal. Unfortunately, when i deploy them to my Server with FastCGI, the site spits out totally different numbers whenever i hit refresh. I really can't find out what the problem is - can anyone help me?
Current Site: Click
The main Flask code:
#!/usr/bin/env python3.4
from flask import Flask, render_template
import calculations
app = Flask(__name__)
@app.route("/")
def index():
return render_template("main.html", DistList=calculations.DistanceList(), DistRelList=calculations.DistanceRelationList())
@app.route("/sources")
def sources():
return render_template("sources.html")
if __name__=="__main__":
app.run()
The python to calculate the distances:
import numpy
import time
from skyfield.api import load
ts = load.timescale()
planets = load('de421.bsp')
# create dictionary with all data needed. (body, min distance, max distance)
# Planetary Information taken from Nasa Fact Sheets
# (https://nssdc.gsfc.nasa.gov/planetary/factsheet/)
celestialsDict = {
"Mercury": [planets["mercury"], 77342099, 221853642],
"Venus": [planets["venus"], 38297055, 260898687],
"Mars": [planets["mars"], 55650408, 401371087],
"Jupiter": [planets["JUPITER_BARYCENTER"], 588518023, 968047821],
"Saturn": [planets["SATURN_BARYCENTER"], 1195436585, 1658441995],
"Uranus": [planets["URANUS_BARYCENTER"], 2581909650, 3157263061],
"Neptune": [planets["NEPTUNE_BARYCENTER"], 4305875512, 4687350083],
"Pluto": [planets["PLUTO_BARYCENTER"], 4293758085, 7533299975],
"Moon": [planets["Moon"], 362102, 404694]
}
def DistanceToEarth(body):
#return current distance to earth
t = ts.now()
astrometric = planets["earth"].at(t).observe(body)
ra, dec, distance = astrometric.radec()
return float("{:.2f}".format(distance.km))
def DistanceList():
#returns a list of distances of the celestials
DistList = []
for body in celestialsDict:
DistList.append(DistanceToEarth(celestialsDict[body][0]))
return DistList
def DistanceRelation(body):
#returns the relative position in percent (0% = closes to earth, 100% = farthest from earth)
relativePosition = ((DistanceToEarth(celestialsDict[body][0]) - celestialsDict[body][1]) / (celestialsDict[body][2] - celestialsDict[body][1])) * 100
return relativePosition
def DistanceRelationList():
DistRelList = []
for body in celestialsDict:
DistRelList.append(float("{:.2f}".format(DistanceRelation(body))))
return DistRelList
And this is the FCGI-Script to start the server:
#!/usr/bin/env python3.4
RELATIVE_WEB_URL_PATH = '/Howfarismars/02_Layout/Code'
LOCAL_APPLICATION_PATH = os.path.expanduser('~') + '/html/Howfarismars/02_Layout/Code'
import sys
sys.path.insert(0, LOCAL_APPLICATION_PATH)
from flup.server.fcgi import WSGIServer
from app import app
class ScriptNamePatch(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
environ['SCRIPT_NAME'] = RELATIVE_WEB_URL_PATH
return self.app(environ, start_response)
app = ScriptNamePatch(app)
if __name__ == '__main__':
WSGIServer(app).run()