Created
October 22, 2019 14:22
-
-
Save JohnDeJesus22/c03ac1d99a8f6dcba1037a5d8ace1825 to your computer and use it in GitHub Desktop.
Backend function to generate create features web-page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# kmeans grouper dashboard | |
@bp.route('/kmeansdashboard/<email>', methods=['GET', 'POST']) | |
@login_required | |
def kmeansdashboard(email): | |
# get user data from database | |
user = User.query.filter_by(email=email).first_or_404() | |
data = user.upload | |
# get columns | |
columns = [*data.columns] | |
# Take in kmeans data input | |
form = KmeansInputForm() | |
form.column1.choices = [(column, column) for column in columns] | |
form.column2.choices = form.column1.choices | |
if form.validate_on_submit(): | |
# initialize kmeans and prep data | |
kmeans = KmeansGrouper(clusters=form.kmeans_group_nums.data) | |
kmeans_prep_data = data[[form.column1.data, form.column2.data]] | |
# Get groups_labels attached to data, and numpy version of data and prep for display | |
kmeans_data, groups, data_np = kmeans.kmeans_categories(kmeans_prep_data) | |
# get scatter plot for displaying group visualization | |
plot = kmeans.scatter_plot(data_np, groups) | |
# Attach additional columns | |
# condition made since default output on add_columns is '', not None | |
if form.additional_columns.data != '': | |
# input additional columns and filter data by the entered and additional columns | |
# > 1 additional column | |
if ',' in form.additional_columns.data: | |
additional_columns = form.additional_columns.data.split(', ') | |
kmeans_data = pd.concat([data[additional_columns], kmeans_data], axis=1) | |
else: | |
# one additional column | |
kmeans_data = pd.concat([data[[form.additional_columns.data]], kmeans_data], axis=1) | |
# Create category tables | |
dataframe_list = kmeans.dataframe_constructors(kmeans_data) | |
dataframe_titles = [f'Category {i}' for i in sorted(kmeans_data.Category.unique())] | |
flash('Plot and Tables displayed successfully!') | |
return render_template('grouping/kmeansdashboard.html', user=user, email=user.email, form=form, plot=plot, | |
columns=columns, tables=kmeans.dfs_to_html(dataframe_list), | |
dataframe_titles=dataframe_titles) | |
return render_template('grouping/kmeansdashboard.html', columns=columns, user=user, form=form) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment