def route_change_options(library_name, model_name, chart_id=-1): Model = str_to_object(model_name) LPO = str_to_object(f'{library_name.capitalize()}PlotOptions') LOF = str_to_object(f'{library_name.capitalize()}OptionsForm') id_library_options = f'id_{library_name}_options' ''' Get values from form. ''' library_form = LOF() if library_form.validate_on_submit(): kwargs = dict() kwargs['color'] = library_form.color.data kwargs['bg_color'] = library_form.bg_color.data kwargs['line_width'] = library_form.line_width.data if library_name == 'matplotlib': kwargs['outline_color'] = library_form.outline_color.data kwargs['line_style'] = library_form.line_style.data kwargs['marker'] = library_form.marker.data if library_name == 'matplotlib': kwargs['flag_bar_plot'] = library_form.flag_bar_plot.data kwargs['flag_scatter_plot'] = library_form.flag_scatter_plot.data kwargs['flag_show_grid'] = library_form.flag_show_grid.data if library_name != 'pygal': kwargs['x_label'] = library_form.x_label.data kwargs['y_label'] = library_form.y_label.data kwargs['title'] = library_form.title.data if library_name != 'pygal': kwargs['flag_logscale_x'] = library_form.flag_logscale_x.data kwargs['flag_logscale_y'] = library_form.flag_logscale_y.data new_options = LPO(**kwargs) db.session.add(new_options) db.session.commit() ''' get lastly_added record ''' recently_added = get_recently_added_record(db, LPO.__name__) new_options_id = recently_added.id if model_name != "FileDataPoint" and chart_id != -1: current_chart = Model.query.get(chart_id) setattr(current_chart, id_library_options, new_options_id) db.session.commit() else: fpo_record = FilePlotOptions.query.first() setattr(fpo_record, id_library_options, new_options_id) db.session.commit() flash(f'Changed options for {library_name}!', 'success') if model_name == "FileDataPoint" and chart_id == -1: return redirect(url_for('route_show_data_from_file')) else: return redirect( url_for('route_show_data', model_name=model_name, chart_id=chart_id))
def route_add_data(model_name): """ Inserts points to the database to the specific model based on values from forms.""" common_models = ['Sinus', 'Cosinus', 'Exponential', 'SquareRoot'] if model_name in common_models: form = DataForm() elif model_name == 'SquareFunc': form = SquareFuncForm() elif model_name == 'CustomEquation': form = CustomEquationForm() if form.validate_on_submit(): Model = str_to_object(model_name) form_step_data = form.step.data if form.step.data is not None else 0.1 model_kwargs = dict() model_kwargs['x_begin'] = form.begin.data model_kwargs['x_end'] = form.end.data model_kwargs['step'] = form_step_data model_kwargs['id_matplotlib_options'] = get_default_matplotlib_options( db, as_dict=False).id model_kwargs['id_seaborn_options'] = get_default_seaborn_options( db, as_dict=False).id model_kwargs['id_bokeh_options'] = get_default_bokeh_options( db, as_dict=False).id model_kwargs['id_plotly_options'] = get_default_plotly_options( db, as_dict=False).id model_kwargs['id_pygal_options'] = get_default_pygal_options( db, as_dict=False).id if model_name in common_models: model_kwargs['a'] = form.coef_a.data model_kwargs['b'] = form.coef_b.data model_kwargs['c'] = form.coef_c.data model_kwargs['d'] = form.coef_d.data elif model_name == 'SquareFunc': model_kwargs['a'] = form.coef_a.data model_kwargs['p'] = form.coef_p.data model_kwargs['q'] = form.coef_q.data elif model_name == 'CustomEquation': model_kwargs['equation'] = form.equation.data model_object = Model(**model_kwargs) db.session.add(model_object) db.session.commit() chart_id = get_recently_added_record(db, model_name).id flash( f'Range <{form.begin.data}, {form.end.data}> has been successfully added to the database!', 'success') return redirect( url_for('route_show_data', model_name=model_name, chart_id=chart_id)) return render_template('add_data.html', form=form, model_name=model_name)
def route_show_data_from_file(): Model = str_to_object("FileDataPoint") records = Model.query.all() kw_options = dict() matplotlib_options_id = FilePlotOptions.query.first().id_matplotlib_options kw_options['matplotlib_options'] = json.dumps( MatplotlibPlotOptions.get_options(matplotlib_options_id)) seaborn_options_id = FilePlotOptions.query.first().id_seaborn_options kw_options['seaborn_options'] = json.dumps( SeabornPlotOptions.get_options(seaborn_options_id)) bokeh_options_id = FilePlotOptions.query.first().id_bokeh_options kw_options['bokeh_options'] = BokehPlotOptions.get_options( bokeh_options_id) plotly_options_id = FilePlotOptions.query.first().id_plotly_options kw_options['plotly_options'] = PlotlyPlotOptions.get_options( plotly_options_id) pygal_options_id = FilePlotOptions.query.first().id_pygal_options kw_options['pygal_options'] = PygalPlotOptions.get_options( pygal_options_id) kwargs = dict() bokeh_chart = make_chart_bokeh("FileDataPoint", -1, kw_options.get('bokeh_options', dict())) script_bokeh, div_bokeh = bokeh_components(bokeh_chart) kwargs["script_bokeh"] = script_bokeh kwargs["div_bokeh"] = div_bokeh kwargs[ "script_plotly"] = '<script src="https://cdn.plot.ly/plotly-latest.min.js"></script> ' kwargs["div_plotly"] = make_chart_plotly( "FileDataPoint", -1, kw_options.get('plotly_options', dict())) pygal_chart = make_chart_pygal("FileDataPoint", -1, kw_options.get('pygal_options', dict())) kwargs["src_pygal"] = pygal_chart.render_data_uri() kwforms = dict() kwforms['matplotlib_form'] = MatplotlibOptionsForm() kwforms['seaborn_form'] = SeabornOptionsForm() kwforms['bokeh_form'] = BokehOptionsForm() kwforms['plotly_form'] = PlotlyOptionsForm() kwforms['pygal_form'] = PygalOptionsForm() return render_template('show_data.html', model_name="FileDataPoint", chart_id=-1, records=records, **kwargs, **kwforms, **kw_options)
def route_delete_chart(model_name, chart_id): """ Deletes chosen chart from the database. """ Model = str_to_object(model_name) chart = Model.query.get_or_404(chart_id) db.session.delete(chart) db.session.commit() flash( f'Chart ({chart.id}) has been succesfully removed from the database.', 'success') chart = get_recently_added_record(db, model_name) if chart: return redirect( url_for('route_show_data', model_name=model_name, chart_id=chart.id)) return redirect(url_for('route_show_data', model_name=model_name))
def route_change_coefs(model_name, chart_id=-1): Model = str_to_object(model_name) model_object = Model.query.get(chart_id) common_models = ['Sinus', 'Cosinus', 'Exponential', 'SquareRoot'] if model_name in common_models: form = DataForm() elif model_name == 'SquareFunc': form = SquareFuncForm() elif model_name == 'CustomEquation': form = CustomEquationForm() coefs = Model.get_coefs(chart_id) if form.validate_on_submit(): if model_name in common_models: model_object.a = form.coef_a.data model_object.b = form.coef_b.data model_object.c = form.coef_c.data model_object.d = form.coef_d.data elif model_name == 'SquareFunc': model_object.a = form.coef_a.data model_object.p = form.coef_p.data model_object.q = form.coef_q.data db.session.commit() flash(f'Changed coefficients for {chart_id} {model_name}!', 'success') return redirect( url_for('route_show_data', model_name=model_name, chart_id=chart_id)) return render_template("change_data.html", model_name=model_name, chart_id=chart_id, form=form, coefs=coefs)
def route_show_data(model_name, chart_id=-1): """ Renders end point on which data table with all points is shown for specific model, five charts for each library: Matplotlib, Seaborn, Bokeh, Plotly and Pygal. """ Model = str_to_object(model_name) records = Model.query.all() kw_options = dict() if chart_id != -1 and model_name != "FileDataPoint": current_chart = Model.query.get(chart_id) matplotlib_options_id = current_chart.id_matplotlib_options kw_options['matplotlib_options'] = json.dumps( MatplotlibPlotOptions.get_options(matplotlib_options_id)) seaborn_options_id = current_chart.id_seaborn_options kw_options['seaborn_options'] = json.dumps( SeabornPlotOptions.get_options(seaborn_options_id)) bokeh_options_id = current_chart.id_bokeh_options kw_options['bokeh_options'] = BokehPlotOptions.get_options( bokeh_options_id) plotly_options_id = current_chart.id_plotly_options kw_options['plotly_options'] = PlotlyPlotOptions.get_options( plotly_options_id) pygal_options_id = current_chart.id_pygal_options kw_options['pygal_options'] = PygalPlotOptions.get_options( pygal_options_id) getable_models = [ 'Sinus', 'Cosinus', 'SquareRoot', 'Exponential', 'SquareFunc' ] kwargs = dict() if model_name in getable_models: kwargs['coefs'] = Model.get_coefs(chart_id) elif chart_id != -1 and model_name == 'CustomEquation': current_chart = Model.query.get(chart_id) kwargs['custom_equation'] = string_to_mathjax(current_chart) bokeh_chart = make_chart_bokeh(model_name, chart_id, kw_options.get('bokeh_options', dict())) script_bokeh, div_bokeh = bokeh_components(bokeh_chart) kwargs["script_bokeh"] = script_bokeh kwargs["div_bokeh"] = div_bokeh kwargs[ "script_plotly"] = '<script src="https://cdn.plot.ly/plotly-latest.min.js"></script> ' kwargs["div_plotly"] = make_chart_plotly( model_name, chart_id, kw_options.get('plotly_options', dict())) pygal_chart = make_chart_pygal(model_name, chart_id, kw_options.get('pygal_options', dict())) kwargs["src_pygal"] = pygal_chart.render_data_uri() kwforms = dict() kwforms['matplotlib_form'] = MatplotlibOptionsForm() kwforms['seaborn_form'] = SeabornOptionsForm() kwforms['bokeh_form'] = BokehOptionsForm() kwforms['plotly_form'] = PlotlyOptionsForm() kwforms['pygal_form'] = PygalOptionsForm() return render_template('show_data.html', model_name=model_name, chart_id=chart_id, records=records, **kwargs, **kwforms, **kw_options)