Is there a "file -> open" possibility inside of a sketch?

I am using the sketch to read various CSV files that have a date in them.
Then I interpret the data and make graphs out of it.

I use
table = loadTable(“Infinity_Report_3_31_2021.csv”, “header”);
Then I populate arrays with my data.

I would like to browse inside the sketch to different files I want to open.
Like you go in “word” and browse for files to open.

Long term I would want to select a date range and open multiple files and see trends…

Is this possible?

Also I am trying to open a file online using the hugely popular “OneDrive” from microsoft.
In sharing, One drive gave me a URL
https://onedrive.live.com/Edit.aspx?resid=B15E9906640B6318!83888&wd=cpe&authkey=!AK6-OP68Ew_FfGE

the link will go to my file OK. So I tried using it in Processing.

 table = loadTable("https://onedrive.live.com/Edit.aspx?resid=B15E9906640B6318!83888&wd=cpe&authkey=!AK6-OP68Ew_FfGE", "csv");

but it did not work out, I get a runtime exception

Please help,
Would be great to use OneDrive

Thanks

Yes there is a possibility with the command

selectInput("Window_name", "fileSelected");

When a file gets selected the method

void fileSelected(File selection){
}

You can call the path with

selection.getPath()

that’s of course the right approach when you have files on your hard drive

but onedrive is different I guess - it’s probably a kind of API where you need to figure out whether there is a way to get a file list and then browse the file list.

Did you check the documentation?

1 Like

Thank you everyone. Lets focus on loading the local file first.
I have a hard time making loadTable accept the file name I select.
I browsed the whole internet, no examples… Please help

Here is what I got.

String Mach_Name []= {"00", "31", "200", "700", "POD1", "POD2", "BHX1", "BHX2", "EDG1", "EDG2", "10", "M_11"};// 11 vals
int  [] Cycles = new int [100];
float [] Pr = new float [100];  // percentage of Uptime
Table table;
int incr=1;
File filename;
void setup() {
  size(1900, 1000, JAVA2D);
selectInput("Select a file to process:", "fileSelected", filename);

  //table = loadTable("https://onedrive.live.com/Edit.aspx?resid=B15E9906640B6318!83888&wd=cpe&authkey=!AK6-OP68Ew_FfGE", "csv");


 
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    println("User selected " + selection.getAbsolutePath());
    println ("Filename: "+ filename);
  }
  
/* 
    table = loadTable(filename, "header");
    println(("Row COunt is " + table.getRowCount()));
   for (TableRow row : table.rows()) 
  {
    Mach_Name [incr] = row.getString("MACH ID");
    Pr[incr] = row.getInt("UPTIME %");
    Cycles [incr] = row.getInt("CYCLES TODAY");

    incr++;
    // println(name + " (" + species + ") has an ID of " + id);
  }
  for (int i=1; i<=10; i++)
  {
    println (i + " Mach_Name; "+Mach_Name[i]+ "  %="+ Pr[i]  );
  }
  */
}


void mouseClicked() 
{

  selectInput("Window_name", "fileSelected");
  println(filename);
}


Also,
The selectInput( function works only in setup?
I tried it under mouseClicked and nothing executes.

Thats a burmmer. The user should be able to load various files without restarting the sketch.

Thanks

1 Like

heres a class that can handle file selection or folder selection

class fileInput{
  String value;
  boolean click = false;
  
  fileInput(){
    
  };
  
 String listen(){
   String s = null;
  if(click())selectInput("Select a file to process:", "fileSelected");
  s = value;
  return s;
 }

  boolean click(){
      boolean k = false;
      if (pos()&&mousePressed&&!click){
        click = true;
        k = false;
      }else if(click&&!mousePressed){
        k = true;
        click = false;
      }
      
      return k;
  };
    
  boolean pos(){
    return mouseX>0&&mouseX<width&&mouseY>0&&mouseY<height;
  };
  
};

void fileSelected(File selection) {
  String s = null;
    if (selection != null) {
      
      file.value = selection.getAbsolutePath();
      //println("User selected " + file.value);
    }
    //return s;
  };
  
class folderInput extends fileInput{
   
  folderInput(){
    
  };
 // @Override
 // public void listen(){
 // if(click())selectInput("Select a file to process:", "folderSelected");
 //}
  
};
  
//void folderSelected(File selection) {
//  if (selection == null) {
//    println("Window was closed or the user hit cancel.");
//  } else {
//    println("User selected " + selection.getAbsolutePath());
//  }
//};

String folderSelected(File selection) {
  String s = null;
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    println("User selected " + selection.getAbsolutePath());
    s = selection.getAbsolutePath();
  }
  return s;
};
1 Like

