def index(request): plot = figure(responsive=True, tools=[], plot_width=500, plot_height=250, x_range=(-180, 180), y_range=(-90, 90)) plot.toolbar_location = None plot.axis.visible = None source = AjaxDataSource(method='GET', data_url='http://localhost:8000/view2d/data/', polling_interval=1000) source.data = dict(x=[], y=[]) # Workaround to initialize the plot img = load_image("static/images/earth.png") plot.image_rgba(image=[img], x=[-180], y=[-90], dw=[360], dh=[180]) plot.cross(source=source, x='x', y='y', size=22, line_width=4, color='Orange') # CLU1 script, div = components(plot, INLINE) js_resources = INLINE.render_js() css_resources = INLINE.render_css() context = { 'bokeh_script' : script, 'bokeh_div' : div, 'js_resources' : js_resources, 'css_resources' : css_resources } return render(request, 'view2d/index.html', context)
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 chart(): all_data = defaultdict(list) query = models.Currency.query.all() p = figure( width=1080, height=600, x_axis_type="datetime", ) for row in query: all_data[row.exchange].append((row.horah, row.price)) for i, (exchange, points) in enumerate(all_data.items()): color = bokeh.palettes.Category20[20][i] X,Y = zip(*sorted(points)) p.line(X,Y, line_width=2, alpha=0.7, legend=exchange, color=color) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(p) return render_template( 'index.html', js_resources = js_resources, css_resources = css_resources, plot_script = script, plot_div = div, )
def generic(): """ Pull up Generic Grid plot the results and download """ # Grab the inputs arguments from the URL args = dict(flask.request.args) fig, fh, closest_match, error_message = generic_grid(args) # Write table string table_string = fh.getvalue() # Web-ify bokeh plot js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig) html = flask.render_template( 'generic.html', inputs=args, closest_match=closest_match, error_message=error_message, table_string=table_string, plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def data_retrieval(): conn = lite.connect('/Users/shanekenny/PycharmProjects/WiFinder/app/website/WiFinderDBv02.db') with conn: df = pd.read_sql_query("SELECT W.Log_Count, W.Time, W.Hour, W.Datetime, R.RoomID, R.Capacity, C.ClassID, C.Module, C.Reg_Students, O.Occupancy, O.OccID FROM WIFI_LOGS W JOIN CLASS C ON W.ClassID = C.ClassID JOIN ROOM R ON C.Room = R.RoomID JOIN OCCUPANCY O ON C.ClassID = O.ClassID WHERE R.RoomID = 'B002' AND W.Datetime = '2015-11-12' GROUP BY W.LogID;", conn) df['Time'] = df['Time'].apply(pd.to_datetime) p = figure(width=800, height=250, x_axis_type="datetime", ) p.extra_y_ranges = {"foo": Range1d(start=0, end=1)} p.line(df['Time'], df['Log_Count'], color='red',legend='Log Count') p.line(df['Time'], df['Reg_Students'], color='green',legend='Registered Students') p.line(df['Time'], df['Capacity'], color='blue', legend='Capacity') p.line(df['Time'], df['Occupancy']*100, color='orange', legend='Occupancy') p.add_layout(LinearAxis(y_range_name="foo"), 'left') p2 = figure(width=800, height=250, x_axis_type="datetime", x_range=p.x_range,) p2.line(df['Time'], df['Log_Count'], color='red', legend='Log Count') r= gridplot([[p, p2]], toolbar_location=None) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(r) return flask.render_template( 'explore.html', script=script, div=div, js_resources=js_resources, css_resources=css_resources,)
async def get_stocks_daily( stockcode: str, request: Request): # async加了就支持异步 把Request赋值给request stock_daily_dict = get_data_dict('daily_qfq_macd_' + stockcode) stock_moneyflow_pct = stocks_moneyflow.cal_moneyflow_pct(stockcode) js_res = INLINE.render_js() css_res = INLINE.render_css() p = draw_candlestick('daily_qfq_macd_' + stockcode) script, div = components(p) p2 = draw_line_param_avgline('daily_qfq_macd_' + stockcode, 'close', 'red', 'close', 10, 'black') script2, div2 = components(p2) p3 = draw_line_param('daily_qfq_macd_' + stockcode, 'pct_chg', 'green') script3, div3 = components(p3) p4 = draw_line_param_avgline('daily_qfq_macd_' + stockcode, 'vol', 'blue', 'vol', 10, 'red') script4, div4 = components(p4) return tmp.TemplateResponse( 'stock_daily.html', { 'request': request, # 一定要返回request 'stockcode': stockcode, 'stockdailycount': len(stock_daily_dict), 'stockdaily': stock_daily_dict, 'stock_moneyflow_pct': stock_moneyflow_pct, "p_script": script, "p_div": div, "p_script2": script2, "p_div2": div2, "p_script3": script3, "p_div3": div3, "p_script4": script4, "p_div4": div4, "js_res": js_res, "css_res": css_res })
def showind(): c0 = request.args['country0'] c1 = request.args['country1'] c2 = request.args['country2'] c3 = request.args['country3'] c4 = request.args['country4'] countries = [c0, c1, c2, c3, c4] i0 = request.args['indicator'] indicator = [i0] performance = get_indicator(countries,indicator) chartind = plot_ind(performance) script,div = components(chartind) return render_template('showind.html', js_resources=INLINE.render_js(), css_resources=INLINE.render_css(), script=script, div=div)
def learn_more_esg(): data = yf.download(tickers='DSI, SPY', period='10y') data = data.reset_index() p = figure(title="Stock chart of KLD400 Social Index and S&P 500", x_axis_label="Date", y_axis_label="Stock Price", x_axis_type="datetime", plot_width=800, plot_height=300) p.line(y=data['Adj Close']['SPY'], x=data["Date"], color="green", legend_label="S&P 500") p.line(y=data['Adj Close']['DSI'], x=data["Date"], color="blue", legend_label="KLD400 Social Index") p.legend.location = "top_left" p.legend.click_policy = "hide" js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(p) return render_template('learn_more_esg.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources)
def analytics(): """ This view launches the analytics jobs of the whole posts contained in a topic, and returns the data in the form of a dashboard. """ target = request.args['ref'] a = Analyzer(target) a.gather_data() p1 = get_pie(a.df_tone) p2 = get_pie(a.df_dom) script1, div1 = components(p1) script2, div2 = components(p2) return render_template("dashboard.html", title='Agor@phon. Topic analysis', topic=a.title, sample=a.sample, stat=a.stat, dom=a.df_dom, tone=a.df_tone, ref=target, plot_script1=script1, plot_div1=div1, plot_script2=script2, plot_div2=div2, js_resources=INLINE.render_js(), css_resources=INLINE.render_css())
def predict(): input_sequence = request.form['sequence'] seq = standardize_sequence(to_numeric_rep(input_sequence, 'mw'), 'protease').reshape(1, -1) preds = predictions(drugs, models, seq) TOOLS = [PanTool(), ResetTool(), WheelZoomTool(), SaveTool()] plot = BoxPlot(data=preds, values='log10(DR)', label='drug', color='drug', title="protease drug resistance", plot_width=600, plot_height=400, legend=False, tools=TOOLS) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(plot, INLINE) return render_template( 'predictor/predictions.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, )
def ajax(): x = [1, 2, 3] source = ColumnDataSource(data=dict(x=x, y=my_data['one'])) plot = figure(height=250, width=300) plot.line('x', 'y', source=source, line_width=3, line_alpha=0.8) callback = CustomJS(args=dict(source=source), code=""" var selected_value = cb_obj.value; var plot_data = source.data; jQuery.ajax({ type: 'POST', url: '/new_option', data: {"value": selected_value}, dataType: 'json', success: function (response) { plot_data['y'] = response["option"]; source.trigger('change'); }, error: function() { alert("An error occured!"); } }); """) select = Select(value='one', options=['one', 'two', 'three'], callback=callback) layout = column(widgetbox(select, width=100), plot) script, div = components(layout, INLINE) return jsonify(script=script, div=div, js_resources=INLINE.render_js(), css_resources=INLINE.render_css())
def home(): x = [x * 0.005 for x in range(0, 200)] y = x source = ColumnDataSource(data=dict(x=x, y=y)) plot = figure(plot_width=300, plot_height=250) plot.line('x', 'y', line_width=3, source=source, line_alpha=0.6) callback = CustomJS(args=dict(source=source), code=""" var data = source.data; var f = cb_obj.value x = data['x'] y = data['y'] for (i = 0; i < x.length; i++) { y[i] = Math.pow(x[i], f) } source.trigger('change'); """) slider = Slider(start=0.1, end=4, value=1, step=.1, title="power") slider.js_on_change('value', callback) layout = column(slider, plot) script, div = components(layout, INLINE) return render_template('bokeh.html', script=script, div=div, js_resources=INLINE.render_js(), css_resources=INLINE.render_css())
def portalEvolution(snapshot, portalid): with Timer(key="get_portalEvolution", verbose=True): Session = current_app.config['dbsession'] data = {} with Timer(key="query_portalEvolution", verbose=True): for R in Session.query(PortalSnapshot).filter( PortalSnapshot.portalid == portalid): data[R.portalid + str(R.snapshot)] = row2dict(R) for R in Session.query(PortalSnapshotQuality).filter( PortalSnapshotQuality.portalid == portalid): data[R.portalid + str(R.snapshot)].update(row2dict(R)) df = pd.DataFrame([v for k, v in data.items()]) with Timer(key="plot_portalEvolution", verbose=True): p = evolutionCharts(df) script, div = components(p) js_resources = INLINE.render_js() css_resources = INLINE.render_css() data = getPortalInfos(Session, portalid, snapshot) return render("odpw_portal_evolution.jinja", plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, snapshot=snapshot, portalid=portalid, data=data)
def fortney(): """ Pull up Forntey Grid plot the results and download """ # Grab the inputs arguments from the URL args = flask.request.args input_args = _param_fort_validation(args) fig, fh, temp_out = fortney_grid(input_args) table_string = fh.getvalue() js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig) html = flask.render_template('fortney.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, temp=temp_out, table_string=table_string) return encode_utf8(html)
def index(): # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # assign the dynamic resources jquery_draggable = "<script type='text/javascript'>$( function() {$('#fig{~}').resizable({grid:[10,10]}).draggable({containment:\"#dashboard\"});});</script>" plot_scripts = '' plot_divs = '' for idx, fig in enumerate(figures): script, div = components(fig) plot_scripts = plot_scripts + jquery_draggable.replace( '{~}', str(idx)) plot_scripts = plot_scripts + script + '\n' div = div.replace( '\"bk-root\"', '\"bk-root\" id=\"fig{}\" style=\"width: 500px; height:500px; padding: 10px;\"' ).format(idx) plot_divs = plot_divs + div + '\n' # render template html = render_template('index.html', js_resources=js_resources, plot_scripts=plot_scripts, css_resources=css_resources, plot_divs=plot_divs) return encode_utf8(html)
def bokeh(): # init a basic bar chart: # http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#bars fig = figure(plot_width=600, plot_height=600) fig.vbar(x=[1, 2, 3, 4], width=0.5, bottom=0, top=[1.7, 2.2, 4.6, 3.9], 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 bokeh(): # chart defaults color = '#FF0000' start = 0 finish = 10 # create a polynomial line graph with the above args x = list(range(start, finish + 1)) fig = figure(title='Polynomial') fig.line(x, [i**2 for i in x], color=color, line_width=2) # 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 showbusscore(): c0 = request.args['country0'] c1 = request.args['country1'] c2 = request.args['country2'] c3 = request.args['country3'] c4 = request.args['country4'] countries = [c0, c1, c2, c3, c4] i0 = "IC.BUS.EASE.XQ" indicators = [i0] dataframes = create_dataframes(countries,indicators) score = get_mean(dataframes) chartsco = plot_score(score) script,div = components(chartsco) return render_template('showscore.html', js_resources=INLINE.render_js(), css_resources=INLINE.render_css(), script=script, div=div)
def hello(rack_eval=None): compares = [("exactly", "=="), ("at least", ">="), ("at most", "<=")] alphabet = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '?' ] if (not rack_eval): return render_template('index.html', has_data=False, bad_data=False, compares=compares, alphabet=alphabet) if (not rack_eval.control_n or not rack_eval.test_n or rack_eval.control_n < 40 or rack_eval.test_n < 40): return render_template('index.html', bad_data=True, has_data=False, compares=compares, alphabet=alphabet) fig = createFigure(rack_eval.control_y, rack_eval.test_y, rack_eval.control_label, rack_eval.test_label) # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script, div = components(fig) if (rack_eval.p_value >= 0.001): p_value_formatted = "{0:.2f}".format(rack_eval.p_value) else: p_value_formatted = "{0:.2E}".format(rack_eval.p_value) ci_lower_formatted = "{0:.2f}".format(rack_eval.ci_lower) ci_upper_formatted = "{0:.2f}".format(rack_eval.ci_upper) mean_diff_formatted = "{0:.2f}".format(rack_eval.mean_diff) return render_template('index.html', has_data=True, bad_data=False, plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, compares=compares, alphabet=alphabet, control_label=rack_eval.control_label, test_label=rack_eval.test_label, control_n=rack_eval.control_n, test_n=rack_eval.test_n, control_y_bar=rack_eval.control_y_bar, test_y_bar=rack_eval.test_y_bar, p_value=rack_eval.p_value, p_value_formatted=p_value_formatted, ci_lower_formatted=ci_lower_formatted, ci_upper_formatted=ci_upper_formatted, mean_diff=mean_diff_formatted)
def root(): conn = dblyr.Database().getConn() engine = dblyr.Database().getEngine() s = select(temperature.Temperature).where(temperature.Temperature.sensor == '95132,us') df = pd.read_sql(s, conn) df['datetime'] = pd.to_datetime(df['datetime'], format='%Y-%m-%d %H:%M:%S') source = ColumnDataSource(df) nest = select(temperature.Temperature).where(temperature.Temperature.sensor == 'nest') nest_df = pd.read_sql(nest, conn) nest_df['datetime'] = pd.to_datetime(nest_df['datetime'], format='%Y-%m-%d %H:%M:%S') nest_source = ColumnDataSource(nest_df) # fig = figure(plot_height=600, plot_width=720, tooltips=[("Title", "@title"), ("Released", "@released")]) fig = figure(x_axis_type="datetime", plot_height=600, plot_width=720) fig.line(x='datetime', y='temp', source=source, line_width=2, legend_label='95132,US') fig.line(x='datetime', y='temp', source=nest_source, line_width=2, color=Spectral3[2], legend_label='Nest') fig.xaxis.axis_label = "DateTime" fig.yaxis.axis_label = "Temperature" script, div = components(fig) return render_template( 'temp.html', plot_script=script, plot_div=div, js_resources=INLINE.render_js(), css_resources=INLINE.render_css(), ).encode(encoding='UTF-8')
def index(): """ Main page """ # Static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() (bands, reviews, albums, labels, songs, artists) = update_ma_stats() # Render our results (if any) html = render_template( 'index.html', bands=bands, reviews=reviews, albums=albums, labels=labels, songs=songs, artists=artists, avgratingsworldmap='avgratingsbandsworldmap-plot.html', avgnumworldmap='avgnumbandsworldmap-plot.html', genres='genres-plot.html', avgratingbygenre='avgratingbygenre-plot.html', avgwordsbyyear='avgwordsbyyear-plot.html', tsne='tsne-plot.html', topicmodel='lda.html', heatmap='heatmap-plot.html') return encode_utf8(html)
def systemevolv(): with Timer(key="get_systemevolv", verbose=True): Session = current_app.config['dbsession'] with Timer(key="query_systemevolv", verbose=True): t = Session.query( PortalSnapshot.snapshot.label('snapshot'), Portal.software, PortalSnapshot.datasetcount, PortalSnapshot.resourcecount).join(Portal).subquery() q = Session.query( t.c.snapshot, t.c.software, func.count().label('count'), func.sum(t.c.resourcecount).label('resources'), func.sum(t.c.datasetcount).label('datasets')).group_by( t.c.snapshot, t.c.software) data = [row2dict(r) for r in q.all()] df = pd.DataFrame(data) with Timer(key="plot_systemevolv", verbose=True): p = systemEvolutionPlot(df) script, div = components(p) js_resources = INLINE.render_js() css_resources = INLINE.render_css() return render("odpw_system_evolution.jinja", plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources)
def pointmatch_z1_z2(rendersource, owner, project, stack, collection, z1, z2): try: host, port = get_host_port(rendersource) render = renderapi.render.connect(host, port, owner, project, client_scripts='') fig = make_pointmatch_plot(render, stack, collection, z1, z2) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig) html = render_template('embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources) return html except renderapi.errors.RenderError as r: return r.message return ("host:%s port:%d rendersource:%s owner:%s project:%s " "stack:%s collection:%s" % (host, port, rendersource, owner, project, stack, collection))
def multiflexx_sim(request): scan_data = request.GET try: if scan_data['submitted'] == 'Ja': submitted = True else: submitted = False except KeyError: submitted = False scan_rows = [] script, div = '', '' if submitted: scan_data_nice = extract_data(scan_data) scan_rows = make_scan_rows(scan_data_nice) script, div = make_figures(scan_data_nice) js_resources = INLINE.render_js() css_resources = INLINE.render_css() tag_lib = { 'scan_data': scan_data, 'scan_rows': scan_rows, 'graph_script': script, 'graph_div': div, 'js_resources': js_resources, 'css_resources': css_resources, 'submitted': submitted } tag_lib.update(scan_data) return render(request, 'multiflexx.html', tag_lib)
async def get_mainpanel(request: Request): # async加了就支持异步 把Request赋值给request js_res = INLINE.render_js() css_res = INLINE.render_css() p1 = draw_candlestick('000001.SH') script1, div1 = components(p1) index_dict_0 = get_data_dict('000001.SH')[0] index_dict_1 = get_data_dict('000001.SH')[1] index_df = get_data('000001.SH') index_close_avg_5 = index_df['close'][0:4].mean() pct_chg_avg_5 = format( (index_df['close'][0] - index_close_avg_5) / index_close_avg_5 * 100, '.2f') return tmp.TemplateResponse( 'data_mainpanel.html', { 'request': request, # 一定要返回request 'index_dict_0': index_dict_0, 'index_dict_1': index_dict_1, 'index_close_avg_5': index_close_avg_5, 'pct_chg_avg_5': pct_chg_avg_5, "p_script1": script1, "p_div1": div1, "js_res": js_res, "css_res": css_res })
def index(): js_resources = INLINE.render_js() css_resources = INLINE.render_css() # Build plots train_script, train_div = components(train_plot()) test_script, test_div = components(test_plot()) model_script, model_div = components(model_plot()) p1_script, p1_div = components(build_p1()) p2_script, p2_div = components(build_p2()) return render_template("index.html", js_resources=js_resources, css_resources=css_resources, train_script=train_script, train_div=train_div, test_script=test_script, test_div=test_div, model_script=model_script, model_div=model_div, p1_script=p1_script, p1_div=p1_div, p2_script=p2_script, p2_div=p2_div)
def show(meid): """ Load a visualization that is on the underlying grids. Args: meid: Uniform Resource Identifier with which to get the data. """ LOGGER.warning(f"meid is {meid}") g.meid = meid if "URI" not in current_app.config: current_app.config["URI"] = "none" db = get_db() fig = figure(plot_width=300, plot_height=300) fig.vbar(x=[1, 2, 3, 4], width=0.5, bottom=0, top=[1.7, 2.2, 4.6, 3.9], color="navy") script, div = components(fig) return encode_utf8( render_template( "natural.html", uri=current_app.config["URI"], plot_script=script, plot_div=div, js_resources=INLINE.render_js(), css_resources=INLINE.render_css(), ))
def result(): if request.method == 'POST': stock_name = request.form.get('stock') stock_data = get_data(stock_name) stock_plot = create_figure(stock_data, stock_name) else: # init a basic bar chart: # http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#bars stock_plot = figure(plot_width=600, plot_height=600) stock_plot.vbar( x=[1, 2, 3, 4], width=0.5, bottom=0, top=[1.7, 2.2, 4.6, 3.9], color='navy' ) # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(stock_plot) # # render template html = render_template( 'result.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources ) return html
def bokeh(): data=request.args.to_dict() a=int(data['a']) b=int(data['b']) n=int(data['n']) x=np.linspace(0,10,n) y=a*np.sin(b*x) # init a basic bar chart: # http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#bars fig = figure(plot_width=1280, plot_height=600) fig.line(x,y) left, right, bottom, top = 0, 10, -10, 10 fig.x_range=Range1d(left, right) fig.y_range=Range1d(bottom, top) # 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 html.encode('utf8')
def data(): data = get_data() columns = OrderedDict([ ("atomic_number", "Atomic number"), ("symbol", "Symbol"), ("name", "Name"), ("atomic_weight", "Atomic weight"), ("en_pauling", "Electronegativity"), ("electron_affinity", "Electron affinity"), ]) table = make_table(ColumnDataSource(data), columns) script, div = components(table) js_resources = INLINE.render_js() css_resources = INLINE.render_css() html = render_template( "data.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 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 with those arguments x = list(range(_from, to + 1)) fig = figure(title="Polynomial") fig.line(x, [i**2 for i in x], color=color, line_width=2) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig) html = flask.render_template('index.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)
async def get_moneyflow_lastday( request: Request): # async加了就支持异步 把Request赋值给request #data_lastday = getindex('moneyflow_lastday') js_res = INLINE.render_js() css_res = INLINE.render_css() plot1 = draw_piechart('moneyflow_lastday', 'net_mf_amount', 20, -1) script1, div1 = components(plot1) plot2 = draw_piechart('moneyflow_lastday', 'net_mf_amount', 20, 1) script2, div2 = components(plot2) plot3 = draw_piechart('moneyflow_lastday', 'buy_lg_amount', 20, -1) script3, div3 = components(plot3) plot4 = draw_piechart('moneyflow_lastday', 'buy_elg_amount', 20, -1) script4, div4 = components(plot4) data_lastday = get_data_sort('moneyflow_lastday', 'net_mf_amount', -1, 9999) return tmp.TemplateResponse( 'moneyflow_stocks_lastday.html', { 'request': request, # 一定要返回request 'stockscount': len(data_lastday), # 额外的参数可有可无 'stocks': data_lastday, "p_script1": script1, "p_div1": div1, "p_script2": script2, "p_div2": div2, "p_script3": script3, "p_div3": div3, "p_script4": script4, "p_div4": div4, "js_res": js_res, "css_res": css_res })
def index(): js_resources = INLINE.render_js() css_resources = INLINE.render_css() try: html = flask.render_template('index.html', js_resources=js_resources, css_resources=css_resources, choose_plot_script=choose_script, choose_plot_div=choose_div, box_plot_script=box_script, box_plot_div=box_div, omni_plot_script=omni_script, omni_plot_div=omni_div, surv_plot_script=surv_script, surv_plot_div=surv_div, pie_plot_script=pie_script, pie_plot_div=pie_div, table_plot_script=table_script, table_plot_div=table_div, bar_plot_script=bar_script, bar_plot_div=bar_div, slider_plot_script=slider_script, slider_plot_div=slider_div) return html except: html = flask.render_template( 'index.html', place_holder="Error in flask, python, bokeh, java pipeline") return html
def bplot(x, y, non, sat): """From a 2D array it returns the Bokeh <script> that contains the data for your plot, together with an accompanying <div> tag that the plot view is loaded into. These tags can be used in HTML documents""" #Define data x = np.array(x) y = np.array(y) source = ColumnDataSource(data=dict(x=x, y=y)) plot = figure(sizing_mode='scale_width', plot_width=1000, plot_height=500, title="SNR vs Integration Time", toolbar_location="above") plot.xaxis.axis_label = 'Time (s)' plot.yaxis.axis_label = 'SNR' plot.line('x', 'y', source=source, line_width=2.) Nonline = Span(location=non, dimension='height', line_color='orange', line_width=3) plot.add_layout(Nonline) Satline = Span(location=sat, dimension='height', line_color='red', line_width=3) plot.add_layout(Satline) #JS js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(plot) return script, div, js_resources, css_resources
def data(): data = get_data() columns = OrderedDict([ ('atomic_number', 'Atomic number'), ('symbol', 'Symbol'), ('name', 'Name'), ('atomic_weight', 'Atomic weight'), ('en_pauling', 'Electronegativity'), ('electron_affinity', 'Electron affinity'), ]) table = make_table(ColumnDataSource(data), columns) script, div = components(table) js_resources = INLINE.render_js() css_resources = INLINE.render_css() html = render_template( 'data.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def dataframe_to_linegraph(dataframe, show_columns, title='graph'): '''Convert a dataframe to a bokeh line graph''' thisplot = figure( tools="pan,box_zoom,reset,save", title=title, x_axis_type="datetime", x_axis_label='Month', y_axis_label='Count', plot_width=1200, ) yvals = [x for x in dataframe.index.tolist()] idx = 0 for col, show in show_columns: if not show: continue width = 2 try: color = Paired[12][idx] except IndexError: color = Paired[12][-1] kwargs = { 'legend': col, 'line_width': width, 'line_color': color } idx += 1 xvals = dataframe[col].tolist() line = thisplot.line( yvals, xvals, **kwargs ) thisplot.add_tools( HoverTool( renderers=[line], tooltips=[ ('Name', col), ('Color', '<span class="bk-tooltip-color-block" ' 'style="background-color:{}"> </span>'. format(kwargs['line_color'])), ('count', "@y{int}"), ] ) ) thisplot.legend.location = "top_left" plot_script, plot_div = components(thisplot) js_resources = INLINE.render_js() css_resources = INLINE.render_css() return (plot_script, plot_div, js_resources, css_resources)
def save(fig): ''' Saves bokeh plots to HTML in ./Bokeh_plots ''' ## Set up directory for the plot files plots_dir = os.path.join('.','Bokeh_plots') if not os.path.exists(plots_dir): os.makedirs(plots_dir) filename = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')+'.html' figfile = os.path.join(plots_dir, filename) ## Save and show figure from jinja2 import Template from bokeh.embed import components from bokeh.resources import INLINE plots = {'fig':fig} script, div = components(plots) template = Template('''<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Bokeh Scatter Plots</title> {{ js_resources }} {{ css_resources }} {{ script }} <style> .embed-wrapper { width: 50%; height: 400px; margin: auto; } </style> </head> <body> {% for key in div.keys() %} <div class="embed-wrapper"> {{ div[key] }} </div> {% endfor %} </body> </html> ''') js_resources = INLINE.render_js() css_resources = INLINE.render_css() bokeh_html = template.render(js_resources=js_resources, css_resources=css_resources, script=script, div=div) with open(figfile, 'w') as f: f.write(bokeh_html)
def app_parse(): ticker_input = app.vars['ticker'] # ticker_input = raw_input('Please enter a stock ticker code (e.g., AAPL): ') quandl_url = 'https://www.quandl.com/api/v3/datasets/WIKI/' api_key = '_Dvc2yU_qTfyYtTzsqFd' csv_ext = '.csv' json_ext = '.json' input_url = quandl_url + ticker_input + json_ext input_csv = quandl_url + ticker_input + csv_ext r = requests.get(input_url) data = json.loads(r.text) pd_data = pd.read_csv(input_csv,parse_dates=['Date']) x = data["dataset"]["data"] data_rows = len(x) data_rows_good = np.zeros(data_rows) start_date = datetime.now().date() + timedelta(-30) booleans = [] for date_row in range(0,len(pd_data.Date)): # ticker_date = datetime.strptime(pd_data.Date[date_row],'%Y-%m-%d').date() ticker_date = pd_data.Date[date_row].date() if start_date < ticker_date: booleans.append(True) else: booleans.append(False) closing_price = pd_data.Close[booleans] # output_file("datetime.html") # create a new plot with a datetime axis type p = figure(width=500, height=500, x_axis_type="datetime") p.line(pd_data.Date[booleans], pd_data.Close[booleans], color='red', alpha=0.5) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(p, INLINE) html = flask.render_template( 'datetime.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 """ # connect to google spreadsheet and get sheet scope = ['https://spreadsheets.google.com/feeds'] credentials = ServiceAccountCredentials.from_json_keyfile_name('oauth-keyfile.json', scopes=scope) gc = gspread.authorize(credentials) responses = gc.open("Homework Histogram (Responses)") responses = responses.worksheet("Form Responses 1") # create plot html elements data = pd.DataFrame(responses.get_all_records()) # hist = Histogram(data, values='Question 1') # create a list of plots scripts = [] divs = [] for question in ['Question 1', 'Question 2']: hist = Histogram(data, values=question) script, div = components(hist, INLINE) scripts.append(script) divs.append(div) # 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() # script, div = components(hist, INLINE) # For more details see: # http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components html = flask.render_template( 'embed.html', scripts = scripts, divs=divs, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def index(): if request.method == 'GET': return render_template('index.html') else: app.vars['Company_Name'] = request.form['ticker'] if not app.vars['Company_Name']: return render_template('error.html') app.vars['features'] = request.form.getlist('features') Company_Name=app.vars['Company_Name'] API_url='https://www.quandl.com/api/v3/datasets/WIKI/%s.csv?api_key=a5-JLQBhNfxLnwxfXoUE' % Company_Name r = requests.get(API_url) if r.status_code == 404: return render_template('error.html') data = pd.read_csv(API_url,parse_dates=['Date']) Colors=["blue","green","yellow","red"] Color_index=0 target_data=data.ix[:,['Open','Adj. Open','Close','Adj. Close']] p=figure(x_axis_type="datetime") p.xaxis.axis_label = 'Date' p.title = 'Data from Quandle WIKI set' if 'Close' in app.vars['features']: p.line(x=data['Date'],y=target_data['Close'],legend="%s:Close" % Company_Name, line_color=Colors[Color_index]) Color_index = Color_index +1 if 'Adj. Close' in app.vars['features']: p.line(x=data['Date'],y=target_data['Adj. Close'],legend="%s:Adj. Close" % Company_Name, line_color=Colors[Color_index]) Color_index = Color_index +1 if 'Open' in app.vars['features']: p.line(x=data['Date'],y=target_data['Open'],legend="%s:Open" % Company_Name, line_color=Colors[Color_index]) Color_index = Color_index +1 if 'Adj. Open' in app.vars['features']: p.line(x=data['Date'],y=target_data['Adj. Open'],legend="%s:Adj. Open" % Company_Name, line_color=Colors[Color_index]) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(p, INLINE) html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, Company_Name= Company_Name ) return encode_utf8(html)
def index(): if request.method == 'POST': # Get the entered keywords keywords = request.form["keywords"] #keywords = [keyword.strip() for keyword in keywords.split(',') if len(keyword) > 0] keywords = [keyword.strip() for keyword in keywords.split(',') if len(keyword.strip()) > 0] keywords = keywords[:len(COLORS)] # prevent too many keywords # Get the location data user_location = (request.form['latitude'], request.form['longitude']) logger.info('{} | {}'.format(user_location, keywords)) fig = make_fig(keywords) # Build the bokeh plot resources # https://github.com/bokeh/bokeh/tree/master/examples/embed/simple js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig, INLINE) # Get recent comments matching the keywords recent_comments = get_matching_comments_2(keywords, user_location) # special case if no keywords entered - show 'All' comment counts if not keywords: keywords = ['All'] # Build the web page html = render_template( 'index.html', keywords = ', '.join(keyword.title() for keyword in keywords), plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, jobs=recent_comments, ) return html return render_template('index.html')
def plot(content, data=[]): x = list(range(0, 100 + 1)) fig = figure(title="Polynomial") print(dir(fig)) #fig.line(x, [i ** 2 for i in x], line_width=2) #fig.hbar(data['requested_by_name'], data['usage']) p=Bar(data, 'requested_by_name', values='usage') js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(p) html = render_template( 'overview.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, content=content, ) return encode_utf8(html)
def bokeh_bild(range=4800): data = bk_plot_timeline( LoadFromSQL( range, app.config['DATABASE_LOCATION'], 'VS1_GT1', 'VS1_GT3', 'VS1_GT2', 'VS1_Setpoint' ) ) print(data) plot_script, plot_div = components(data, INLINE) js_resources = INLINE.render_js() css_resources = INLINE.render_css() return render_template( 'bild.html', script=plot_script, div=plot_div, js_resources=js_resources, css_resources=css_resources)
def predict(): input_sequence = request.form['sequence'] seq = standardize_sequence(to_numeric_rep(input_sequence, 'mw'), 'protease').reshape(1, -1) preds = predictions(drugs, models, seq) TOOLS = [PanTool(), ResetTool(), WheelZoomTool(), SaveTool()] plot = BoxPlot(data=preds, values='log10(DR)', label='drug', color='drug', title="protease drug resistance", plot_width=600, plot_height=400, legend=False, tools=TOOLS) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(plot, INLINE) return render_template('predictor/predictions.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources,)
def showlegscore(): c0 = request.args['country0'] c1 = request.args['country1'] c2 = request.args['country2'] c3 = request.args['country3'] c4 = request.args['country4'] countries = [c0, c1, c2, c3, c4] i0 = ['CC.EST'] i1 = ['GE.EST'] i2 = ['RQ.EST'] i3 = ['VA.EST'] i4 = ['RL.EST'] indicators = [i0, i1, i2, i3, i4] dataframes = create_dataframes(countries,indicators) score = get_mean(dataframes) chartsco = plot_score(score) script,div = components(chartsco) return render_template('showscore.html', js_resources=INLINE.render_js(), css_resources=INLINE.render_css(), script=script, div=div)
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 index(): args = flask.request.args _input_material = str(args.get('_input_material', DEFAULT_MATERIAL)) _input_thickness = str(args.get('_input_thickness', DEFAULT_THICKNESS)) mat = Material(_input_material, float(_input_thickness) * u.micron) energy = u.Quantity(np.arange(1, 100), 'keV') source = ColumnDataSource(data={'x': energy.value, 'y': mat.absorption(energy).value}) source_static = ColumnDataSource(data={'x': energy.value, 'y': mat.absorption(energy).value}) fig = figure(title="Absorption", tools=TOOLS, plot_height=PLOT_HEIGHT, width=PLOT_WIDTH, toolbar_location="right", x_axis_label='Energy [keV]') fig.line('x', 'y', source=source_static, color='red', line_width=2, legend=_input_material) hover = HoverTool() #hover.tooltips = [ # ("x", "@time_str"), # ("y", "@xrsb") #] fig.add_tools(hover) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig, INLINE) html = flask.render_template( 'index.html', plot_script=script, plot_div=div, material_list=list(roentgen.elements['name']), js_resources=js_resources, css_resources=css_resources, _input_material=_input_material, _input_thickness=_input_thickness, ) return encode_utf8(html)
def show_simulation(): rounds = 0 try: if gtruncate_initial_rounds == 0: ignore_initial_rounds = int(session.get('ignore_initial_rounds', 50)) else: ignore_initial_rounds = 0 except NameError: ignore_initial_rounds = int(session.get('ignore_initial_rounds', 50)) gtruncate_initial_rounds = 0 plots = {} filenames = [] path = request.args.get('subdir') if path is None: path = newest_subdirectory('./result') try: with open(path + 'description.txt') as desc_file: desc = desc_file.read() except IOError: desc = '' plots = {} for filename in os.listdir(path): if not filename.endswith('.csv'): continue elif filename.startswith('#'): continue df = pd.read_csv(path + filename).ix[gtruncate_initial_rounds:] try: rounds = max(df['round']) except KeyError: rounds = max(df['index']) if ignore_initial_rounds >= rounds: ignore_initial_rounds = 0 print('kill') df = df.where((pd.notnull(df)), None) df.dropna(1, how='all', inplace=True) if filename.startswith('aggregate_'): plots.update(make_aggregate_graphs(df, filename, ignore_initial_rounds)) else: try: if max(df.get('id', [0])) == 0: plots.update(make_simple_graphs(df, filename, ignore_initial_rounds)) else: plots.update(make_panel_graphs(df, filename, ignore_initial_rounds)) except ValueError: print((filename, 'not displayable: ValueError')) script, div = components(plots) output = [] for idname_title, graph in div.items(): idname, title = json.loads(idname_title) output.append({'idname': idname, # can not stay i otherwise the cookie minimizing does not work 'title': title, 'graph': graph}) output.extend(load_text(path)) output.extend(load_text(path + '/../../')) output = sorted(output, key=lambda x: x['title']) return render_template('show_outcome.html', entries=output, desc=desc, setup=setup_dialog(rounds), script=script, js_resources=INLINE.render_js(), css_resources=INLINE.render_css())
{{ script }} <style> .embed-wrapper { display: flex; justify-content: space-evenly; } </style> </head> <body> <div class="embed-wrapper"> {% for key in div.keys() %} {{ div[key] }} {% endfor %} </div> </body> </html> ''') resources = INLINE.render() filename = 'embed_multiple.html' html = template.render(resources=resources, script=script, div=div) with io.open(filename, mode='w', encoding='utf-8') as f: f.write(html) view(filename)
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 _source=getitem(args,'_source',url_default) _size=int(getitem(args,'size','5')) favcolor=getitem(args,'favcolor','#1F77B4') bcolor=getitem(args,'bcolor','#F5F5DC') _shape=getitem(args,'_shape','circle') lcolor=getitem(args,'lcolor','#FCB938') toggle=bool(getitem(args,'toggle','')) #Parsing json DEFAULT_TICKERS = ['TOAs','RawProfiles', 'Period', 'PeriodDerivative', 'DM', 'RMS', 'Binary'] TOOLS = "tap,resize,crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select,previewsave,hover" Pulsar,TOAs,RawProfiles, Period, PeriodDerivative, DM, RMS, Binary=[],[],[],[],[],[],[],[] url_location=urllib.urlopen(_source) for item in ijson.items(url_location,"item"): Pulsar.append(str(item["Pulsar"])) TOAs.append(float(item["TOAs"])) RawProfiles.append(int(item["Raw Profiles"])) Period.append(float(item["Period"])) PeriodDerivative.append(float(item["Period Derivative"])) DM.append(str(item["DM"])) RMS.append(str(item["RMS"])) if (item["Binary"]=="Y"): Binary.append("Yes") else: Binary.append("No") #Create a plot periodvs period derivative s1 = ColumnDataSource( data=dict( Pulsar=Pulsar, TOAs=TOAs, RawProfiles=RawProfiles, Period=Period, PeriodDerivative=PeriodDerivative, DM=DM, RMS=RMS, Binary=Binary, ) ) p1 = figure(plot_width=600, plot_height=600, title="Period vs Period Derivative", y_axis_type="log" ,y_range=[min(PeriodDerivative)-min(PeriodDerivative)/5, max(PeriodDerivative)+max(PeriodDerivative)/5],x_range=[min(Period)-min(Period)/5, max(Period)+max(Period)/5],x_axis_label='Period[s]', y_axis_label='Period Derivative[s/s]',tools=TOOLS) p1.background_fill_color = bcolor p1.background_fill_alpha = "0.5" getattr(p1,_shape)('Period', 'PeriodDerivative', legend="period deri",color=favcolor,size=_size, source=s1) #p1.circle('Period', 'PeriodDerivative', legend="period deri",color=favcolor,size=_size, source=s1) p1.xaxis.axis_label_text_font_size = "15pt" p1.yaxis.axis_label_text_font_size = "15pt" #p1.xaxis[0].formatter = PrintfTickFormatter(format="s") #p1.yaxis[0].formatter = PrintfTickFormatter(format=" s/s") #Toggle Line #if getattr(p1,line,None): # getattr(p1,line)('Period', 'PeriodDerivative',legend="period deri",line_dash=[4, 4], line_color=lcolor, line_width=1,source=s1) p1.line('Period', 'PeriodDerivative',legend="period deri",line_dash=[4, 4], line_color=lcolor, line_width=1,source=s1,visible=toggle) # Custom data source for selected points s2 = ColumnDataSource( data=dict( Pulsar=[], TOAs=[], RawProfiles=[], Period=[], PeriodDerivative=[], DM=[], RMS=[], Binary=[], ) ) p2= figure(plot_width=600, plot_height=600, title=" Selected points from Period vs Period Derivative", y_axis_type="log" ,y_range=[min(PeriodDerivative)-min(PeriodDerivative)/10, max(PeriodDerivative)+max(PeriodDerivative)/10],x_range=[min(Period)-min(Period)/10, max(Period)+max(Period)/10],x_axis_label='Period[s]', y_axis_label='Period Derivative[s/s]',tools=TOOLS) p2.xaxis.axis_label_text_font_size = "15pt" p2.yaxis.axis_label_text_font_size = "15pt" p2.circle('Period', 'PeriodDerivative', legend="period deri",alpha=1.2, source=s2) s1.callback = CustomJS(args=dict(s2=s2), code=""" var inds = cb_obj.get('selected')['1d'].indices; var d1 = cb_obj.get('data'); var d2 = s2.get('data'); d2['Pulsar'] = [] d2['TOAs'] = [] d2['RawProfiles'] = [] d2['Period'] = [] d2['PeriodDerivative'] = [] d2['DM'] = [] d2['RMS'] = [] d2['Binary'] = [] for (i = 0; i < inds.length; i++) { d2['Pulsar'].push(d1['Pulsar'][inds[i]]) d2['TOAs'].push(d1['TOAs'][inds[i]]) d2['RawProfiles'].push(d1['RawProfiles'][inds[i]]) d2['Period'].push(d1['Period'][inds[i]]) d2['PeriodDerivative'].push(d1['PeriodDerivative'][inds[i]]) d2['DM'].push(d1['DM'][inds[i]]) d2['RMS'].push(d1['RMS'][inds[i]]) d2['Binary'].push(d1['Binary'][inds[i]]) } s2.trigger('change'); """ ) hover = p1.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("Pulsar's Name", '@Pulsar'), ('TOAs', '@TOAs'), ('RawProfiles', '@RawProfiles'), ('Period[s]', '@Period'), ('PeriodDerivative[s/s]', '@PeriodDerivative'), ('DM[pc/cc]', '@DM'), ('RMS[us]', '@RMS'), ('Binary', '@Binary'), ]) js_resources = INLINE.render_js() css_resources = INLINE.render_css() #Setting up layout layout = hplot(p1, p2) script, div = components(layout,INLINE) html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, _source=_source, Pulsar=Pulsar, _shape=_shape, favcolor=favcolor, bcolor=bcolor, _size=_size, lcolor=lcolor, toggle=toggle ) return encode_utf8(html)
def cluster_snapshot(): #args = flask.request.args # Get all the form arguments in the url with defaults #new_att = str(getitem(args, 'device_button','G_Swap_Used')) request_att=[] if request.method == "POST": #checked = 'att' in request.form request_att = request.form.getlist('att') #any_selected = bool(selected) request_att=[x.encode('UTF8') for x in request_att] #print(type(request_att)) #return str(request_att) #print (new_att) n_times=getData_N_Min_cluster(5) #n_times=cursor if n_times.count()==0: print ("No Data") #exit(1) data=dict() t=0 data=[] #Target only swap space #'compute-2-28' removed machine=['compute-2-29', 'compute-9-30', 'compute-9-36', 'compute-9-35','compute-9-34','compute-2-23', 'compute-2-25', 'compute-2-24', 'compute-2-27', 'compute-2-26', 'compute-6-29', 'compute-6-28', 'compute-6-25', 'compute-6-24', 'compute-6-27', 'compute-6-26', 'compute-6-23', 'compute-9-33', 'compute-9-32', 'compute-22-17', 'compute-22-16', 'compute-22-15', 'compute-22-14', 'compute-22-13', 'compute-22-12', 'compute-22-11', 'compute-22-18', 'compute-7-39', 'compute-7-38', 'compute-21-29', 'compute-21-28', 'compute-21-27', 'compute-21-26', 'compute-21-25', 'compute-21-24', 'compute-21-23', 'compute-5-1', 'compute-14-1', 'compute-14-2', 'compute-14-3', 'compute-14-4', 'compute-14-6', 'compute-14-7', 'compute-14-8', 'compute-13-6', 'compute-13-5', 'compute-13-4', 'compute-7-40', 'compute-13-2', 'compute-13-1', 'compute-14-30', 'compute-5-8', 'compute-14-32', 'compute-14-33', 'compute-14-34', 'compute-14-35', 'compute-18-18', 'compute-14-37', 'compute-14-38', 'compute-18-17', 'compute-5-3', 'compute-18-15', 'compute-5-5', 'compute-18-13', 'compute-5-7', 'compute-5-6', 'compute-6-2', 'compute-3-41', 'compute-6-1', 'compute-6-6', 'compute-6-7', 'compute-6-4', 'compute-6-5', 'compute-6-8', 'compute-13-28', 'compute-13-29', 'compute-13-26', 'compute-13-27', 'compute-13-24', 'compute-13-25', 'compute-13-23', 'compute-2-10', 'compute-2-11', 'compute-2-12', 'compute-2-14', 'compute-2-15', 'compute-2-16', 'compute-2-17', 'compute-2-18', 'compute-14-40', 'compute-2-8', 'compute-2-9', 'compute-2-7', 'compute-20-40', 'compute-1-9', 'compute-1-8', 'compute-6-11', 'compute-8-40', 'compute-6-14', 'compute-6-15', 'compute-6-16', 'compute-6-17', 'compute-6-10', 'compute-6-12', 'compute-6-13', 'compute-6-18', 'compute-4-29', 'compute-4-28', 'compute-23-38', 'compute-22-2', 'compute-23-36', 'compute-23-37', 'compute-23-34', 'compute-23-35', 'compute-4-27', 'compute-23-33', 'compute-4-25', 'compute-11-18', 'compute-8-38', 'compute-8-39', 'compute-11-17', 'compute-11-16', 'compute-22-40', 'compute-1-11', 'compute-1-10', 'compute-1-13', 'compute-1-12', 'compute-1-15', 'compute-1-14', 'compute-1-17', 'compute-1-16', 'compute-5-15', 'compute-5-14', 'compute-12-8', 'compute-5-16', 'compute-5-11', 'compute-5-10', 'compute-5-13', 'compute-5-12', 'compute-12-2', 'compute-12-3', 'compute-12-1', 'compute-12-6', 'compute-12-7', 'compute-12-4', 'compute-12-5', 'compute-12-27', 'compute-12-26', 'compute-12-18', 'compute-19-37', 'compute-19-36', 'compute-12-10', 'compute-12-11', 'compute-12-12', 'compute-12-13', 'compute-12-14', 'compute-12-15', 'compute-12-16', 'compute-12-17', 'compute-20-37', 'compute-20-36', 'compute-20-35', 'compute-20-39', 'compute-20-38', 'compute-23-39', 'compute-23-32', 'compute-4-26', 'compute-23-30', 'compute-12-23', 'compute-12-22', 'compute-12-25', 'compute-12-24', 'compute-19-39', 'compute-19-38', 'compute-12-29', 'compute-12-28', 'compute-19-35', 'compute-9-27', 'compute-21-40', 'compute-9-28', 'compute-9-29', 'compute-11-40', 'compute-21-38', 'compute-21-39', 'compute-21-30', 'compute-21-33', 'compute-21-34', 'compute-21-35', 'compute-21-36', 'compute-21-37', 'compute-5-28', 'compute-5-29', 'compute-5-24', 'compute-5-25', 'compute-5-26', 'compute-5-27', 'compute-5-23', 'compute-13-18', 'compute-13-13', 'compute-13-12', 'compute-13-11', 'compute-13-10', 'compute-13-17', 'compute-13-16', 'compute-13-15', 'compute-13-14', 'compute-14-15', 'compute-22-39', 'compute-22-38', 'compute-22-30', 'compute-22-33', 'compute-22-35', 'compute-22-34', 'compute-22-37', 'compute-22-36', 'compute-7-17', 'compute-7-16', 'compute-7-18', 'compute-5-17', 'compute-14-18', 'compute-14-12', 'compute-14-13', 'compute-14-10', 'compute-14-11', 'compute-14-16', 'compute-14-17', 'compute-14-14', 'compute-5-18', 'compute-21-8', 'compute-21-1', 'compute-21-2', 'compute-21-3', 'compute-21-4', 'compute-21-5', 'compute-21-6', 'compute-21-7', 'compute-4-30', 'compute-18-14', 'compute-23-27', 'compute-23-26', 'compute-12-37', 'compute-12-38', 'compute-21-11', 'compute-5-39', 'compute-5-38', 'compute-5-37', 'compute-5-36', 'compute-5-35', 'compute-5-34', 'compute-5-33', 'compute-5-32', 'compute-5-30', 'compute-22-3', 'compute-18-35', 'compute-22-1', 'compute-18-37', 'compute-22-7', 'compute-22-6', 'compute-22-5', 'compute-22-4', 'compute-22-8', 'compute-18-38', 'compute-18-39', 'compute-20-15', 'compute-20-17', 'compute-20-16', 'compute-20-18', 'compute-9-25', 'compute-2-38', 'compute-2-39', 'compute-2-36', 'compute-2-37', 'compute-2-34', 'compute-2-35', 'compute-2-32', 'compute-2-33', 'compute-2-30', 'compute-6-32', 'compute-6-33', 'compute-6-30', 'compute-6-36', 'compute-6-37', 'compute-6-34', 'compute-6-35', 'compute-6-38', 'compute-6-39', 'compute-9-26', 'compute-21-12', 'compute-21-13', 'compute-23-16', 'compute-23-17', 'compute-21-16', 'compute-21-17', 'compute-21-14', 'compute-21-15', 'compute-21-18', 'compute-23-18', 'compute-8-18', 'compute-8-16', 'compute-8-17', 'compute-11-39', 'compute-11-38', 'compute-22-28', 'compute-22-29', 'compute-22-23', 'compute-22-26', 'compute-22-27', 'compute-22-24', 'compute-22-25', 'compute-13-8', 'compute-13-7', 'compute-19-13', 'compute-19-15', 'compute-19-14', 'compute-19-17', 'compute-19-16', 'compute-14-27', 'compute-14-26', 'compute-14-25', 'compute-14-24', 'compute-14-23', 'compute-14-36', 'compute-14-29', 'compute-14-28', 'compute-18-16', 'compute-14-39', 'compute-3-39', 'compute-3-38', 'compute-5-2', 'compute-13-39', 'compute-13-38', 'compute-13-35', 'compute-13-34', 'compute-13-37', 'compute-13-36', 'compute-13-30', 'compute-13-33', 'compute-13-32', 'compute-3-40', 'compute-6-3', 'compute-13-40', 'compute-18-36', 'compute-23-29', 'compute-23-28', 'compute-23-25', 'compute-4-32', 'compute-4-33', 'compute-4-34', 'compute-4-35', 'compute-19-40', 'compute-18-40'] for t1 in n_times: devices=t1['data'].keys() for d in machine: lst=[] lst.append(d) for x in xrange(0,39): lst.append(t1['data'][d][x][1]) data.append(lst) t=t+1 res=['Device','G_Swap_Total', 'G_Swap_Free', 'G_Swap_Used', 'G_Proc_Run', 'G_Cpu_User', 'G_Cpu_Wio', 'G_Load_One', 'G_Load', 'G_Five', 'G_Load_Fifteen', 'G_Mem_Cached', 'G_Mem_Total', 'T_State', 'T_Slots', 'T_SlotsUsed', 'T_AvailMem(MB)', 'T_TotalMem(MB)/Swap', 'T_Time_Last_Rec', 'T_LoadAve', 'T_NetLoad(MB)', 'N_Status', 'N_Swap_Service', 'N_Swap_State', 'N_Swap_Info', 'N_IPMI_Service', 'N_IPMI_State', 'N_IPMI_Info', 'N_FreeSpace_Service', 'N_FreeSpace_State', 'N_FreeSpace_Info', 'N_CVMFS-OSG_Service', 'N_CVMFS-OSG_State', 'N_CVMFS-OSG_Info', 'N_CVMFS-CERN_Service', 'N_CVMFS-CERN_State', 'N_CVMFS-CERN_Info', 'N_CVMFS-CONDB_Service', 'N_CVMFS-CONDB_State', 'N_CVMFS-CONDB_Info'] att=['G_Swap_Used','G_Cpu_User', 'G_Cpu_Wio', 'G_Load_One', 'G_Load', 'G_Five', 'G_Load_Fifteen', 'G_Mem_Cached', 'T_AvailMem(MB)', 'T_LoadAve', 'T_NetLoad(MB)'] new_att=['Device','G_Swap_Used', 'G_Proc_Run', 'G_Cpu_User', 'G_Cpu_Wio', 'G_Load_One', 'G_Load', 'G_Five', 'G_Load_Fifteen', 'G_Mem_Cached', 'T_State', 'T_Slots', 'T_SlotsUsed','T_AvailMem(MB)', 'T_Time_Last_Rec', 'T_LoadAve','N_Status', 'N_Swap_State','N_IPMI_State','N_IPMI_Info','N_FreeSpace_State', 'N_CVMFS-OSG_State', 'N_CVMFS-CERN_State', 'N_CVMFS-CONDB_State'] #new_att=['Device','G_Swap_Used','T_AvailMem(MB)','G_Five','G_Cpu_Wio'] if request_att !=[]: new_att=request_att print (request_att) new_index=[] full_index=[] for i in new_att: full_index.append(res.index(i)) for a in att: new_index.append(res.index(a)) new_data=[] for d in data: core_count=int(d[14]) if core_count!=0: for i in new_index: d[i]=round(float(d[i])/core_count,2) d[i]=unicode(d[i]) tmp=[] for i in full_index: if i==res.index('N_IPMI_Info'): code_in_IPMI=re.findall(r'\d+',str(d[i])) if code_in_IPMI==[]: d[i]='0' else: d[i]=code_in_IPMI[0] tmp.append(d[i]) # tmp=[d[i] for i in full_index] new_data.append(tmp) df=pd.DataFrame(new_data) df.columns=new_att X = df.ix[:,1:len(df.columns)].values y = df.ix[:,0].values from sklearn.preprocessing import StandardScaler X_std = StandardScaler().fit_transform(X) from sklearn.decomposition import PCA as sklearnPCA sklearn_pca = sklearnPCA(n_components=2) Y_sklearn = sklearn_pca.fit_transform(X_std) x_corr=[] y_corr=[] label=[] x_l=[] y_l=[] new_dim_data=dict() for lab in machine: x_fact = Y_sklearn[y==lab, 0].tolist() y_fact = Y_sklearn[y==lab, 1].tolist() new_dim_data[lab] =[x_fact,y_fact] x_l.append(x_fact) y_l.append(y_fact) # Store new dimensions in database post={"date":datetime.datetime.utcnow(),"data":new_dim_data} d_var=db_new_dim.data post_id=d_var.insert_one(post).inserted_id for x in x_l: for x1 in x: x_corr.append(x1) for y in y_l: for y1 in y: y_corr.append(y1) l=len(x_l[0]) for lab in machine: for i in [lab for x in range(0,l)]: label.append(i) new_arr=np.array(zip(x_corr,y_corr)) k_means=KMeans(n_clusters=4) k_means.fit(new_arr) centroid=k_means.cluster_centers_ labels=k_means.labels_ colors=["green","red","cyan","yellow","blue"] color_src=[] for i in range(len(x_corr)): color_src.append(colors[labels[i]]) #output_file("toolbar.html") TOOLS="resize,crosshair,pan,wheel_zoom,box_zoom,reset,tap,previewsave,box_select,poly_select,lasso_select" source = ColumnDataSource( data=dict( x=x_corr, y=y_corr, desc=label, # colors=color_src, ) ) hover = HoverTool( tooltips=""" <div> <div> <span style="font-size: 17px; font-weight: bold;">@desc</span> <span style="font-size: 15px; color: #966;">[$index]</span> </div> <div> <span style="font-size: 15px;">Location</span> <span style="font-size: 10px; color: #696;">($x, $y)</span> </div> </div> """ ) #TOOLS= [BoxZoomTool(), ResetTool(),hover,ResizeTool(),WheelZoomTool()] TOOLS=["pan,wheel_zoom,box_zoom,reset,resize",hover] p = figure(plot_width=600, plot_height=600, tools=TOOLS, title="Mouse over the dots") p.circle('x', 'y', size=30, source=source,fill_color=color_src) p.scatter(centroid[:,0],centroid[:,1], color='black')#,s=200,linewidths=5,zorder=10) 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( 'cluster_snapshot.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def render_main_page(self, portability, plus = None): """ Renders the main page template for a plot and association expression information: gene/transcript list, annotations, (GO/KEGG) enrichment results. """ # TODO: make css customizing accessible csstables = ''' <style type="text/css"> .etables { dir: ltr; width: 1200px; } </style> ''' # Hardcoded, but (for the time being) it is for the best. # NOTE: if changed in the future, remember to keep paths relative. if portability == "batch": css_resources = '<link rel="stylesheet" href="static/bokeh-0.11.1.min.css" type="text/css" />' js_resources = ''' <script type="text/javascript" src="static/bokeh-0.11.1.min.js"></script> <script type="text/javascript"> Bokeh.set_log_level("info"); </script> ''' elif portability == "web": css_resources = ''' <link rel="stylesheet" href="https://cdn.pydata.org/bokeh/release/bokeh-0.11.1.min.css" type="text/css" />'''+csstables js_resources = ''' <script type="text/javascript" src="https://cdn.pydata.org/bokeh/release/bokeh-0.11.1.min.js"></script> <script type="text/javascript"> Bokeh.set_log_level("info"); </script> ''' # or "full", meaning full portability, but at a cost of increased filesize else: js_resources = INLINE.render_js() css_resources = INLINE.render_css() # ---------------------------------------------------------------------------- if plus is not None: bp = plus['bp'] mf = plus['mf'] cc = plus['cc'] kegg = plus['kegg'] # a small hack to pass along the significance # level (filter for GO enrichments) alpha = plus['alpha'] else: bp = None mf = None cc = None kegg = None alpha = 0.05 t_bp, t_mf, t_cc, t_kegg = self.process_enrichment_dict(bp, mf, cc, kegg, alpha) if bp is not None or mf is not None or cc is not None: goheader = '''<p style="font-size:20px; font-weight: bold;">GO term enrichment</p>''' else: goheader = "" if bp is not None: bp_info = ''' <span style="font-size:16px; font-weight: bold;">Biological Process</span> {% block table1a %} {{ tablegobp }} {% endblock %} <br/> ''' else: bp_info = "" if mf is not None: mf_info = ''' <span style="font-size:16px; font-weight: bold;">Molecular Function</span> {% block table1b %} {{ tablegomf }} {% endblock %} <br/> ''' else: mf_info = "" if cc is not None: cc_info = ''' <span style="font-size:16px; font-weight: bold;">Cellular Component</span> {% block table1c %} {{ tablegocc }} {% endblock %} <br/> <br/> ''' else: cc_info = "" if kegg is not None: kegg_info = ''' <span style="font-size:20px; font-weight: bold;">KEGG pathways enrichment</span> {% block table2 %} {{ tablekegg }} {% endblock %} <br/> ''' else: kegg_info = "" # ---------------------------------------------------------------------------- template = Template('''<!DOCTYPE html> <html lang="en"> <head> <meta charset="latin-1"> <title>{{ title }}</title> {{ js_resources }} {{ css_resources }} {{ script }} </head> <body> {{ div }} <br/> <br/> '''+goheader+bp_info+mf_info+cc_info+kegg_info+ ''' {% block table3 %} {{ annots }} {% endblock %} </body> </html> ''') html = template.render(js_resources=js_resources, css_resources=css_resources, script=self.script, div=self.div, title = self.title, tablegobp = t_bp, tablegomf = t_mf, tablegocc = t_cc, tablekegg = t_kegg, annots = self.render_gene_table(), ) return html
def status_newDimensions(): """ 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 device = str(getitem(args, 'device', 'compute-2-29')) time_frame = int(getitem(args, 'time_frame', 60)) #device='compute-2-29' # Create a polynomial line graph n_times=getData_N_Min_device(time_frame) #n_times=cursor if n_times.count()==0: return "No Data" #exit(1) data=dict() t=0 data=[] choose_machine=device x=[] y=[] label=[] time_label=[] i=1 for t1 in n_times: device_data=t1['data'][choose_machine] x.append(device_data[0][0]) y.append(device_data[1][0]) label.append(choose_machine) time_label.append(i) i+=1 #output_file("trace_new_dims.html") TOOLS="resize,crosshair,pan,wheel_zoom,box_zoom,reset,tap,previewsave,box_select,poly_select,lasso_select" source = ColumnDataSource( data=dict( x=x, y=y, desc=time_label, # colors=color_src, ) ) hover = HoverTool( tooltips=""" <div> <div> <span style="font-size: 17px; font-weight: bold;">@desc</span> <span style="font-size: 15px; color: #966;">[$index]</span> </div> <div> <span style="font-size: 15px;">Location</span> <span style="font-size: 10px; color: #696;">($x, $y)</span> </div> </div> """ ) TOOLS=["pan,wheel_zoom,box_zoom,reset,resize",hover] p = figure(plot_width=600, plot_height=600, tools=TOOLS, title="Mouse over the dots") p.line('x', 'y', source=source) # 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( 'embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
width: 50%; height: 400px; margin: auto; } </style> </head> <body> {% for key in div.keys() %} <div class="embed-wrapper"> {{ div[key] }} </div> {% endfor %} </body> </html> ''') js_resources = INLINE.render_js() css_resources = INLINE.render_css() filename = 'embed_multiple.html' html = template.render(js_resources=js_resources, css_resources=css_resources, script=script, div=div) with io.open(filename, mode='w', encoding='utf-8') as f: f.write(html) view(filename)
def user_response_times(username): df = MongoTools.get_user_response_time(username, usecache=True) ''' df['x_min'] = df['x_min'] / ( 24.0 * 60.0 * 60.0) df['x_max'] = df['x_max'] / ( 24.0 * 60.0 * 60.0) df['x_mean'] = df['x_mean'] / ( 24.0 * 60.0 * 60.0) ''' TOOLS = "pan,wheel_zoom,box_zoom,reset,save" w = 1000 * 10 p = figure( x_axis_type="datetime", tools=TOOLS, plot_width=1000, title="%s response times" % username, x_axis_label='month', y_axis_label='total days to respond after mention', ) p.xaxis.major_label_orientation = pi/4 p.grid.grid_line_alpha = 0.3 p.segment(df.index, df.x_max, df.index, df.x_min, color="black") ''' p.vbar( x=df.index, top=df.x_std, bottom=df.x_std + 1, width=100, fill_color="green", line_color="black" ) ''' # AttributeError: unexpected attribute 'height' to VBar, possible attributes are # bottom, fill_alpha, fill_color, js_callbacks, line_alpha, line_cap, line_color, # line_dash, line_dash_offset, line_join, line_width, name, tags, top, visible, # width or x p.vbar( x=df.index, top=df.x_mean, bottom=df.x_std, width=1000, fill_color="red", line_color="black" ) line = p.line( df.index, df.x_std, legend='stdev' ) line = p.line( df.index, df.x_mean, legend='mean', color='green' ) p.legend.location = "top_left" plot_script, plot_div = components(p) js_resources = INLINE.render_js() css_resources = INLINE.render_css() return render_template( 'user_response_times.html', data={ 'username': username, }, js_resources=js_resources, css_resources=css_resources, plot_script=plot_script, plot_div=plot_div, )
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 embedding(): from bokeh.plotting import figure from bokeh.resources import INLINE from bokeh.models import HoverTool, ColumnDataSource from bokeh.embed import components # from sklearn.manifold import Isomap articles = list(db.get_articles()) vect = vectorizer.load(config.vectorizer_filename) documents = map(article_to_document, articles) vects = vect.transform(documents) if issparse(vects): vects = vects.toarray() #E = PCA(n_components=2) E = make_pipeline( PCA(n_components=50), TSNE(n_components=2, perplexity=30, early_exaggeration=4, verbose=1) ) embed = E.fit_transform(vects) _, pca = E.steps[0] print(pca.explained_variance_ratio_) titles = map(lambda article: article.title, articles) titles = map(lambda title: to_multiline(title, max_line_length=30), titles) titles = map(lambda title: "".join(title), titles) links = [article.link for article in articles] authors = map(lambda article: article.authors, articles) authors = map(lambda author: [a.name for a in author], authors) authors = map(lambda author: ",".join(author), authors) authors = map(lambda author: to_multiline(author, max_line_length=30), authors) authors = map(lambda author: "".join(author)[0:40]+"[...]", authors) ds = ColumnDataSource( dict(x=embed[:, 0], y=embed[:, 1], title=titles, link=links, author=authors) ) tools = "resize, hover, save, pan,wheel_zoom,box_zoom,reset,resize" fig = figure(title="paper embedding", tools=tools, width=1400, height=800) fig.scatter("x", "y", source=ds, size=5, marker='cross') hover = fig.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("title", "@title"), ("author", "@author"), ("link", "@link"), ]) app.logger.info(INLINE.__dict__.keys()) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig, INLINE) html = render_template( 'figure.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return html
def member_stats(): """ This function carries out knn, and all the other relevant statistics + graphs produced by this toool. Some optmixations should be carried out and the plots should be abstracted in to their own seperate methods """ # Panda parsers pandifycv2 = lambda x: pd.DataFrame(x.__dict__, index=[0])[mlp.cat_vec2] pandifynv2 = lambda x: list(pd.DataFrame(x.__dict__, index=[0])[mlp.num_vec2].iloc[0]) out = db.session.query(Product).join(Products).join(User).filter(User.email == session["user"]) num_per_vec = np.mean(map(pandifynv2, out.all()), axis=0) # Parsing the user data in to a feature vector for k, ind in enumerate(map(pandifycv2, out.all())): cat_per_vec = deepcopy(mlp.cat_matrix) for i,d in enumerate(dict(ind).values()): cat_per_vec[i][int(list(d)[0])] = 1 # Averaging out to project the person in to product space. per_vec = list(np.sum(cat_per_vec, axis=0)) + list(num_per_vec) # for plotting purposes xn= per_vec[-4] yn =per_vec[-3] # Taking a random slice of size 700 out of our products to use as training # for the recomender model rand = random.randrange(700,5400) out2 = db.session.query(Product).outerjoin(Products).filter(Products.email == None)[rand-700:rand] # Ser up variables for parsing the trainign set cat_train = deepcopy(mlp.cat_matrix) num_train = map(pandifynv2, out2) train = list() end = enumerate(map(pandifycv2, out2) ) # Cur dictionary which is later on used as a data structure for creating # plots that sumarize costs per Manufacturer. # This is slow. Another aditional thing that takes place here is the collapse # of the cathegorical variables in to a binary representation. cur = dict() for k, ind in end: cat_train = deepcopy(mlp.cat_matrix) for i,d in enumerate(dict(ind).values()): cat_train[i][int(list(d)[0])] = 1 if mlp.cat_dics["manu"][int(list(ind["manu"])[0])] in cur: cur[mlp.cat_dics["manu"][int(list(ind["manu"])[0])]].append(num_train[k][1]) else: cur[mlp.cat_dics["manu"][int(list(ind["manu"])[0])]] =[ num_train[k][1]] train.append(list(chain(*cat_train)) + num_train[k]) # K-neares neighbors object fitting on the training set (products from # data base) nbrs = NearestNeighbors(n_neighbors=5, algorithm='ball_tree').fit(train) # predicting on current user who are his 5 closest prducts distances, indices = nbrs.kneighbors(per_vec) # Formatting the prediction in to a useful html renderable form prediction = [(out2[ind].__dict__["name"], out2[ind].__dict__["price"], out2[ind].__dict__["url"]) for ind in indices[0]] # variable used by bokeh for tooltip lables labl = [out2[ind].__dict__["name"] for ind in range(len(out2))] # Yes this is a crime getitem = lambda obj, item, default: default if item not in obj else obj[item] # price dimension of the training set # max number of delivery days of the training set trainprice = np.array(train)[:,-4] trainmax = np.array(train)[:,-3] # Get all the form arguments in the url with defaults args = request.args _from = int(getitem(args, '_from', 0)) to = int(getitem(args, 'to', 10)) # color dict for space plot ahead colors = [ "#%02x%02x%02x" % (int(r), int(g), 150) for r, g in zip(50+2*trainprice, 30+2*trainmax) ] TOOLS="resize,hover,crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select" # Data sources for the first plot source = ColumnDataSource( data=dict( x=trainprice, y=trainmax, label=labl ) ) source2 = ColumnDataSource( data=dict( x=[xn], y=[yn], label=["YOU"] ) ) fig = figure(title="Price vs Discount", tools=TOOLS, x_axis_label = "Price", y_axis_label = "Discount") fig.circle(x='x',y='y', radius=150, fill_color=colors, fill_alpha=0.6, line_color=None, source=source) fig.square(x='x',y='y', fill_color="yellow",size=20, line_color="green", source=source2) # fig.xgrid.grid_line_color = None fig.axis.major_tick_line_color = None fig.axis[0].ticker.num_minor_ticks = 0 fig.axis[1].ticker.num_minor_ticks = 0 fig.outline_line_color = "white" fig.xaxis.axis_line_color = "white" fig.yaxis.axis_line_color = "white" hover =fig.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("label", "@label"), ]) # Generation of the second plot plt_list = list() for k, v in cur.items(): plt_list.append((k, np.mean(v), np.std(v))) # Sorce data for tthe second plot plt_list.sort(key=lambda x: x[1]) source3 = ColumnDataSource( data=dict( x=range(len(np.array(plt_list)[:,0])), y= np.array(plt_list)[:,1], label=np.array(plt_list)[:,0] ) ) xr = range(len(np.array(plt_list)[:,0])) + list(reversed(range(len(np.array(plt_list)[:,0])))) yr = list((np.array(plt_list)[:,1].astype(float))) + list(reversed(np.array(plt_list)[:,1].astype(float) + np.array(plt_list)[:,2].astype(float))) source4 = ColumnDataSource( data=dict( x=xr, y= yr, label=list(np.array(plt_list)[:,0])*2 ) ) yr2 = list((np.array(plt_list)[:,1].astype(float))) + list(reversed(np.array(plt_list)[:,1].astype(float) - np.array(plt_list)[:,2].astype(float))) source5 = ColumnDataSource( data=dict( x=xr, y= yr2, label=list(np.array(plt_list)[:,0])*2 ) ) fig2 = figure(title="Manufacturer and their average cost", tools=TOOLS, x_axis_label = "Manufacturer", y_axis_label = "Averag Price") # fig2.circle(x='x', y='y' , source=source3) fig2.patch(x='x', y='y', color="#99d8c9" , source=source4) fig2.patch(x='x', y='y', color="#99d8c9" , source=source5) fig2.line(x='x' , y='y' , source=source3) fig2.xgrid.grid_line_color = None fig2.axis[0].major_label_text_font_size = "0pt" fig2.axis.major_tick_line_color = None fig2.axis[0].ticker.num_minor_ticks = 0 fig2.axis[1].ticker.num_minor_ticks = 0 fig2.outline_line_color = "white" fig2.xaxis.axis_line_color = "white" fig2.yaxis.axis_line_color = "white" hover2 =fig2.select(dict(type=HoverTool)) hover2.tooltips = OrderedDict([ ("label", "@label"), ]) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig, INLINE) script2, div2 = components(fig2, INLINE) return render_template('user/member_stats.html', plot_script=script, plot_div=div, plot_script2=script2, plot_div2=div2, js_resources=js_resources, css_resources=css_resources, _from=_from, to=to, prediction=prediction)
def ngramgraph(): pwidth = 550 pheight = 400 # Grab the inputs arguments from the URL # This is automated by the button args = request.args # Get all the form arguments in the url with defaults #default to obama query = getitem(args, 'input', 'obama') #splittign the query splitstr = query.split(",") #Fetching data from the query XX = [] YY = [] YY2 = [] lgnd = [] cnt = 0 source = [] for strs in splitstr: ngram = strs.rstrip().lstrip().lower() #print "ngram = ", ngram subreddits = get_subreddit_data(ngram) #print "subreddits", subreddits if len(subreddits) > 0: mainsr = subreddits[0]['subreddit'] lgndstr = "{0}::{1}".format(ngram, mainsr) data = get_timeseries_data(ngram, mainsr) x = [ k['date'] for k in data] y = [k['count'] for k in data] y2 = [k['percentage'] for k in data] else: x = Xtime y = [0] * len(Xtime) y2 = y lgndstr = ngram lgnd.append(lgndstr) XX.append(x) YY.append(y) YY2.append(y2) cnt +=1 source.append(dict(xx = [datetime.strftime(kk, "%Y-%m") for kk in x], yy = y, desc = [lgndstr] * len(x))) #Creating figure box fig = figure(title="Trends (absolute count)", x_axis_label = "Time", y_axis_label = "Word Count", width=pwidth, height=pheight, x_axis_type="datetime" ) #Plot the lines for k in range(cnt): fig.line(XX[k], YY[k], color=random.choice(colors), legend = lgnd[k], line_width=2) fig.legend.orientation = "top_left" fig2 = figure(title="Trends (ratio)", x_axis_label = "Time", y_axis_label = "Ratio", width=pwidth, height=pheight, x_axis_type="datetime" ) #Plot the lines for k in range(cnt): fig2.line(XX[k], YY2[k], color=random.choice(colors), legend = lgnd[k], line_width=2) fig2.legend.location = "top_left" p = hplot(fig, fig2) # Configure resources to include BokehJS inline in the document. # For more details see: js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(p, INLINE) #script2, div2 = components(fig2, INLINE) html = render_template( 'Ngram.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources ) return encode_utf8(html)