def create_line_plot(df): """ create a mg line plot Args: df (pandas.DataFrame): data to plot """ fig = Figure("/mg/line_plot/", "mg_line_plot") fig.graphics.transition_on_update(True) fig.graphics.animate_on_load() fig.layout.set_size(width=450, height=200) fig.layout.set_margin(left=40, right=40) return LineChart(df, fig, "Date", ["value"], init_params={"Data": "Steps"}, timeseries=True)
def create_histogram(df): """ create a mg line plot Args: df (pandas.DataFrame): data to plot """ fig = Figure("/mg/histogram/", "mg_histogram") fig.layout.set_size(width=450, height=200) fig.layout.set_margin(left=40, right=40) fig.graphics.animate_on_load() # Make a histogram with 20 bins return Histogram(df, fig, "value", 20, init_params={"Data": "Steps"})
def create_scatterplot(df): """ create a mg line plot Args: df (pandas.DataFrame): data to plot """ fig = Figure("/mg/scatter/", "mg_scatter") fig.layout.set_size(width=450, height=200) fig.layout.set_margin(left=40, right=40) fig.graphics.animate_on_load() init_params = {"Data": "Steps"} def get_data(): y = request.args.get("Data", "Steps") return jsonify(ScatterPlot.to_json(df, "Steps", y)) # Make a histogram with 20 bins return ScatterPlot(df, fig, "Steps", "Distance", init_params={}, route_func=get_data)
def create_line_plot(df, i, name, n, titel): """ create a mg line plot Args: df (pandas.DataFrame): data to plot """ fig = Figure("/mg/line_plot" + str(i), "mg_line_plot") fig.graphics.transition_on_update(True) fig.graphics.animate_on_load() fig.graphics.point_size(2.0) fig.layout.set_size(width=450 + 10 * i, height=210) fig.layout.set_margin(left=40, right=40) fig.axes.set_min_y_from_data(True) fig.axes.set_inflator(1.005) fig.axes.set_xticks_count(n) return LineChart(df=df, figure=fig, x="Year", y=["value"], init_params={"Data": name}, timeseries=True, title=titel)
filter_style="''") # Read in the data and stack it, so that we can filter on columns df = pd.read_csv("fitbit_data.csv") sf = df.set_index("Date").stack().reset_index() sf = sf.rename(columns={"level_1": "Data", 0: "value"}) # Make a Button cols = [c for c in df.columns if c != "Date"] btn = SelectButton("Data", cols, "Data", "Steps") # Make a FilterFrame and add the button to the UI ui.add_filter(btn) # Make a Figure, add some settings, make a line plot fig = Figure("/mgchart/", "mychart") fig.graphics.transition_on_update(True) fig.graphics.animate_on_load() fig.layout.set_size(width=450, height=200) fig.layout.set_margin(left=40, right=40) lc = LineChart(sf, fig, "Date", ["value"], init_params={"Data": "Steps"}, timeseries=True) ui.add_chart(lc) # Now make a FilterFrame for the histogram hFig = Figure("/mghist/", "myhist") hFig.layout.set_size(width=450, height=200) hFig.layout.set_margin(left=40, right=40) hFig.graphics.animate_on_load() # Make a histogram with 20 bins hc = Histogram(sf, hFig, "value", 20, init_params={"Data": "Steps"}) ui.add_chart(hc)
def uploaded_file(filename): extension = get_extension(filename) path = os.path.join(app.config['UPLOAD_FOLDER'], extension, filename) txt_path = os.path.join(app.config['UPLOAD_FOLDER'], 'txt', filename) if extension == "pdf": txt_path += '.txt' if not os.path.isfile(txt_path): #Layout preservation crucial to preserve clues about tabular data cmd = "pdftotext -layout %s %s" % (path, txt_path) os.system(cmd) min_columns = request.args.get('min_columns') tables = return_tables(txt_path) #Construct histogram lines_per_page = 80 nr_data_rows = [] for b, t in tables.iteritems(): e = t['end_line'] #print b, e for l in range(b, e): page = l / lines_per_page if len(nr_data_rows) <= page: nr_data_rows += ([0] * (page - len(nr_data_rows) + 1)) nr_data_rows[page] += 1 dr = pd.DataFrame() dr['value'] = nr_data_rows dr['page'] = range(0, len(dr)) js_layout = filename + ".js" ui_show = UILayout("FilterChart", "../static/bower_components/pyxley/build/pyxley.js", "component_id", filter_style="''") if filename in all_charts: print "old/update ui", filename path_to_fig = '/show/line/' + filename #del all_charts[filename] #hFig = Figure(path_to_fig, "line") #bc = LineChart(dr, hFig, "page", ["page"], "Rows containing Data per Page") elif True: print "new ui", filename # Make a Button cols = ["page"] btn = SelectButton("Data", cols, "Data", "Data Rows per Page") # Make a FilterFrame and add the button to the UI ui_show.add_filter(btn) # Now make a FilterFrame for the histogram path_to_fig = '/show/line/' + filename hFig = Figure(path_to_fig, "line") hFig.layout.set_size(width=1000, height=300) hFig.layout.set_margin(left=80, right=80) #hFig.graphics.animate_on_load() bc = LineChart(dr, hFig, "page", ["value"], "Rows containing Data per Page") ui_show.add_chart(bc) all_charts[filename] = bc sb = ui_show.render_layout(app, "./static/ug/" + js_layout) _scripts = ["ug/" + js_layout] notices = ['Extraction Results for ' + filename, 'Ordered by lines'] dfs = (table_to_df(table).to_html() for table in tables.values()) headers = [] for t in tables.values(): if 'header' in t: headers.append(t['header']) else: headers.append('-') line_nrs = [('line %i-%i' % (t['begin_line'], t['end_line'])) for t in tables.values()] #headers = ['aslkdfjas', ' alsdkfjasoedf'] return render_template('index.html', title=TITLE + ' - ' + filename, base_scripts=scripts, page_scripts=_scripts, css=css, notices=notices, tables=dfs, headers=headers, line_nrs=line_nrs)
for k, table in tables.iteritems(): df = table_to_df(table) print k, ' !!! ', table['header'], ' -----------------' print df.head() #print dr # Make a Button cols = [c for c in df.columns if c != "Date"] btn = SelectButton("Data", cols, "Data", "Steps") # Make a FilterFrame and add the button to the UI ui.add_filter(btn) # Now make a FilterFrame for the histogram hFig = Figure("/mghist/", "myhist") hFig.layout.set_size(width=450, height=200) hFig.layout.set_margin(left=40, right=40) hFig.graphics.animate_on_load() # Make a histogram with 20 bins hc = Histogram(sf, hFig, "value", 20, init_params={"Data": "Steps"}) ui.add_chart(hc) # Let's play with our input df["Date"] = pd.to_datetime(df["Date"]) df["week"] = df["Date"].apply(lambda x: x.isocalendar()[1]) gf = df.groupby("week").agg({ "Date": [np.min, np.max], "Steps": np.sum, "Calories Burned": np.sum, "Distance": np.sum