-
Notifications
You must be signed in to change notification settings - Fork 1
/
flask-plotly.py
executable file
·105 lines (85 loc) · 2.88 KB
/
flask-plotly.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
from flask import Flask
from datetime import date
# pip install plotly
import plotly.plotly as py
from plotly.graph_objs import *
###### Configuration, read by app.config.from_object ######
BIRTH_YEAR = 1971
## The ollowing is over-ridden below for non-WSGI implementation
LISTEN_IP = '0.0.0.0'
LISTEN_PORT = 5000
CREDS_FILE = '/var/lib/plotly-creds.sec'
###########################################################
app = Flask(__name__)
app.config.from_object(__name__)
def makeIframeString(data):
trace1 = Scatter (
x = data['x'], y = data['y'],
fill='tozeroy', fillcolor="red",
opacity=0.52, name="Amazing Things"
)
layout = Layout(
title='Amazing Things over time',
xaxis=XAxis(
title='My Age',
titlefont=Font(
size=18, color='#7f7f7f'
)
),
yaxis=YAxis(
title='Number of things I find amazing',
titlefont=Font(
size=18, color='#7f7f7f'
)
)
)
data = Data([trace1])
fig = Figure(data=data, layout=layout)
plot_url = py.plot(fig, filename='amazing-things', auto_open=False)
iframe_string = '<iframe id="igraph" style="border:none" src="'
iframe_string = iframe_string+plot_url+'/550/550" width="100%" height="700"></iframe>'
return (iframe_string)
def makeData():
data = {}
data['x'] = data['y'] = []
data['x'] = range(date.today().year - app.config['BIRTH_YEAR'] + 1)
data['y'] = [ i * i * i for i in data['x']]
return (data)
@app.route('/')
def index():
"""
Make a simple index.html page
"""
'''
The file 'plotly-creds.sec' is a text file with your Plot.ly
credentials in it. The first line is your username and the
second line is your secret key.
'''
creds=[]
with open(app.config['CREDS_FILE']) as f:
creds = [x.strip('\n') for x in f.readlines()]
res = py.sign_in(creds[0], creds[1])
html = "<h1>Amazing Things</h1>"
flaskData = makeData()
iFrameString = makeIframeString(flaskData)
html = html + "<hr>\n";
html = html + iFrameString + "\n"
html = html + "<ht>\n";
return html
if __name__ == "__main__":
'''
Configuration setting and reading for when running with Python's/Flask's
internal web server; when running under WSGI this block isn't read.
'''
###### Configuration, read by app.config.from_object ######
LISTEN_IP = '' # '' == localhost
CREDS_FILE = 'plotly-creds.sec' # for local
LISTEN_PORT = 5001
###########################################################
app.config.from_object(__name__)
'''
this is the debugging version
'''
# app.run(host=app.config['LISTEN_IP'],port=app.config['LISTEN_PORT'],debug=True)
app.run(host=app.config['LISTEN_IP'],port=app.config['LISTEN_PORT'])