0

I am trying to implement a simple web app using a web assembly created with wasm-bindgen. I am entirely new to Rust.

The web page would have a file input element to select a CSV file. The web-assembly code would load this CSV and use polars to group, sort, filter, etc. the data and then render the result back to the client(directly on the web page or by sharing data with the JS code)

Here is the code of the web assembly:

mod utils;

use wasm_bindgen::JsCast;
use wasm_bindgen::prelude::*;
use polars::prelude::*;
use std::io::Cursor;
use js_sys::JsString;
use polars_lazy::prelude::col;

#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[wasm_bindgen]
pub fn load_file(file_input : web_sys::HtmlInputElement) {

    let filelist = file_input.files().expect("Failed to get filelist from File Input!");
    if filelist.length() < 1 {        
        return;
    }
    if filelist.get(0) == None {        
        return;
    }
    let file = filelist.get(0).expect("Failed to get File from filelist!");
    let file_reader : web_sys::FileReader = match web_sys::FileReader::new() {
        Ok(f) => f,
        Err(e) => {
            web_sys::FileReader::new().expect("")
        }
    };   
   
    let fr_c = file_reader.clone();
    let onloadend_cb = Closure::wrap(Box::new(move |_e: web_sys::ProgressEvent| {
        let data = fr_c.result().unwrap();
        let file_string: JsString = data.dyn_into::<JsString>().unwrap();
        let file_vec: Vec<u8> = file_string.iter().map(|x| x as u8).collect();
        let cursor = Cursor::new(file_vec);
        let df = CsvReader::new(cursor)
        .has_header(true)
        .finish().unwrap()
        .groupby(col("name"));
    }) as Box<dyn Fn(web_sys::ProgressEvent)>);

    file_reader.set_onloadend(Some(onloadend_cb.as_ref().unchecked_ref()));
    file_reader.read_as_array_buffer(&file).expect("blob not readable");
    onloadend_cb.forget();
}


The compiler shows the error on line .groupby(col("name")):

error[E0277]: `Expr` is not an iterator
   --> src\lib.rs:41:18
    |
41  |         .groupby(col("name"));
    |          ------- ^^^^^^^^^^^ expected an implementor of trait `IntoIterator`
    |          |
    |          required by a bound introduced by this call
    |

I can't understand what's the reason for the error. It suggests adding "&" before the "col", but it does not help.

Does anyone know how to fix the error?

sovo2014
  • 487
  • 7
  • 17
  • Since you stated being new to rust I would strongly recommend you check the type signature of the return value of the unwrap() call just before that .groupBy() call, it likely is not implementing the trait mentioned in the error, or you are missing a use statement that does contain the implementation. My preferred setup is rust-analyzer with vscode (disclaimer, I am a backer of rust-analyzer), as it might give you an auto-fix option for this – gxtaillon Jan 30 '22 at 17:57

1 Answers1

0

Looking at the docs for the DataFrame::groupby() method and the provided example, we can see that the column can be specified through an array of strings:

.groupby(["name"])
Brian Bowman
  • 892
  • 6
  • 9