you beat me to it

here is an example with buttons

// Demo for selectInput

//states 
final int stateWaitForFile   = 0;  // consts  
final int stateDone          = 1;
final int stateBreak         = 2;  
final int stateWaitForButton = 3;                
int state = stateWaitForButton;  // current

String fileGlobal = "";  

Table tableTest ; 

// ----------------------------------------------------------------------------------------

void setup() {
  size (1100, 700); 
  background(111);
}

void draw() {
  // 
  // switch 
  switch (state) {

  case stateWaitForFile:
    // waiting until file has been selected
    background(111);
    text("Please choose a file. Hit Cancel to abort.", 
      33, 33);    

    // THE WAIT IS OVER   
    if (!fileGlobal.equals("")) {
      state = stateDone;
      // here you would load the table
      tableTest = loadTable(fileGlobal); 
      //
    } // if 
    break;

  case stateDone:
    // here you would have the table and display it 
    background(111);
    text("File is there now: "
      +fileGlobal, 100, 100); 
    showButtons();
    text(tableTest.getColumnCount(), 30, height-177);  //
    break;

  case stateWaitForButton:
    background(111);
    text("Please selecr a button.", 100, 100); 
    showButtons(); 
    break; 

  case stateBreak:
    background(111);
    text("Window was closed or the user hit cancel.", 33, 33); 
    showButtons(); 
    break; 

  default:
    // error
    println ("error 91"); 
    exit();
    break;
    //
  }  // switch

  //debug
  fill(255);
  text(state, 30, height-30);
}  // func 

// -----------------------------------------------------------------
// file handling 

void fileSelected(File selection) {
  //
  // the 'callback' function.
  //
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
    state = stateBreak;
  } else {
    println("User selected " + selection.getAbsolutePath());
    fileGlobal = selection.getAbsolutePath();
  } // else
}//func 

// ----------------------------------------------------------------
// Input 

void mousePressed() {
  // check Buttons
  if (state==stateDone||state==stateBreak||state==stateWaitForButton) {
    if (dist(mouseX, mouseY, 133, 133) < 66) {
      // reset / restart
      fileGlobal=""; 
      selectInput("Select a file (Cancel to abort):", 
        "fileSelected");
      state=stateWaitForFile;
    }//if
    else if (dist(mouseX, mouseY, 233, 133) < 66) {
      exit();
    }//else if
  } //if
  //
} //func 

//-------------------------------------------------------------------
//Tools

void showButtons() {
  // set mode for text and rect 
  textAlign(CENTER, CENTER);
  rectMode(CENTER);

  // rects 
  noFill();  
  rect(133-5, 133+3, 84, 23);
  rect(233-1, 133+3, 84, 23);

  // text 
  fill(255); 
  text ( "Choose file", 133, 133);
  text ( "Quit", 233, 133);

  // reset 
  textAlign(LEFT);
  rectMode(CORNER); // The default mode is rectMode(CORNER)
}//func 
//

1 Like

Paul,
Thanks for the example. I ran it but I get “file cannot be resolved as a variable” and this line is highlighted

  file.value = selection.getAbsolutePath();

Chrisir,
Hallo,

Thanks a lot again, the code works well. I will adapt it to my needs. I will work on loading from OneDrive later.

Mitch

1 Like