@home.route('/load_data/<filename>', methods=['GET', 'POST'])
@login_required
def load_data(filename=None):
    log_info(f'Entering load_data: request.method={request.method}')
    filename = None
    form = LoadDataForm()
    document = current_user.get_filename()
    uploads_folder = user_data.get_document_uploads_folder_name()
    if ((request.method == 'POST') and (BTN_CANCEL in request.form)):
        return redirect(get_back_url())
    if ((request.method == 'POST') and form.validate_on_submit()):
        if ('file' not in request.files):
            flash('No file part', 'error')
            return redirect(request.url)
        eml_node = load_eml(filename=document)
        dataset_node = eml_node.find_child(names.DATASET)
        if (not dataset_node):
            dataset_node = new_child_node(names.DATASET, eml_node)
        file = request.files['file']
        if file:
            filename = file.filename
        if filename:
            if ((filename is None) or (filename == '')):
                flash('No selected file', 'error')
            elif allowed_data_file(filename):
                Path(uploads_folder).mkdir(parents=True, exist_ok=True)
                filepath = os.path.join(uploads_folder, filename)
                if file:
                    file.save(filepath)
                num_header_rows = 1
                delimiter = form.delimiter.data
                quote_char = form.quote.data
                try:
                    (dt_node, new_column_vartypes, new_column_names, new_column_categorical_codes, *_) = load_data_table(uploads_folder, filename, num_header_rows, delimiter, quote_char)
                except UnicodeDecodeError as err:
                    errors = display_decode_error_lines(filepath)
                    return render_template('encoding_error.html', filename=filename, errors=errors)
                except DataTableError as err:
                    flash(f'Data table has an error: {err.message}', 'error')
                    return redirect(request.url)
                flash(f'Loaded {filename}')
                dt_node.parent = dataset_node
                dataset_node.add_child(dt_node)
                user_data.add_data_table_upload_filename(filename)
                if new_column_vartypes:
                    user_data.add_uploaded_table_properties(filename, new_column_vartypes, new_column_names, new_column_categorical_codes)
                delete_data_files(uploads_folder)
                save_both_formats(filename=document, eml_node=eml_node)
                return redirect(url_for(PAGE_DATA_TABLE, filename=document, dt_node_id=dt_node.id, delimiter=delimiter, quote_char=quote_char))
            else:
                flash(f'{filename} is not a supported data file type')
                return redirect(request.url)
    return render_template('load_data.html', title='Load Data', form=form)
@home.route('/load_data/<filename>', methods=['GET', 'POST'])
@login_required
def load_data(filename=None):
    log_info(f'Entering load_data: request.method={request.method}')
    filename = None
    form = LoadDataForm()
    document = current_user.get_filename()
    uploads_folder = user_data.get_document_uploads_folder_name()
    if ((request.method == 'POST') and (BTN_CANCEL in request.form)):
        return redirect(get_back_url())
    if ((request.method == 'POST') and form.validate_on_submit()):
        if ('file' not in request.files):
            flash('No file part', 'error')
            return redirect(request.url)
        eml_node = load_eml(filename=document)
        dataset_node = eml_node.find_child(names.DATASET)
        if (not dataset_node):
            dataset_node = new_child_node(names.DATASET, eml_node)
        file = request.files['file']
        if file:
            filename = file.filename
        if filename:
            if ((filename is None) or (filename == )):
                flash('No selected file', 'error')
            elif allowed_data_file(filename):
                Path(uploads_folder).mkdir(parents=True, exist_ok=True)
                filepath = os.path.join(uploads_folder, filename)
                if file:
                    file.save(filepath)
                num_header_rows = 1
                delimiter = form.delimiter.data
                quote_char = form.quote.data
                try:
                    (dt_node, new_column_vartypes, new_column_names, new_column_categorical_codes, *_) = load_data_table(uploads_folder, filename, num_header_rows, delimiter, quote_char)
                except UnicodeDecodeError as err:
                    errors = display_decode_error_lines(filepath)
                    return render_template('encoding_error.html', filename=filename, errors=errors)
                except DataTableError as err:
                    flash(f'Data table has an error: {err.message}', 'error')
                    return redirect(request.url)
                flash(f'Loaded {filename}')
                dt_node.parent = dataset_node
                dataset_node.add_child(dt_node)
                user_data.add_data_table_upload_filename(filename)
                if new_column_vartypes:
                    user_data.add_uploaded_table_properties(filename, new_column_vartypes, new_column_names, new_column_categorical_codes)
                delete_data_files(uploads_folder)
                save_both_formats(filename=document, eml_node=eml_node)
                return redirect(url_for(PAGE_DATA_TABLE, filename=document, dt_node_id=dt_node.id, delimiter=delimiter, quote_char=quote_char))
            else:
                flash(f'{filename} is not a supported data file type')
                return redirect(request.url)
    return render_template('load_data.html', title='Load Data', form=form)