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,)
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 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 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 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 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 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 systemfetch(): with Timer(key="get_systemfetch"): Session = current_app.config['dbsession'] cursn = getSnapshotfromTime(datetime.datetime.now()) snapshots = getLastNSnapshots(cursn, n=5) nWeeksago = snapshots[-1] cnts = defaultdict(int) data = {} with Timer(key="query_systemfetch"): for r in Session.query(PortalSnapshot.snapshot, PortalSnapshot.start, PortalSnapshot.end - PortalSnapshot.start).filter( PortalSnapshot.snapshot > nWeeksago): sn, start, dur = r[0], r[1], r[2] cnts[sn] += 1 d = data.setdefault(sn, {}) if dur is not None: ds = d.setdefault(start, []) ds.append(dur.total_seconds()) for sn, d in data.items(): dd = [] gstart = min(d.keys()) for start, durations in d.items(): for dur in durations: delta = (start - gstart).total_seconds() + dur dd.append(delta) data[sn] = dd with Timer(key="plot_systemfetch"): p = fetchProcessChart(data, cnts) script, div = components(p) js_resources = INLINE.render_js() css_resources = INLINE.render_css() return render("odpw_system_fetch.jinja", plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources)
def flask_example(): p = figure() x = [1, 2, 3, 4, 5] y = [6, 7, 2, 4, 5] p.line(x, y, line_width=3) script, div = components(p) js_resources = INLINE.render_js() css_resources = INLINE.render_css() html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def home(): ve_script, ve_div = make_vaccine_effectiveness_plot() (nseq_script, nseq_div), (nseq_metadata) = \ make_num_sequences_per_year_plot() evo_script, evo_div = make_coord_plots() js_resources = INLINE.render_js() css_resources = INLINE.render_css() seqs = [s for s in SeqIO.parse('data/twoQ_predictions.fasta', 'fasta')] n_seqs = len(seqs) return render_template('index.html', js_resources=js_resources, css_resources=css_resources, evo_script=evo_script, evo_div=evo_div, nseq_script=nseq_script, nseq_div=nseq_div, nseq_metadata=nseq_metadata, n_seqs=n_seqs, ve_script=ve_script, ve_div=ve_div)
def graphs(): with open( '/home/mcian91/news_analysis/pickles/lda_model_' + identifier + '.pkl', 'rb') as f: lda_model = pickle.load(f) vis_plot = codecs.open( "/home/mcian91/news_analysis/web_app/static/img/pyLDAvis_graphs/pyLDAvis_" + identifier + ".html", 'r') pyLDAvis_html = vis_plot.read() js_resources = INLINE.render_js() css_resources = INLINE.render_css() html = render_template('graphs.html', js_resources=js_resources, css_resources=css_resources, pyLDAvis_html=pyLDAvis_html) return encode_utf8(html)
def plot(content, data=[]): x = list(range(0, 100 + 1)) fig = figure(title="Polynomial") 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(): 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') js_resources = INLINE.render_js() css_resources = INLINE.render_css() 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(): 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 chuyến bay trong ngày"] # 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 unigram(): df = pd.read_csv('data/unigramfreqweight.csv') unigram = df['unigrams'].head(10) frequency = df['freq'].head(10) colors = [ "#F89926", "#F89926", "#F89926", "#F89926", "#F89926", "#F89926", "#F89926", "#F89926", "#F89926 ", "#F89926" ] source = ColumnDataSource( data=dict(unigram=unigram, frequency=frequency, color=colors)) p = figure(x_range=unigram, plot_height=400, title="Top 10 Unigrams based on Frequency", toolbar_location=None, tools="hover", tooltips="@unigram @frequency", x_axis_label='Unigram', y_axis_label='Frequency') p.vbar(x='unigram', top='frequency', width=0.9, color='color', source=source) p.xgrid.grid_line_color = None p.y_range.start = 0 p.legend.orientation = "horizontal" p.legend.location = "top_right" uni_script, uni_div = components(p) return render_template( 'unigram.html', uni_script=uni_script, uni_div=uni_div, js_resources=INLINE.render_js(), css_resources=INLINE.render_css(), ).encode(encoding='UTF-8')
def plot(): ticker = request.form['name_ticker'] df = get_stock(ticker) fig = figure(x_axis_type="datetime", title="Data from Quandle WIKI set") fig.line(df['Date'], df['Close'], color='#33A02C') js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script, div = components(fig) html = render_template( 'home.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
async def get_category(request: Request): # async加了就支持异步 把Request赋值给request js_res = INLINE.render_js() css_res = INLINE.render_css() swl1_dict = get_data_sw_level('swl1') swl2_dict = get_data_sw_level('swl2') swl3_dict = get_data_sw_level('swl3') concept_dict = get_data_concept_list() industry_dict = get_data_stocksbasic_industry_list() return tmp.TemplateResponse( 'industry_category.html', { 'request': request, 'swl1_dict': swl1_dict, 'swl2_dict': swl2_dict, 'swl3_dict': swl3_dict, 'concept_dict': concept_dict, 'industry_dict': industry_dict, "js_res": js_res, "css_res": css_res })
def index(): if request.method == 'GET': return render_template('userinfo_pg1.html') else: stock = request.form['ticker'] dates, closing_prices = get_stock_data(stock, 30) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = plot(dates, closing_prices) return render_template('userinfo_pg2.html', stock_symbol=stock.upper(), script=script, div=div, js_resources=js_resources, css_resources=css_resources)
def generate_html(plot): js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(plot) template_file = os.path.join(os.path.dirname(__file__), "timeline.template") with open(template_file) as fp: template_src = fp.read() template = jinja2.Template(template_src) html = template.render(plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, custom_css=r""" .xl-hidden { visibility: hidden; } .xl-sync button:before{ content: "\f021 "; display: inline-block; font: normal normal normal 14px/1 FontAwesome; font-size: inherit; text-rendering: auto; /*-webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; -webkit-animation: fa-spin 2s infinite linear; animation: fa-spin 2s infinite linear;*/ } .bk-toolbar-box { position: fixed !important; z-index: 999999999; } .bk-toolbar-above{ background-color: rgba(255, 255, 255, 0.85); } """, title="TensorFlow Timeline", custom_header='', custom_js='') return encode_utf8(html)
def home(): historical = get_historical_data() chart = create_chart(historical) # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script, div = components(chart) return render_template( 'main/home.html', points=len(historical), plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, )
def predictions(): plots_preds = db.plots_preds.find_one({'_id': 1}) script, div = plots_preds['script_preds'], plots_preds['div_preds'] last_updated = db.last_updated.find_one({'_id': 1})['last_updated'] js_resources = INLINE.render_js() css_resources = INLINE.render_css() params = db.models_params.find_one({'_id': 1}) sol = params['sol'] c = params['logistic_values'][2] end_date = datetime.strptime("22.01.2020", "%d.%m.%Y") + timedelta(sol) end_date_str = end_date.strftime("%d.%m.%Y") return render_template("predictions.html", js_resources=js_resources, css_resources=css_resources, script=script, div=div, last_updated=last_updated, end_date_str=end_date_str, n_confirmed=int(c))
def index(): ''' # Determine the selected option current_pricetype = request.args.get("pricetype") if current_pricetype == None: current_pricetype = price_type_list # Create the plot plot = create_figure(current_pricetype) # Embed plot into HTML via Flask Render script, div = components(plot) return render_template("quandlWIKIprice.html", script=script, div=div, price_type_list=price_type_list, current_pricetype=current_pricetype) ''' if request.method == 'GET': return render_template('WIKIpriceinfo.html') else: # request was a post app.vars['tickersymbol'] = request.form['tickersymbol'] for option in price_type_list: name = request.form.get(option) if name == None: app.vars[option] = 0 else: app.vars[option] = 1 ticker = app.vars['tickersymbol'] data = get_data(app.vars) p = create_figure(data, ticker) # Embed plot into HTML via Flask Render script, div = components(p) js_resources = INLINE.render_js() css_resources = INLINE.render_css() return render_template("end.html", plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources)
def learn_more_investing(): data = yf.download(tickers='SPY', period='5y') data = data.reset_index() p = figure(title="Stock chart of 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'], x=data["Date"], color="green") js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(p) return render_template('learn_more_investing.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources)
def index(): if request.method == 'POST': company = request.form[ 'ticker'] # input from the typing (import from html) time_span = [request.form['start_date'], request.form['end_date']] data = grab_data(company, time_span) try: #add input to the table, stack the tasks fig = figure(x_axis_type="datetime", title="Stock Closing Prices", plot_width=600, plot_height=600) fig.grid.grid_line_alpha = 0.3 fig.xaxis.axis_label = 'Date' fig.yaxis.axis_label = 'Price' fig.line(datetime(data['date']), data['close'], line_width=2, legend=company, 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( 'bokeh.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html) # return redirect('/bokeh', data= data) except: return 'There was an issue searching your ticker' else: return render_template('index.html')
def showGraph(): # code to generate graph here # assign shome shorter names to user input variables for readability ticker = app.vars['ticker'] start_date = app.vars['start_date'] end_date = app.vars['end_date'] quote_type = app.vars['quote_type'] figure_title_string = ticker + ' ' + quote_type # fetch data from quandl data = quandl.get_table('WIKI/PRICES', qopts={'columns': ['ticker', 'date', quote_type]}, ticker=[ticker], date={ 'gte': start_date, 'lte': end_date }) x = data['date'].astype(datetime) y = data[quote_type] p = figure(title=figure_title_string.upper(), x_axis_type='datetime', x_axis_label='Date', y_axis_label=quote_type) p.line(x, y, legend=ticker.upper(), line_width=2) # code to render HTML with embedded bokeh plot # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script, div = components(p) html = render_template( 'bokeh_template.html', ticker=ticker.upper(), plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
async def get_limit_stockcode(request: Request, stockcode: str, n: int): # async加了就支持异步 把Request赋值给request js_res = INLINE.render_js() css_res = INLINE.render_css() p = draw_candlestick_nday(stockcode, int(n + 10)) # 额外的参数可有可无 script, div = components(p) data_stockcode = get_data_stockcode('limit_' + str(n), stockcode) return tmp.TemplateResponse( 'limit_stocks.html', { 'request': request, 'days': n, 'stockscount': len(data_stockcode), # 额外的参数可有可无 'stocks': data_stockcode, "p_script": script, "p_div": div, "js_res": js_res, "css_res": css_res })
async def get_limit_lastday( request: Request): # async加了就支持异步 把Request赋值给request #data_lastday = getindex('limit_lastday') js_res = INLINE.render_js() css_res = INLINE.render_css() plot1 = draw_vbar('limit_last', 'strth', 50, -1) script1, div1 = components(plot1) data_lastday = get_data_sort('limit_last', 'strth', -1, 9999) return tmp.TemplateResponse( 'limit_stocks_lastday.html', { 'request': request, # 一定要返回request 'stockscount': len(data_lastday), # 额外的参数可有可无 'stocks': data_lastday, "p_script1": script1, "p_div1": div1, "js_res": js_res, "css_res": css_res })
def expense(): 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_expense_reports(user_id)) html = render_template("report/expense.html", plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, title="Анализ расходов") return encode_utf8(html)
async def get_stocks_detail( stockcode: str, request: Request): # async加了就支持异步 把Request赋值给request stock_daily_dict = get_data_dict('daily_' + stockcode) stock_moneyflow_pct = stocks_moneyflow.cal_moneyflow_pct(stockcode) js_res = INLINE.render_js() css_res = INLINE.render_css() p = draw_candlestick('daily_' + stockcode) script, div = components(p) p1 = draw_candlestick('weekly_' + stockcode) script1, div1 = components(p1) p2 = draw_line_param_avgline('daily_' + stockcode, 'close', 'red', 'close', 10, 'black') script2, div2 = components(p2) p3 = draw_line_param('daily_' + stockcode, 'pct_chg', 'green') script3, div3 = components(p3) p4 = draw_line_param_avgline('daily_' + stockcode, 'vol', 'blue', 'vol', 10, 'red') script4, div4 = components(p4) p5 = draw_line_macd('daily_' + stockcode) script5, div5 = components(p5) return tmp.TemplateResponse( 'stock_detail.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_script1": script1, "p_div1": div1, "p_script2": script2, "p_div2": div2, "p_script3": script3, "p_div3": div3, "p_script4": script4, "p_div4": div4, "p_script5": script5, "p_div5": div5, "js_res": js_res, "css_res": css_res })
def index(): if request.method == 'POST': Team = request.form['Team'] Stat = request.form['Stat'] script, div = make_fancy_plot(Team, Stat) js_resources = INLINE.render_js() css_resources = INLINE.render_css() html = render_template('index.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources) return encode_utf8(html) else: return render_template('index.html', plot_script='', plot_div='', js_resources='', css_resources='')
def zagreb(): fig = figure(title='Pollen in Zagreb', x_axis_label='Date', y_axis_label='Level', x_axis_type='datetime', plot_width=600, plot_height=600) start_date = datetime.date.today() + datetime.timedelta(-30) query = """ SELECT array_agg(date), array_agg(level), name FROM pollen WHERE city='Zagreb' AND date >= '%s' GROUP BY name """ % start_date result = db.session.execute(query).fetchall() for city in result: fig.line( x=city._row[0], y=city._row[1], color='#{:06x}'.format(random.randint(0, 0xFFFFFF)), legend_label=city._row[2], ) fig.legend.location = 'top_left' # 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 graficas(): #Traer Todos los Datos response = requests.get(urlAPI + 'all') data = response.json() df = DataFrame.from_dict(data) z = df.query('completo == 1')[['tiempocomp', 'id_tarea']] yvalue = z["tiempocomp"].head() ymap = yvalue.map(lambda x: getHours(x)) yl = list(set(ymap)) auxData = z['id_tarea'].head() x = auxData.map(lambda x: str(x)) xl = list(set(x.sort_values(ascending=True))) x = xl colors = ["#c9d9d3", "#718dbf", "#e84d60"] fig = figure(x_range=x, plot_width=900, plot_height=600, x_axis_label='Tareas', y_axis_label='Tiempo', title="Tareas Completadas", toolbar_location=None, tools="") fig.vbar(x=x, width=0.7, bottom=0, top=yl, color=colors) # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script, div = components(fig) html = render_template( 'grafica.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return (html)
def injection(): p, inputs = define_inj_plot() # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # Embed plot into HTML via Flask Render script, div = components(p) html = render_template('injection.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, time=inputs['time'], depth=inputs['depth'], wd=inputs['wd'], kop=inputs['kop'], eob=inputs['eob'], build_angle=inputs['build_angle'], kop2=inputs['kop2'], eob2=inputs['eob2'], sod=inputs['sod'], eod=inputs['eod'], q=inputs['q'], tin=inputs['tin'], rhof=inputs['rhof'], rhot=inputs['rhot'], rhoc=inputs['rhoc'], rhor=inputs['rhor'], rhofm=inputs['rhofm'], rhow=inputs['rhow'], rhocem=inputs['rhocem'], n_casings=inputs['n_casings'], dro=inputs['dro'], dri=inputs['dri'], dto=inputs['dto'], dti=inputs['dti'], wtg=inputs['wtg'], gt=inputs['gt']) return encode_utf8(html)
def generate_sample_graph(msg): """ Generate a sample dummy diagram """ # Create a polynomial line graph with those arguments x_list = list(range(1, 6)) fig = figure(title="Polynomial") fig.line(x_list, [i**2 for i in x_list], color="blue", line_width=2) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig) html = flask.render_template('demo.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, form=ExpenseForm(), message=msg) return encode_utf8(html)
def cahrt(bars_count): if bars_count <= 0: bars_count = 1 data = {"days": [], "bugs": [], "costs": []} for i in range(1, bars_count + 1): data['days'].append(str(i)) data['bugs'].append(random.randint(1, 100)) data['costs'].append(random.uniform(1.00, 1000.00)) hover = create_hover_tool() plot = create_bar_chart(data, "Bugs found per days", "days", "bugs", hover) script, div = components(plot) return render_template("chart.html", bars_count=bars_count, the_div=div, the_script=script, js_resources=INLINE.render_js(), css_resources=INLINE.render_css())
def bokeh(): vacation = read_csv("df_vacation.csv", parse_dates=True) print(vacation.head()) fig = get_figure(vacation) # 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) return render_template("index.html")
def get_news(pname): fullname = handle_name(pname) retlist = model.get_top_news(fullname) news_list = retlist[0] plot = retlist[1] # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(plot) html = render_template('news.html', player_name=fullname, news_list=news_list, plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, pname=pname) 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 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 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 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 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)
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 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)
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 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 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 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 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)
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())
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 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