Beispiel #1
0
def import_csv():
    form = forms.DatasetCSVImportForm()
    if form.validate_on_submit():
        # Decode the rows as UTF-8.
        # The utf-8-sig codec removes a UTF-8 BOM if it exists at the start of a file.
        # In that case, col1 in the first row could start with 0xfeff, so remove it.
        # For all other items it will decode as regular UTF-8
        file = TextIOWrapper(request.files[form.file.name], encoding='utf-8-sig')
        description, classes = _parse_dataset_csv(file)
        dataset_dict = {
            "name": form.name.data,
            "description": description if description else form.description.data,
            "classes": classes,
            "public": form.public.data,
        }

        try:
            dataset_id = db.dataset.create_from_dict(dataset_dict, current_user.id)
        except dataset_validator.ValidationException as e:
            raise BadRequest(e.error)
        flash.info("Dataset has been imported successfully.")
        return redirect(url_for(".view", dataset_id=dataset_id))

    else:
        return render_template("datasets/import.html", form=form)
Beispiel #2
0
def import_csv():
    form = forms.DatasetCSVImportForm()
    if form.validate_on_submit():
        dataset_dict = {
            "name": form.name.data,
            "description": form.description.data,
            "classes": _parse_dataset_csv(request.files[form.file.name]),
            "public": True,
        }
        try:
            dataset_id = db.dataset.create_from_dict(dataset_dict, current_user.id)
        except dataset_validator.ValidationException as e:
            raise BadRequest(str(e))
        flash.info("Dataset has been imported successfully.")
        return redirect(url_for(".view", id=dataset_id))

    else:
        return render_template("datasets/import.html", form=form)