def check_session_id_signature(session_id, secret_key=settings.secret_key_bytes(), signed=settings.sign_sessions()): """Check the signature of a session ID, returning True if it's valid. The server uses this function to check whether a session ID was generated with the correct secret key. If signed sessions are disabled, this function always returns True. Args: session_id (str) : The session ID to check secret_key (str, optional) : Secret key (default: value of 'BOKEH_SECRET_KEY' env var) signed (bool, optional) : Whether to check anything (default: value of 'BOKEH_SIGN_SESSIONS' env var) """ secret_key = _ensure_bytes(secret_key) if signed: pieces = session_id.split('-', 1) if len(pieces) != 2: return False base_id = pieces[0] provided_signature = pieces[1] expected_signature = _signature(base_id, secret_key) # hmac.compare_digest() uses a string compare algorithm that doesn't # short-circuit so we don't allow timing analysis # encode_utf8 is used to ensure that strings have same encoding return hmac.compare_digest(encode_utf8(expected_signature), encode_utf8(provided_signature)) else: return True
def P(): if request.method == 'GET': html = render_template( 'projections.html', total_disp=1, NS_disp=1, ) return encode_utf8(html) else: app.vars['agep'] = request.form['agep'] app.vars['bwp'] = request.form['bwp'] app.vars['totalp'] = request.form['totalp'] if app.vars['agep'] == 'Ruben Olmedo': html = render_template( 'projections.html', total_disp='A new WR of 305.5', NS_disp='OVER 9000!!!!', ) return encode_utf8(html) else: if app.vars['agep']== '' or app.vars['bwp'] == '' or app.vars['totalp']== '': html = render_template( 'projections.html', total_disp=1, NS_disp=1, ) return encode_utf8(html) else: agep = float(app.vars['agep']) bwp = float(app.vars['bwp']) totalp = float(app.vars['totalp']) #NSp = round(-approx(bwp)+totalp, 2) #for index, row in recent_data.iterrows(): #if abs(row['Age']-agep)<=2 and abs(row['NSC']-NSp)<=2: #comparison = (row['LC'],row['CC'],row['Birth'],row['Year'],row['Year']+1) comparison = (5933, 15, 2016-agep, agep,2016) NSp2 = nei_model_pred.predict(comparison) NSp = round(-approx(bwp)+totalp, 2) NSp3 = round(NSp2[0],2) total_disp = NSp-NSp3+totalp html = render_template( 'projections.html', total_disp=total_disp, NS_disp=NSp3, ) return encode_utf8(html)
def plot_titles(): session_id = request.args.get("session_id") df_file = get_sess(session_id)['df_file'] df = load_csv(session_id) ind = indeed_scrape.Indeed("kw") df['jobtitle'] = df['jobtitle'].apply(lambda x: x.lower()) df['jobtitle'] = df['jobtitle'].apply(lambda x:' '.join(ind._split_on_spaces(x))) df['jobtitle'] = df['jobtitle'].apply(lambda x: re.sub('[^a-z\d\s]', '', x)) num = df.shape[0] title_de_duped = ind.summary_similarity(df, 'jobtitle', 80) grp = title_de_duped.groupby("jobtitle").count().sort("url", ascending=False) cities = grp.index.tolist() counts = grp['city'] # any key will do here dff = pd.DataFrame({'kw':cities, 'count':counts }) kws = get_sess(session_id)['keyword'][0] p = plot_fig(dff, num, kws) script, div = components(p) session_string = "?session_id=%s" % session_id page = render_template('titles.html', div=div, script=script, session_id=session_string) return encode_utf8(page)
def graph(): # request was a POST StockTicker = request.form['stock'] Type = request.form['type'] api_url = 'https://www.quandl.com/api/v1/datasets/WIKI/%s.json' % StockTicker session = requests.Session() session.mount('http://', requests.adapters.HTTPAdapter(max_retries=3)) raw_data = session.get(api_url) json_dat = raw_data.json() data = json_dat["data"] df = pd.DataFrame(data, columns= json_dat["column_names"]) df['Date'] = pd.to_datetime(df['Date']) df = df[df["Date"] >= start] p1 = figure(x_axis_type = "datetime") p1.title = "Stock Closing Prices" p1.grid.grid_line_alpha=0.3 p1.xaxis.axis_label = 'Date' p1.yaxis.axis_label = 'Price' p1.line(df["Date"], df[Type], color='#A6CEE3', legend=StockTicker) # For more details see: # http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components script, div = components(p1, INLINE) return encode_utf8(render_template('graph.html', script=script, div=div))
def graph(): df = app.vars['data'] # Create a line plot from our data. p = figure(width=700, height=500, x_axis_type="datetime", title="Data from Quandle WIKI set") for category in app.vars['features']: p.line(pd.to_datetime(df['Date']), df[category], color=app.vars['color'][category], line_width=1, legend=app.vars['ticker'] + ": " + category) p.legend.orientation = "top_right" # Configure resources to include BokehJS inline in the document. plot_resources = RESOURCES.render( js_raw=INLINE.js_raw, css_raw=INLINE.css_raw, js_files=INLINE.js_files, css_files=INLINE.css_files, ) script, div = components(p, INLINE) html = render_template( 'graph.html', ticker=app.vars['ticker'], plot_script=script, plot_div=div, plot_resources=plot_resources ) return encode_utf8(html)
def plot_data(): # get data x = [] y = [] colors = [] i = 0 with open("time_dep.csv", "r") as f: while True: line = f.readline() if len(line) == 0: break items = line.split(',') x.append(float(items[0])) y.append(float(items[1])) fig = figure(title="Taxi trips versus Day", plot_width=600, plot_height=400, x_axis_type="datetime", toolbar_location="below") fig.line(x, y) fig.xaxis[0].formatter = DatetimeTickFormatter(formats=dict(days=["%a"])) fig.yaxis.axis_label = 'Number of Trips' script, div = components(fig, INLINE) html = flask.render_template( 'layout.html', #'layout.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def graph(): app.symbol = request.form['symbol'] try: graph_html = build_graph(str(app.symbol)) return encode_utf8(graph_html) except KeyError: return render_template('error.html')
def get_data(): if request.method == "POST": session_id = mk_random_string() logging.info("session id: %s" % session_id) type_ = request.form['type_'] kws = request.form['kw'].lower() kws = indeed_scrape.Indeed._split_on_spaces(kws) kws = " ".join(kws) #enter into DB normalized # key used for sql to recover other info to_sql({'session_id':session_id, 'type_':type_, 'keyword':kws, 'ind':0, 'end':0, 'count_thres':50 }) logging.info("running get_data:%s" % time.strftime("%d-%m-%Y:%H:%M:%S")) logging.info("type:%s" % type_) logging.info("key words:%s" % kws) session_string = "?session_id=%s" % session_id html = render_template('output.html', session_id=session_string) return encode_utf8(html)
def plot_cities(): session_id = request.args.get("session_id") logging.info("session id:%s" % session_id) df = load_csv(session_id) count = df.groupby("city").count()['url'] cities = count.index.tolist() num_posts = df.shape[0] df_city = pd.DataFrame({'kw':cities, 'count':count}) df_city.sort('count', ascending=False, inplace=True) df_city.reset_index(inplace=True) # hack df_city[df_city['count'] == 0] = None df_city.dropna(inplace=True) n = df_city.shape[0] if n > 20: end = 20 else: end = n kws = get_sess(session_id)['keyword'][0] slice = df_city.loc[0:end, :].copy() p = plot_fig(slice, num_posts, kws) script, div = components(p) session_string = "?session_id=%s" % session_id page = render_template('cities.html', div=div, script=script, session_id=session_string) return encode_utf8(page)
def polynomial(): """ Very simple embeding of a polynomial chart """ # Grab input arguments from the URL # This is automated by the button args = flask.request.args # Get all the form arguments in their url with defaults color = colors[getitem(args,'color','Black')] fig = figure(width=800, height=350,x_axis_type='datetime', title='%s closing prices for last 30 days' % st) fig.line(dates, closing_prices, line_width=2, color=color, alpha=0.2, legend='close') fig.xaxis.axis_label = 'Date' fig.yaxis.axis_label = 'Price' # Configure resources to include BokehJS inline in the document. plot_resources = RESOURCES.render( js_raw = INLINE.js_raw, css_raw = INLINE.css_raw, js_files = INLINE.js_files, css_files = INLINE.css_files ) script, div = components(fig, INLINE) html = flask.render_template( 'embed.html', plot_script = script, plot_div=div, plot_resources=plot_resources, color=color ) return encode_utf8(html)
def set(self, key, val): _data = self.shelve_module.open('bokeh.server') try: key = encode_utf8(key) _data[key] = json.dumps(val) finally: _data.close()
def plot_cities(): session_id = request.args.get("session_id") logging.info("session id:%s" % session_id) df = load_csv(session_id) count = df.groupby("city").count()['url'] cities = count.index.tolist() num_posts = df.shape[0] df_city = pd.DataFrame({'kw':cities, 'count':count}) df_city.sort('count', ascending=False, inplace=True) df_city.reset_index(inplace=True) n = df_city.shape[0] if n > 20: end = 20 else: end = n p = plot_fig(df_city.loc[0:end,:], num_posts, 'Posts Per City in the Analysis:Top 20') script, div = components(p) session_string = "?session_id=%s" % session_id page = render_template('cities.html', div=div, script=script, session_id=session_string) return encode_utf8(page)
def plot_data(): """Obtains post data and produces two graphs: volume of posts mentioning 'dengue' versus time; and volume of posts versus neighborhood """ (df, df_volume, df_by_location) = get_data() script1, div1 = volumeGraph(df_volume) script2, div2 = locationGraph(df_by_location) plot_resources = RESOURCES.render( js_raw=INLINE.js_raw, css_raw=INLINE.css_raw, js_files=INLINE.js_files, css_files=INLINE.css_files, ) html = flask.render_template( 'embed.html', plot_script1=script1, plot_div1=div1, plot_script2=script2, plot_div2=div2, plot_resources=plot_resources ) return encode_utf8(html)
def plot_titles(): session_id = request.args.get("session_id") df_file = get_sess(session_id)['df_file'] df = load_csv(session_id) ind = indeed_scrape.Indeed("kw") df['jobtitle'] = df['jobtitle'].apply(lambda x: x.lower()) df['jobtitle'] = df['jobtitle'].apply(lambda x:' '.join(ind._split_on_spaces(x))) df['jobtitle'] = df['jobtitle'].apply(lambda x: re.sub('[^a-z\d\s]', '', x)) title_de_duped = ind.summary_similarity(df, 'jobtitle', 80) grp = title_de_duped.groupby("jobtitle").count().sort("url", ascending=False) cities = grp.index.tolist() counts = grp['city'] # any key will do here row = u'<tr><td>%s</td><td>%s</td></tr>' rows = u'' for cty, cnt in zip(cities, counts): try: cty = cty.encode("utf-8", "ignore") rows += row % (cty, cnt) except: continue session_string = "?session_id=%s" % session_id page = render_template('titles.html', rows=rows, session_id=session_string) return encode_utf8(page)
def get(self, *args, **kwargs): session = yield self.get_session() element_id = self.get_argument("bokeh-autoload-element", default=None) if not element_id: self.send_error(status_code=400, reason='No bokeh-autoload-element query parameter') return resources = self.application.resources(self.request) websocket_url = self.application.websocket_url_for_request(self.request, self.bokeh_websocket_path) # TODO: yes, this should resuse code from bokeh.embed more directly render_items = [dict(sessionid=session.id, elementid=element_id, use_for_title=True)] script = _script_for_render_items(None, render_items, websocket_url=websocket_url, wrap_script=False) js = AUTOLOAD_JS.render( js_urls = resources.js_files, css_urls = resources.css_files, js_raw = resources.js_raw + [script], css_raw = resources.css_raw_str, elementid = element_id, ) self.set_header("Content-Type", 'application/javascript') self.write(encode_utf8(js))
def plot_data(): # get data x = [] y = [] colors = [] i = 0 with open("data.csv", "r") as f: while True: line = f.readline() if len(line) == 0: break if i == 6: break items = line.split(',') x.append(float(items[0])) y.append(float(items[1])) #z = int(items[2][:-1]) colors.append("#%02x%02x%02x" % (255, 0, 0)) i += 1 fig = figure(title="Data", plot_width=600, plot_height=400, toolbar_location="below") fig.scatter(x, y, radius=0.1, fill_color=colors, fill_alpha=0.6, line_color=None) script, div = components(fig, INLINE) html = flask.render_template( 'figure.html', #'layout.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def extract_zip(zip_name, exclude_term=None): """Extracts a zip file to its containing directory.""" zip_dir = os.path.dirname(os.path.abspath(zip_name)) try: with zipfile.ZipFile(zip_name) as z: # write each zipped file out if it isn't a directory files = [zip_file for zip_file in z.namelist() if not zip_file.endswith('/')] print('Extracting %i files from %r.' % (len(files), zip_name)) for zip_file in files: # remove any provided extra directory term from zip file if exclude_term: dest_file = zip_file.replace(exclude_term, '') else: dest_file = zip_file dest_file = os.path.normpath(os.path.join(zip_dir, dest_file)) dest_dir = os.path.dirname(dest_file) # make directory if it does not exist if not os.path.isdir(dest_dir): os.makedirs(dest_dir) # read file from zip, then write to new directory data = z.read(zip_file) with open(dest_file, 'wb') as f: f.write(encode_utf8(data)) except zipfile.error as e: print("Bad zipfile (%r): %s" % (zip_name, e)) raise e
def polynomial(): """ Very simple embedding of a polynomial chart""" # Grab the inputs arguments from the URL # This is automated by the button args = flask.request.args # Get all the form arguments in the url with defaults color = colors[getitem(args, 'color', 'Black')] _from = int(getitem(args, '_from', 0)) to = int(getitem(args, 'to', 10)) # Create a polynomial line graph x = list(range(_from, to + 1)) fig = figure(title="Polynomial") fig.line(x, [i ** 2 for i in x], color=color, line_width=2) # Configure resources to include BokehJS inline in the document. # For more details see: # http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#module-bokeh.resources plot_resources = RESOURCES.render( js_raw=INLINE.js_raw, css_raw=INLINE.css_raw, js_files=INLINE.js_files, css_files=INLINE.css_files, ) # For more details see: # http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components script, div = components(fig, INLINE) html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, plot_resources=plot_resources, color=color, _from=_from, to=to ) return encode_utf8(html)
def second_stock(): n = app_stock.vars['name'] ss = "WIKI/" + n + ".4" mydata = quandl.get(ss, encoding='latin1', parse_dates=['Date'], dayfirst=True, index_col='Date', trim_start="2016-05-05", trim_end="2016-06-05", returns = "numpy", authtoken="ZemsPswo-xM16GFxuKP2") mydata = pd.DataFrame(mydata) #mydata['Date'] = mydata['Date'].astype('datetime64[ns]') x = mydata['Date'] y = mydata['Close'] p = figure(title="Stock close price", x_axis_label='Date', y_axis_label='close price', plot_height = 300, plot_width = 550) p.line(x, y, legend="Price in USD", line_width=3, color = "#2222aa") # Configure resources to include BokehJS inline in the document. # For more details see: # http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh-embed js_resources = INLINE.render_js() css_resources = INLINE.render_css() # For more details see: # http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components script, div = components(p, INLINE) html = flask.render_template( 'stockgraph.html', ticker = app_stock.vars['name'], plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def polynomial(): """ Very simple embedding of a polynomial chart """ # Grab the inputs arguments from the URL args = flask.request.args # Get all the form arguments in the url with defaults color = getitem(args, 'color', 'Black') _from = int(getitem(args, '_from', 0)) to = int(getitem(args, 'to', 10)) # Create a polynomial line graph with those arguments x = list(range(_from, to + 1)) fig = figure(title="Polynomial") fig.line(x, [i ** 2 for i in x], color=colors[color], line_width=2) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig) html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, color=color, _from=_from, to=to ) return encode_utf8(html)
def graphstocks(ssymbol): """ Very simple embedding of a polynomial chart""" # Grab the inputs arguments from the URL # This is automated by the button args = ["ADAM"] args = request.args # Get all the form arguments in the url with defaults color = colors[getitem(args, 'color', 'Black')] _from = int(getitem(args, '_from', 0)) to = int(getitem(args, 'to', 10)) # Create a polynomial line graph #x = list(range(_from, to + 1)) #fig = figure(title="Polynomial") #fig.line(x, [i ** 2 for i in x], color=color, line_width=2) AAPL= pd.read_csv("https://ichart.yahoo.com/table.csv?s="+ssymbol+"&a=0&b=1&c=2000&d=0&e=1&f=2010",parse_dates=['Date']) data = dict(AAPL=AAPL['Adj Close'], Date=AAPL['Date']) tsline = TimeSeries(data,x='Date', y='AAPL', ylabel='Stock Prices', legend=True) #tsline=TimeSeries(data,x='Date', y=['AAPL'], color=['AAPL'], dash=['AAPL'], # title="Timeseries", ylabel = 'Stock Prices', legend=True) # tspoint=TimeSeries(data,x='Date',y=[ssymbol], dash=[ssymbol],title="Timeseries",ylabel='Stock Prices', legend=True) output_file("timeseries.html") fig=vplot(tsline) # Configure resources to include BokehJS inline in the document. # For more details see: # http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh-embed js_resources = JS_RESOURCES.render( js_raw=INLINE.js_raw, js_files=INLINE.js_files ) css_resources = CSS_RESOURCES.render( css_raw=INLINE.css_raw, css_files=INLINE.css_files ) #from: http://bokeh.pydata.org/en/0.11.1/docs/releases/0.11.0.html # Before: #html = file_html(layout, None, title=title, template=template, js_resources=js_resources, css_resources=css_resources) #v0.11: # #html = file_html(layout, resources=(js_resources, css_resources), title=title, template=template) # For more details see: # http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components script, div = components(fig, INLINE) html = render_template( 'embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, color=color, _from=_from, to=to ) return encode_utf8(html)
def get(self, key): _data = shelve.open("bokeh.server") key = encode_utf8(key) data = _data.get(key, None) if data is None: return None attrs = json.loads(decode_utf8(data)) _data.close() return attrs
def model(): pTOOLS = "crosshair,hover,pan,box_zoom,reset,save" p2 = figure(tools=pTOOLS,background_fill="#dbe0eb", x_range = (0,100), y_range =(80,100), x_axis_label='Decision Boundary (score)', y_axis_label='Precision', title='Recall and Precision', plot_height=600, plot_width=800) # Setting the second y axis range name and range p2.extra_y_ranges = {"foo": Range1d(start=-5, end=105)} # Adding the second axis to the plot. p2.add_layout(LinearAxis(y_range_name="foo",axis_label="Recall", axis_label_text_color = "red", major_label_text_color = "red"), 'right') source1 = ColumnDataSource( data=dict(precision = crit_r_c[1]*100, recall = crit_r_c[2]*100.0/153546, risk = 100 - crit_r_c[1]*100, miss = 100- crit_r_c[2]*100.0/153546)) source2 = ColumnDataSource( data=dict(precision = crit_r_c[1]*100, recall = crit_r_c[2]*100.0/153546, risk = 100 - crit_r_c[1]*100, miss = 100- crit_r_c[2]*100.0/153546)) p2.line(crit_r_c[0],crit_r_c[1]*100,line_color="blue", line_width=20, alpha=0.7,source=source1) p2.line(crit_r_c[0],crit_r_c[2]*100.0/153546,line_color="red", line_width=20, alpha=0.7,y_range_name="foo",source=source2) hover = p2.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ('Decison Boundary (score)', '$x'), ("Precision (%)","@precision"), ("Risk (%)", "@risk"), ("Recall (%)", "@recall"), ("Missed Opportunity (%)", "@miss"), ]) js_resources2 = JS_RESOURCES.render( js_raw=INLINE.js_raw, js_files=INLINE.js_files) css_resources2 = CSS_RESOURCES.render( css_raw=INLINE.css_raw, css_files=INLINE.css_files) p2script, p2div = components(p2, INLINE) html = flask.render_template( 'model.html', plot_script2=p2script, plot_div2=p2div, js_resources=js_resources2, css_resources=css_resources2) return encode_utf8(html)
def root(): html = flask.render_template( 'layout.html', # plot_script=script, # plot_div=div, # plot_div2=div2, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def get(self, key): _data = self.shelve_module.open('bokeh.server') try: key = encode_utf8(key) data = _data.get(key, None) if data is None: return None attrs = json.loads(decode_utf8(data)) finally: _data.close() return attrs
def root(): global plot_scripts html = flask.render_template( 'layout.html', plot_script=plot_scripts[0], plot_div=plot_scripts[1], plot_div2=plot_scripts[2], js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def stock(): """ Very simple plot of stock closing price for last month or so""" # Grab the inputs arguments from the URL # This is automated by the button args = flask.request.args return 'ALA' # Get all the form arguments in the url with defaults if 'company' in args.keys() and args['company']: company = args['company'] else: company = 'GOOG' cl = requests.get("https://www.quandl.com/api/v3/datasets/WIKI/%s.json?order=asc&rows=31&start_date=2015-07-01&end_date=2015-09-03" % (company)) if cl.status_code == 200: c2=cl.content stock=simplejson.loads(c2) abb=stock['dataset']['dataset_code'] datanames=stock['dataset']['column_names'] data=stock['dataset']['data'] dataorg=pd.DataFrame(data,columns=datanames) dataorg['Date']=pd.to_datetime(dataorg['Date']) else: ######## THIS IS NOT RECOMMENDED, because now it just returns an error message if not find the ticker. return 'Error! Ticker does not exist!' # Create a graph fig = figure(x_axis_type="datetime") fig.line(dataorg.Date,dataorg.Close) fig.title="Stock closing price (%s), from 07-01-2015 " % (company) # fig.xaxis_axis_label='Date' # fig.yaxis_axis_label='Price' # Configure resources to include BokehJS inline in the document. # For more details see: # http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#module-bokeh.resources plot_resources = RESOURCES.render( js_raw=INLINE.js_raw, css_raw=INLINE.css_raw, js_files=INLINE.js_files, css_files=INLINE.css_files, ) # For more details see: # http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components script, div = components(fig, INLINE) html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, plot_resources=plot_resources, # color=color, company=company ) return encode_utf8(html)
def main(ipynb): print("running %s" % ipynb) with io.open(ipynb, encoding='utf8') as f: nb = read(f, NO_CONVERT) test_notebook(nb) base, ext = os.path.splitext(ipynb) exportHtml = HTMLExporter() (body, resources) = exportHtml.from_notebook_node(nb) outfile = ipynb + ".html" open(outfile, 'w').write(encode_utf8(body)) print("wrote %s" % outfile)
def search(): ticker = request.form['ticker'] url = "https://www.quandl.com/api/v3/datasets/WIKI/" + ticker + "/data.json" payload = {'api_key':'9o4vcpiASzqaA6dczzzZ','start_date': '2015-11-01', 'end_date': '2015-11-30'} res = requests.get(url, params=payload) raw_data = json.loads(res.text) date_strings = [x[0] for x in raw_data["dataset_data"]["data"]] close_price = [x[4] for x in raw_data["dataset_data"]["data"]] date = pd.to_datetime(pd.Series(date_strings)) p = figure(title="Stock Price for "+ticker+" (source: Quandl WIKI)", x_axis_label="Date",y_axis_label="Price", x_axis_type="datetime") p.line(date ,close_price, legend="Close Price", line_width=3) script, div = components(p, INLINE) html = render_template('results.html', plot_script=script, plot_div=div) return encode_utf8(html)
def check_for_low_count_using_title(): session_id = request.args.get("session_id") logging.debug("session id:%s" % session_id) df = load_csv(session_id) logging.debug("title count:%i" % df.shape[0]) string = "<br><p><i>Your search by job title returned a small number of job \ postings. Click 'NEW SEARCH' and use the 'keyword' search.</i></p><br>" if df.shape[0] < 25: return encode_utf8(string) else: return ""
def get(self, *args, **kwargs): session = yield self.get_session() element_id = self.get_argument("bokeh-autoload-element", default=None) if not element_id: self.send_error(status_code=400, reason='No bokeh-autoload-element query parameter') return resources = self.application.resources(self.request) websocket_url = self.application.websocket_url_for_request(self.request, self.bokeh_websocket_path) js = AUTOLOAD_JS.render( docs_json = None, js_urls = resources.js_files, css_files = resources.css_files, elementid = element_id, sessionid = session.id, websocket_url = websocket_url ) self.set_header("Content-Type", 'application/javascript') self.write(encode_utf8(js))
def index(): # Static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() plot_script = "" plot_div = "" articles = "" # Initialize the ticker registry (only once though) tickers_init() # Parse form data if request.method == 'POST': ticker = request.form.get('ticker').strip().upper() close = request.form.get('close') opening = request.form.get('open') high = request.form.get('high') low = request.form.get('low') # Go get stock info df = alpha_vantage(ticker) if df is not None: plot_script, plot_div = create_figure(df, ticker, [opening, close, high, low]) articles = get_news(ticker) else: flash('Stock ticker not found.') return redirect(url_for('index')) # Render our results (if any) html = render_template('index.html', js_resources=js_resources, css_resources=css_resources, plot_script=plot_script, plot_div=plot_div, articles=articles, entries=TICKERS) return encode_utf8(html)
def graph(): if request.method == 'POST': symbol = request.form['ticker'] else: dat = "method not post" #get quandl data quandlTicker = "WIKI/%s" % symbol data = quandl.get(quandlTicker, start_date="2018-01-01") data.index.name = 'Date' data.reset_index(inplace=True) fig = figure(x_axis_type="datetime", title="Stock Closing Price") fig.grid.grid_line_alpha = 0.3 fig.xaxis.axis_label = 'Date' fig.yaxis.axis_label = 'Price' fig.line(data['Date'], data['Close'], color='#A6CEE3', legend=symbol) js_resources = INLINE.render_js() css_resources = INLINE.render_css() # window_size = 30 # window = np.ones(window_size)/float(window_size) # avg = np.convolve(data['Close'], window, 'same') # return(output_file("graph.html", title="Stock Closing Price")) script, div = components(fig) html = render_template('graph.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources) return encode_utf8(html)
def bokeh(filename): #print(filename) data = xr.open_dataarray(os.path.join(app.config['UPLOAD_FOLDER'], filename)) #print(data) # init a basic bar chart: # http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#bars pandas_data = data.sel(Meson_Number=15,Momentum_List='0 0 0').to_dataframe() x = list(pandas_data.index) y = pandas_data[pandas_data.columns[-1]].values y = np.log(y/np.roll(y,-1)) print() print(x) print() print(y) print() fig = figure(plot_width=600, plot_height=600) fig.scatter( x, y, color='navy' ) # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script, div = components(fig) html = render_template( 'index.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def chart(): brand = request.form['brand'] response = get_data_from_api(brand=brand) # print(response) total = response['number_of_tweets'] positive = response['positive'] neutral = response['neutral'] negative = response['negative'] sentiments = ["Positive", "Neutral", "Negative"] distribution = [positive, neutral, negative] source = ColumnDataSource(data=dict(sentiments=sentiments, distribution=distribution)) p = figure(x_range=sentiments, plot_height=550, plot_width= 1000, title="Sentiment distribution", x_axis_label = 'Sentiments') p.vbar(x='sentiments', top='distribution', width=0.9, source=source, legend_field="sentiments", line_color=None, fill_color=factor_cmap('sentiments', palette=['#39DA00', '#FDB229', '#FF0445'], factors=sentiments)) p.xgrid.grid_line_color = None p.y_range.start = 0 p.legend.orientation = "horizontal" p.legend.location = "top_right" p.background_fill_color = "#0B0022" script, div = components(p) pos = round((float(positive)/float(total))*100, 1) neu = round((float(neutral)/float(total))*100, 1) neg = round((float(negative) / float(total)) * 100, 1) print(pos) print(neu) print(neg) html = render_template("chart.html", the_div=div, the_script=script, positive=pos, neutral=neu, negative=neg) return encode_utf8(html)
def extract_zip(zip_name, exclude_term=None): """Extracts a zip file to its containing directory.""" zip_dir = os.path.dirname(os.path.abspath(zip_name)) try: with zipfile.ZipFile(zip_name) as z: # write each zipped file out if it isn't a directory files = [ zip_file for zip_file in z.namelist() if not zip_file.endswith('/') ] print('Extracting %i files from %r.' % (len(files), zip_name)) for zip_file in files: # remove any provided extra directory term from zip file if exclude_term: dest_file = zip_file.replace(exclude_term, '') else: dest_file = zip_file dest_file = os.path.normpath(os.path.join(zip_dir, dest_file)) dest_dir = os.path.dirname(dest_file) # make directory if it does not exist if not os.path.isdir(dest_dir): os.makedirs(dest_dir) # read file from zip, then write to new directory data = z.read(zip_file) with open(dest_file, 'wb') as f: f.write(encode_utf8(data)) except zipfile.error as e: print("Bad zipfile (%r): %s" % (zip_name, e)) raise e
def dashboard(): CASSANDRA_HOST = ['192.168.0.101', '192.168.0.106'] CASSANDRA_PORT = 9042 CASSANDRA_DB = "cryptocoindb" cluster = Cluster(contact_points=CASSANDRA_HOST, port=CASSANDRA_PORT) session = cluster.connect(CASSANDRA_DB) session.row_factory = pandas_factory session.default_fetch_size = None ndays = request.args.get('ndays', default=7, type=int) print(ndays) tblGainLoss, tblDailyGainLoss, coinHistory, tblCoins = getAllData( ndays, session=session) # tblGainLoss, tblCoins, coinHistory = getSomeValueFromA_Database() # Grab the inputs arguments from the URL # args = flask.request.args l = layout( [[[[GainLossPlot(tblGainLoss)], [MarketPlot(tblCoins, coinHistory)]], WalletPlot(coinHistory)]], repsonsive=True) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(l) html = flask.render_template('old_dashboard.html', title="BitCoin Dashboard", plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, active_page='projects', footer='false') return encode_utf8(html)
def index(): # Static resources: js_resources = INLINE.render_js() css_resources = INLINE.render_css() plot_script = "" plot_div = "" # Parse form data if request.method == 'POST': ticker = request.form.get('ticker').strip().upper() close = request.form.get('close') opening = request.form.get('open') high = request.form.get('high') low = request.form.get('low') # Get stock info from Alpha Vantage df = alpha_vantage(ticker) if df is not None: plot_script, plot_div = create_plot(df, ticker, [opening, close, high, low]) # TODO: the function below isn't working, Need to figure out how to get input from multiple forms. # Use the index.html file in /template_tinkering and reference: # https://github.com/thedataincubator/sql_heroku/blob/master/app/templates/index.html#L54 #plot_script, plot_div = create_candlesctick(df, ticker) else: flash('Stock ticker not found') return redirect(url_for('index')) # Render results is available html = render_template('index.html', js_resources=js_resources, css_resources=css_resources, plot_script=plot_script, plot_div=plot_div, entries=['AAPL', 'GOOG', 'MSFT']) return encode_utf8(html)
def plot_data(): """Obtains post data and produces two graphs: volume of posts mentioning 'dengue' versus time; and volume of posts versus neighborhood """ (df, df_volume, df_by_location) = get_data() script1, div1 = volumeGraph(df_volume) script2, div2 = locationGraph(df_by_location) plot_resources = RESOURCES.render( js_raw=INLINE.js_raw, css_raw=INLINE.css_raw, js_files=INLINE.js_files, css_files=INLINE.css_files, ) html = flask.render_template('embed.html', plot_script1=script1, plot_div1=div1, plot_script2=script2, plot_div2=div2, plot_resources=plot_resources) return encode_utf8(html)
def bokeh(): # but = Button(label="click me", width = 80, height = 10, button_type = 'primary') # # def onclick(): # if par.text == "": # par.text = "Hello! World" # else: # par.text = "" # # but.on_click(onclick) # par = Paragraph(text="", width = 80, height = 61, style = {'background-color':'#F2F3F4'}) # grid = column(but, Div(text = "", width = 10, height = 100), par) # # js_resources = INLINE.render_js() # css_resources = INLINE.render_css() # render template # script, div = components(grid) script = server_document(url="http://localhost:5006/bokeh_code") html = render_template('index.html', plot_script=script) # div=div, js_resources=js_resources, css_resources=css_resources) return encode_utf8(html)
def balances(): if not current_user.is_authenticated: return redirect(url_for("user.login")) user_id = current_user.user_id # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script, div = components(plot_balance_report(user_id)) html = render_template("report/balances.html", plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, title="Остатки на счетах", current_month=get_current_month()["name"], last_month=get_last_month()["name"], accounts_list=sorted( data_observer.get_all_account_names(user_id))) return encode_utf8(html)
def example(): ticker = (request.form['ticker'])#new start = dt.datetime(2019,1,1) end = dt.datetime(2019,1,31) #PULL DATA FROM WEB df = web.DataReader(ticker, 'yahoo', start, end) #TSLA = tesla ds = ColumnDataSource(df) #allows for calling the date as index below #output_file("lines.html") ##if using this, add "from bokeh.plotting import figure, **output_file**, show TOOLS="hover,crosshair,pan,wheel_zoom,box_zoom,reset,tap,save,box_select,poly_select,lasso_select" p = figure(title="Stock price (adjusted close) for ticker '" + ticker + "', 2019 Jan 1 - 2019 Jan 31", x_axis_type='datetime', x_axis_label='Date', y_axis_label='Adjusted Closing Price', tools=TOOLS) p.xaxis.formatter=DatetimeTickFormatter( hours=["%d %B %Y"], days=["%d %B %Y"], months=["%d %B %Y"], years=["%d %B %Y"], ) p.xaxis.major_label_orientation = pi/4 p.line(source=ds, x='Date', y='Adj Close') p.circle(source=ds, x='Date', y='Adj Close') # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script, div = components(p) html = render_template( 'index.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def polynomial(): """ Very simple embedding of a polynomial chart """ # Grab the inputs arguments from the URL # This is automated by the button args = flask.request.args # Get all the form arguments in the url with defaults color = colors[getitem(args, 'color', 'Black')] _from = int(getitem(args, '_from', 0)) to = int(getitem(args, 'to', 10)) # Create a polynomial line graph x = list(range(_from, to + 1)) fig = figure(title="Polynomial") fig.line(x, [i**2 for i in x], color=color, line_width=2) # Configure resources to include BokehJS inline in the document. # For more details see: # http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh-embed js_resources = INLINE.render_js() css_resources = INLINE.render_css() # For more details see: # http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components script, div = components(fig, INLINE) html = flask.render_template('embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, color=color, _from=_from, to=to) return encode_utf8(html)
def get(self, *args, **kwargs): session = yield self.get_session() element_id = self.get_argument("bokeh-autoload-element", default=None) if not element_id: self.send_error(status_code=400, reason='No bokeh-autoload-element query parameter') return resources = self.application.resources() # TODO: yes, this should resuse code from bokeh.embed more directly render_items = [dict(sessionid=session.id, elementid=element_id, use_for_title=False)] script = _script_for_render_items(None, render_items, wrap_script=False) js = AUTOLOAD_JS.render( js_urls = resources.js_files, css_urls = resources.css_files, js_raw = resources.js_raw + [script], css_raw = resources.css_raw_str, elementid = element_id, ) self.set_header("Content-Type", 'application/javascript') self.write(encode_utf8(js))
def index(): # Create layout c_map = climate_map() ts = timeseries() l = legend() t = title() map_legend = hplot(c_map, l) layout = vplot(t, map_legend, ts) plot_resources = RESOURCES.render( js_raw=INLINE.js_raw, css_raw=INLINE.css_raw, js_files=INLINE.js_files, css_files=INLINE.css_files, ) script, div = components(layout, INLINE) html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, plot_resources=plot_resources, ) return encode_utf8(html)
def html_for_render_items(comm_js, docs_json, render_items, template=None, template_variables={}): comm_js = wrap_in_script_tag(comm_js) json_id = make_id() json = escape(serialize_json(docs_json), quote=False) json = wrap_in_script_tag(json, "application/json", json_id) script = wrap_in_script_tag(script_for_render_items(json_id, render_items)) context = template_variables.copy() context.update( dict( title='', bokeh_js=comm_js, plot_script=json + script, docs=render_items, base=NB_TEMPLATE_BASE, macros=MACROS, )) if len(render_items) == 1: context["doc"] = context["docs"][0] context["roots"] = context["doc"].roots if template is None: template = NB_TEMPLATE_BASE elif isinstance(template, string_types): template = _env.from_string("{% extends base %}\n" + template) html = template.render(context) return encode_utf8(html)
def bokeh(): global data_received import logging logging.warn("data %s" % data_received) div = handle_data_recieved(data_received) # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() titles = [ "Biểu đồ thống kê số lượng vi phạm theo giờ trong ngày", "Biểu đồ thống kê số lượng vi phạm theo ngày trong tuần", "Biểu đồ thống kê số lượng vi phạm của từng loại phương tiện", "Biểu đồ thống kê 10 bộ số xuất hiện thường xuyên", "Biểu đồ thống kê 10 bộ số xuất hiện không thường xuyên" ] # render template html = render_template( 'index.html', titles=titles, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def graph(): text = request.form.get('stock-ticker') ti = text.upper() mon = request.form.get('mon') yr = request.form.get('yr') day = '01' if mon == '01': mon2 = '12' yr2 = yrs[yrs.index(yr) - 1] else: mon2 = months[months.index(mon) - 1] #making the selection the previous month yr2 = yr d1 = '%s-%s-%s' % (yr, mon, day) d2 = '%s-%s-%s' % (yr2, mon2, day) # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() plot = create_stock_plot(ti, d1, d2) script, div = components(plot) html = render_template('graph.html', stock_ticker=ti, plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources) if ti: return encode_utf8(html) #return render_template('graph3.html', stock_ticker=ti, script=script, div=div) else: return 'Please enter a valid stock ticker', 400
def root(): """ Returns the spectrogram of audio data served from /data """ spectrogram = make_spectrogram() resources = Resources("inline") plot_resources = RESOURCES.render( js_raw = resources.js_raw, css_raw = resources.css_raw, js_files = resources.js_files, css_files = resources.css_files, ) plot_script, plot_div = components( spectrogram, resources ) html = flask.render_template( "spectrogram.html", plot_resources = plot_resources, plot_script = plot_script, plot_div = plot_div, ) return encode_utf8(html)
def index(): args = flask.request.args selected_team = flask.request.values.get("selected_team", "LAA") selected_year = int(flask.request.values.get("selected_year", "2012")) p = create_plot(selected_team, selected_year) plot_resources = RESOURCES.render( js_raw=INLINE.js_raw, css_raw=INLINE.css_raw, js_files=INLINE.js_files, css_files=INLINE.css_files, ) script, div = components(p, INLINE) html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, plot_resources=plot_resources, selected_team=selected_team, selected_year=selected_year, years=distinct_years, teams=distinct_teams, ) return encode_utf8(html)
def plotDataset(ds,template='dataset.html',title='Dataset',color=None,*args, **kwargs): ds = ds.copy() ds.data.columns = ds.data.columns.astype(str) # ts = TimeSeries(ds.data) numlines=len(ds.data.columns) if color is None: color = ds.meta[ds.meta.columns[0]] label = color color = colorby(color) # print color # source = ColumnDataSource(data=ds.data) source = ColumnDataSource(dict(xs=[ds.data.index.values]*ds.data.shape[1], ys = [ds.data[name].values for name in ds.data], yslog = [np.log2(ds.data[name].values) for name in ds.data], color=color, label=label)) labelsource = ColumnDataSource(ds.meta) colorsource = ColumnDataSource({k:colorby(ds.meta[k]) for k in ds.meta.columns.tolist()}) # if color is None: # # color = viridis(numlines) # color = colorby(range(numlines)) # else: # # v = viridis(max(color)+1) # # color = [v[c] for c in color] # color = colorby(color) fig = figure(title=title,plot_width=97*8,) # plot = Plot() # fig.line(ds.data.index.values, ds.data, line_width=2) # fig.multi_line(xs=[ds.data.index.values]*ds.data.shape[1], # ys = [ds.data[name].values for name in ds.data], # line_color=color, # line_width=5) fig.multi_line('xs', 'ys', color='color', legend='label', source=source) fig.legend.location = "top_left" # glyph = MultiLine(xs="xs", ys="ys", line_color="", line_width=2) # fig.add_glyph(source, glyph) # plot.add_glyph(source, glyph) callback = CustomJS(args=dict(source=source,colorsource=colorsource,labelsource=labelsource), code=""" var data = source.get('data'); var data2 = colorsource.get('data'); var data3 = labelsource.get('data'); var f = cb_obj.get('value') color = data['color'] color2 = data2[f] label = data['label'] label2 = data3[f] for (i = 0; i < color.length; i++) { color[i] = color2[i] label[i] = label2[i] } source.trigger('change'); """) logcallback = CustomJS(args=dict(source=source), code=""" var data = source.get('data'); data['ys'] = data['yslog'] source.trigger('change'); """) menu = [(c,c) for c in ds.meta.columns] dropdown = Dropdown(label="Color by", button_type="warning", menu=menu, callback=callback) # layout = vform(dropdown, fig) layout = column(dropdown, fig) js_resources = INLINE.render_js() css_resources = INLINE.render_css() # script, div = components(ts) script, div = components(layout) # script, div = components(fig) html = render_template( template, plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, *args, **kwargs ) return encode_utf8(html)
def heatmap(): selector_data = get_selector_data() # generate/retrieve data for selector menu default_data = {'building': '6', 'point': '305', 'from': '2017-08-18', 'to': '2017-08-30'} html = render_template('heatmap.html', selector_data=selector_data, data=default_data) return encode_utf8(html)
def graphs_input(): if request.method == 'POST': topic = request.form['topic'] sentence = request.form['sentence'] if sentence != '': sentence = str(sentence) with open('../pickles/lda_model_' + identifier + '.pkl', 'rb') as f: lda_model = pickle.load(f) bow = lda_model.id2word.doc2bow(sentence.split(' ')) topics = lda_model[bow] topic = 0 prob = 0 for topic_and_prob in topics: temp_topic = topic_and_prob[0] temp_prob = topic_and_prob[1] if temp_prob > prob: topic = temp_topic prob = temp_prob topic += 1 else: topic = int(topic) script, div = get_components(topic=topic) with open('../pickles/topic_dict_' + identifier + '.pkl', 'rb') as f: topic_dict = pickle.load(f) dates = topic_dict[topic]['date_published'] df = pd.DataFrame({'date_published': pd.Series(dates)}) df = df[df['date_published'] > date(2017, 5, 18)] df = df['date_published'].dt.date.value_counts() max_date = df.index[np.argmax(df.values)] # script, div = make_bokeh_plot(topic_dict, topic) anger_tones = topic_dict[topic]['Anger'] disgust_tones = topic_dict[topic]['Disgust'] fear_tones = topic_dict[topic]['Fear'] joy_tones = topic_dict[topic]['Joy'] sadness_tones = topic_dict[topic]['Sadness'] analytical_score = topic_dict[topic]['Analytical'] tone_mean = [] for i in range(5): tone_mean.append(np.mean(anger_tones)) tone_mean.append(np.mean(disgust_tones)) tone_mean.append(np.mean(fear_tones)) tone_mean.append(np.mean(joy_tones)) tone_mean.append(np.mean(sadness_tones)) colors = ['red', 'green', 'purple', 'yellow', 'blue'] tone = ['Anger', 'Disgust', 'Fear', 'Joy', 'Sadness'] idx = np.argmax(tone_mean) js_resources = INLINE.render_js() css_resources = INLINE.render_css() html = render_template( 'graphs_input.html', js_resources=js_resources, css_resources=css_resources, script=script, div=div, topic_num=topic, word_cloud='src="../static/img/wordclouds/wordcloud_topic' + str(topic) + '_' + identifier + '.png"', mood_plot='src="../static/img/mood_plots/mood_by_site_plot_by_topic' + str(topic) + '_' + identifier + '.png"', pos_neg_plot= 'src="../static/img/pos_neg_plots/pos_neg_by_site_plot_by_topic' + str(topic) + '_' + identifier + '.png"', tone_mean="{0:.1f}".format(tone_mean[idx] * 100), tone=tone[idx], color='color="' + colors[idx] + '"', max_date=max_date, date_plot='src="../static/img/coverage_plots/coverage_plot_by_topic' + str(topic) + '_' + identifier + '.png"') return encode_utf8(html)
def test_file_html_handles_css_only_resources(): css_resources = CSSResources(mode="relative", components=["bokeh"]) template = Template("<head>{{ bokeh_css }}</head><body></body>") output = embed.file_html(_embed_test_plot, (None, css_resources), "title", template=template) html = encode_utf8("<head>%s</head><body></body>" % css_resources.render_css()) assert output == html
def plot_page(df, columns, request, page): """Render the Bokeh plot. Inputs ------ df : pd.DataFrame The DataFrame with the data columns : list Which columns to use for choices in the plot request : flask The request object from flask page : str ('plot', 'plot_exo') Which page to render (for combined ['plot_exo'] or just SWEET-Cat ['plot']) Output ------ The rendered page with the plot """ colorscheme = 'Plasma' if request.method == 'POST': # Something is being submitted color = request.form['color'] x = str(request.form['x']) y = str(request.form['y']) z = str(request.form['z']) z = None if z == 'None' else z if (x not in columns) or (y not in columns): return redirect(url_for(page)) if (z is not None) and (z not in columns): return redirect(url_for(page)) colorscheme = str(request.form.get('colorscheme', colorscheme)) if colorscheme not in colorschemes.keys(): return redirect(url_for(page)) checkboxes = request.form.getlist("checkboxes") if not checkboxes: # When [] is returned checkboxes = [''] if checkboxes[0] not in [[], '', 'h**o']: return redirect(url_for(page)) df, x, y, z = extract(df, x, y, z, checkboxes) # Setting the limits x1, x2, y1, y2 = get_limits(request.form, x, y) if x.name != session.get('x', None): x1 = min(x) x2 = max(x) session['x'] = x.name if y.name != session.get('y', None): y1 = min(y) y2 = max(y) session['y'] = y.name xscale = str(request.form['xscale']) yscale = str(request.form['yscale']) if xscale not in ['linear', 'log']: return redirect(url_for(page)) if yscale not in ['linear', 'log']: return redirect(url_for(page)) else: if page == "plot_exo": x = 'discovered' y = 'plMass' z = None x1, x2 = 1985, 2020 y1, y2 = 0.0001, 200 session['x'] = 'discovered' session['y'] = 'plMass' session['z'] = 'None' else: x = 'teff' y = 'lum' z = 'logg' x1, x2 = 8000, 2500 y1, y2 = 1e-3, 3000 session['x'] = 'teff' session['y'] = 'lum' session['z'] = 'logg' yscale = 'log' color = 'Orange' xscale = 'linear' checkboxes = [] df, x, y, z = extract(df, x, y, z, checkboxes) # Check scale xscale, yscale, error = check_scale(x, y, xscale, yscale) stars = df['Star'] stars = list(stars.values) # Turn series into list. hover = HoverTool(tooltips=[ ("{}".format(x.name), "$x"), ("{}".format(y.name), "$y"), ("Star", "@star"), ]) num_points = count(x, y, [x1, x2], [y1, y2]) title = '{} vs. {}:\tNumber of objects in plot: {}'.format( x.name, y.name, num_points) tools = "crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select,save".split( ',') fig = figure(title=title, tools=tools + [hover], plot_width=800, plot_height=400, toolbar_location='above', x_range=[x1, x2], y_range=[y1, y2], x_axis_type=xscale, y_axis_type=yscale) if z is not None: # Add colours and a colorbar COLORS, pallete = colorschemes[colorscheme] groups = pd.qcut(z.values, len(COLORS), duplicates="drop") c = [COLORS[xx] for xx in groups.codes] source = ColumnDataSource(data=dict(x=x, y=y, c=c, star=stars)) color_mapper = LinearColorMapper(palette=pallete, low=z.min(), high=z.max()) color_bar = ColorBar(color_mapper=color_mapper, label_standoff=12, border_line_color=None, location=(0, 0)) fig.add_layout(color_bar, 'right') fig.circle('x', 'y', source=source, size=10, color='c', fill_alpha=0.2, line_color=None) z = z.name fig.xaxis.axis_label = x.name fig.yaxis.axis_label = y.name color_bar.title = z else: # Simple colorbar source = ColumnDataSource(data=dict(x=x, y=y, star=stars)) fig.circle('x', 'y', source=source, size=10, color=colors[color], fill_alpha=0.2, line_color=None) fig.xaxis.axis_label = x.name fig.yaxis.axis_label = y.name # Horizontal histogram hhist, hedges, hmax = scaled_histogram(x, num_points, xscale) ph = figure(toolbar_location=None, plot_width=fig.plot_width, plot_height=200, x_range=fig.x_range, y_range=(-hmax * 0.1, hmax), min_border=10, min_border_left=50, y_axis_location="right", x_axis_type=xscale) ph.xgrid.grid_line_color = None ph.yaxis.major_label_orientation = np.pi / 4 ph.background_fill_color = "#fafafa" ph.quad(bottom=0, left=hedges[:-1], right=hedges[1:], top=hhist, color="white", line_color=colors[color]) # Vertical histogram vhist, vedges, vmax = scaled_histogram(y, num_points, yscale) pv = figure(toolbar_location=None, plot_width=200, plot_height=fig.plot_height, x_range=(-vmax * 0.1, vmax), y_range=fig.y_range, min_border=10, y_axis_location="right", y_axis_type=yscale) pv.ygrid.grid_line_color = None pv.xaxis.major_label_orientation = np.pi / 4 pv.background_fill_color = "#fafafa" pv.quad(left=0, bottom=vedges[:-1], top=vedges[1:], right=vhist, color="white", line_color=colors[color]) layout = column(row(fig, pv), row(ph, Spacer(width=200, height=200))) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(layout) if error is not None: flash('Scale was changed from log to linear') html = render_template('plot.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, color=color, colors=colors, x=x.name, y=y.name, z=z, x1=x1, x2=x2, y1=y1, y2=y2, xscale=xscale, yscale=yscale, checkboxes=checkboxes, columns=columns, colorschemes=colorschemes, colorscheme=colorscheme) return encode_utf8(html)
def index(): form = InputForm(request.form) if request.method == 'POST' and form.validate(): veg, fylke, hpfra, hptil, mfra, mtil = form.veg.data, form.fylke.data, form.hpfra.data, form.hptil.data, form.mfra.data, form.mtil.data viseside = True else: viseside = False veg = 'fv241' fylke = '14' hpfra = 1 hptil = 10 mfra = 0 mtil = 10000 if hpfra == hptil: vegref = str(fylke) + '00' + veg + 'hp' + str(hpfra) + 'm' + str( mfra) + '-' + str(mtil) else: vegref = str(fylke) + '00' + veg + 'hp' + str(hpfra) + 'm' + str( mfra) + '-' + 'hp' + str(hptil) + 'm' + str(mtil) df = data.hentData(vegref) fig_skredtype = plot.plot_skredtype(df, vegref) fig_maaned = plot.plot_maaned(df, vegref) fig_losneområde = plot.plot_losneomrade(df, vegref) fig_steinmaaned = plot.plot_steinmaaned(df, vegref) fig_snomaaned = plot.plot_snomaaned(df, vegref) #fig_steinm = plot.plot_steinmaaned(df, vegref) fig_aar = plot.plot_aar(df, vegref) fig_volumomraade = plot.plot_volumomraade(df, vegref) fig_steinlosneomrade = plot.plot_steinlosneomrade(df, vegref) #grid_oversikt = gridplot([fig_skredtype, fig_maaned, fig_losneområde, fig_aar], ncols=1, plot_width=500) #grid_skredtype = gridplot([fig_steinmaaned, fig_snomaaned], ncols=1, plot_width=500) #gird_samlaplot = gridplot([], ncols=1, plot_width=500, plot_height=500) # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script_skredtype, div_skredtype = components(fig_skredtype) script_maaned, div_maaned = components(fig_maaned) script_aar, div_aar = components(fig_aar) script_steinmaaned, div_steinmaaned = components(fig_steinmaaned) script_snomaaned, div_snomaaned = components(fig_snomaaned) script_losneområde, div_losneområde = components(fig_losneområde) script_volumomraade, div_volumomraade = components(fig_volumomraade) script_steinlosneområde, div_steinlosneområde = components( fig_steinlosneomrade) html = render_template('index.html', script_skredtype=script_skredtype, div_skredtype=div_skredtype, script_maaned=script_maaned, div_maaned=div_maaned, script_aar=script_aar, div_aar=div_aar, script_losneområde=script_losneområde, div_losneområde=div_losneområde, script_steinmaaned=script_steinmaaned, div_steinmaaned=div_steinmaaned, script_steinlosneområde=script_steinlosneområde, div_steinlosneområde=div_steinlosneområde, script_snomaaned=script_snomaaned, div_snomaaned=div_snomaaned, script_volumomraade=script_volumomraade, div_volumomraade=div_volumomraade, js_resources=js_resources, css_resources=css_resources, form=form, vegref=vegref, viseside=viseside) return encode_utf8(html)
def map_ptsd(): # Grab the inputs arguments from the URL args = flask.request.args # Get all the form arguments in the url with defaults metric = getitem(args, 'metric', 'PTSD Rate') year = getitem(args, 'year', '2014') # connect to database eng = create_engine("mysql://%s:%s@localhost/va_open" % (os.getenv("MYSQL_USER"), os.getenv("MYSQL_PASS"))) # execute query with open('ptsd_rate_by_state.sql', 'r') as fid: ptsd_table = pd.read_sql_query(fid.read(), eng) # separate years p14 = ptsd_table.query('year == 2014').reset_index(drop=True) p15 = ptsd_table.query('year == 2015').reset_index(drop=True) col_add = ['total_served', 'total_ptsd'] if year == '2014': ptsd_table = p14 elif year == '2015': ptsd_table = p15 elif year == '2015+2014': ptsd_table = p15 ptsd_table[col_add] += p14[col_add] # get the state boundaries state_table = pd.DataFrame(us_states.data).T # merge tables ptsd_table = pd.merge(state_table, ptsd_table, how='inner', left_index=True, right_on='state') ptsd_table['ptsd_rate'] = (100 * ptsd_table.total_ptsd / ptsd_table.total_served) # create data source for map src = ColumnDataSource({ 'lons': ptsd_table['lons'].tolist(), 'lats': ptsd_table['lats'].tolist(), 'ptsd_rate': ptsd_table['ptsd_rate'], 'total_served': ptsd_table['total_served'], 'total_ptsd': ptsd_table['total_ptsd'], 'name': ptsd_table['name'] }) # generate color map cmap = LinearColorMapper(palette=palette) # create figure us_map = figure(width=1200, x_axis_location=None, y_axis_location=None, tools="hover", title="Rates of Post Traumatic Stress (2015)") us_map.grid.grid_line_color = None us_map.patches('lons', 'lats', source=src, line_color='black', fill_color={ 'field': metric, 'transform': cmap }) us_map.x_range = Range1d(-130, -60) us_map.y_range = Range1d(20, 55) # program hover tool hover = us_map.select_one(HoverTool) hover.tooltips = [("State", "@name"), ("Total Veterans Enrolled", "@total_served"), ("Total PTSD", "@total_ptsd (@ptsd_rate{int}%)")] js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(us_map) html = flask.render_template('embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, metric=metric, year=year) return encode_utf8(html)
def Ope(): if not request.method in ['POST']: return redirect(url_for('main.index')) files = request.files.getlist("ope[]") app.logger.debug('files={}'.format(files)) opes = [] del_files = [] upload_dir = current_app.config['APP_UPLOAD'] for f in files: filename = secure_filename(f.filename) ope = os.path.join(upload_dir, filename) f.save(ope) del_files.append(ope) if filename.lower().endswith(".ope"): opes.append(ope) app.logger.debug('opes={}'.format(opes)) try: targets = helper.ope(opes, upload_dir, app.logger) #errors = helper.format_error(targets, app.logger) except Exception as e: app.logger.error('Error: invalid ope file. {}'.format(e)) err_msg = "Plot Error: {}".format(e) #errors.append(err_msg) delete_files(del_files) return render_template('target_visibility.html', errors=[err_msg]) #filepath = tempfile.NamedTemporaryFile().name #filepath = os.path.join(current_app.config['APP_UPLOAD'], filename) mysite = helper.site(request.form.get('site')) mydate = request.form.get('date') #app.logger.debug('targets={}'.format(targets)) #app.logger.debug('filepath={}'.format(filepath)) app.logger.debug('mydate={}'.format(mydate)) delete_files(del_files) try: #fig = helper.populate_target2(targets, mysite, mydate, filepath, app.logger) fig = helper.populate_interactive_target(target_list=targets, mysite=mysite, mydate=mydate, logger=app.logger) except Exception as e: app.logger.error('Error: failed to populate ope plot. {}'.format(e)) err_msg = "Plot Error: {}".format(e) #errors.append(err_msg) return render_template('target_visibility.html', errors=[err_msg]) else: # Grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script, div = components(fig) html = render_template('target_visibility.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, errors=None) return encode_utf8(html)
def graph(): ajax_input = dict(x_time=[], x1_time=[], y=[], y_female=[], y_male=[], x_female_proj=[], y_female_proj=[], x_male_proj=[], y_male_proj=[], y_female_avg_viewing=[], y_male_avg_viewing=[]) source = AjaxDataSource(data=ajax_input, data_url='http://127.0.0.1:5000/data', polling_interval=ajax_refresh_inter * 1000) #adapter=adapter) p = figure(plot_height=300, plot_width=800, x_axis_type="datetime", tools="wheel_zoom,reset", title="People Viewing Statistics") p.line(x='x_time', y='y', line_dash="4 4", line_width=3, color='gray', source=source) p.vbar(x='x_time', top='y_female', width=200, alpha=0.5, color='red', legend='female', source=source) #female p.vbar(x='x1_time', top='y_male', width=200, alpha=0.5, color='blue', legend='male', source=source) #male p.xaxis.formatter = DatetimeTickFormatter(milliseconds=["%X"], seconds=["%X"], minutes=["%X"], hours=["%X"]) p.y_range = DataRange1d(start=0, range_padding=5) #padding leave margin on the top p.legend.orientation = "horizontal" #legend horizontal p.xaxis.axis_label = 'Time' p.yaxis.axis_label = 'Number' # p.x_range.follow = "end" # p.x_range.follow_interval = timedelta(seconds=30) p2 = figure(plot_height=300, plot_width=800, tools="wheel_zoom,reset", title="Project Viewing Statistics") p2.vbar(x='x_female_proj', top='y_female_proj', width=f2_vbar_interval, alpha=0.5, color='red', legend='female', source=source) #female p2.vbar(x='x_male_proj', top='y_male_proj', width=f2_vbar_interval, alpha=0.5, color='blue', legend='male', source=source) #male p2.xaxis.ticker = [2, 6, 10, 14] p2.xaxis.major_label_overrides = {2: 'P1', 6: 'P2', 10: 'P3', 14: 'P4'} # avg_view_male = LabelSet(x='x_male_proj', y='y_male_proj', text='y_male_avg_viewing', level='glyph', # x_offset=0, y_offset=5, source=source, render_mode='canvas') # avg_view_female = LabelSet(x='x_female_proj', y='y_female_proj', text='y_female_avg_viewing', level='glyph', # x_offset=0, y_offset=5, source=source, render_mode='canvas') # # p2.add_layout(avg_view_male) # p2.add_layout(avg_view_female) p2.x_range = DataRange1d(start=0, end=16) #padding leave margin on the top p2.y_range = DataRange1d(start=0, range_padding=5) #padding leave margin on the top p2.legend.orientation = "horizontal" p2.xaxis.axis_label = 'Project' p2.yaxis.axis_label = 'Number' js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components((p, p2)) html = render_template( 'embed.html', plot_script=script, plot_div=div[0], plot_div2=div[1], js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def correlation(): "the scatter plot" args = flask.request.args xattr = args.get("x", "atomic_number") yattr = args.get("y", "covalent_radius_pyykko") categ = args.get("categ", "name_series") data = get_data() properties = get_property_names(data) categories = get_category_names() fig = Figure(title="{} vs {}".format(properties[xattr], properties[yattr]), plot_width=PLOT_WIDTH, plot_height=PLOT_HEIGHT, tools="box_zoom,pan,resize,save,reset", toolbar_location="above", toolbar_sticky=False, ) fig.xaxis.axis_label = properties[xattr] fig.yaxis.axis_label = properties[yattr] ccm = get_color_mapper(categ, data) if categ == "None": legend = None color_dict = "#1F77B4" else: legend = categ color_dict = {"field": categ, "transform": ccm} fig.circle(x=xattr, y=yattr, fill_alpha=0.7, size=10, source=ColumnDataSource(data=data), fill_color=color_dict, line_color=color_dict, legend=legend) if categ != "None": fig.legend.location = (0, 0) fig.legend.plot = None fig.add_layout(fig.legend[0], "right") hover = HoverTool(tooltips=HOVER_TOOLTIPS) fig.add_tools(hover) script, div = components(fig) js_resources = INLINE.render_js() css_resources = INLINE.render_css() html = render_template( "correlations.html", plot_script=script, plot_div=div, properties=properties, categories=categories, xselected=xattr, yselected=yattr, catselected=categ, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)