Holding message with Inputs.file, required=true

I like the way that if using Inputs.file allows you to set required:true so that downstream cells remain blank until resolved.

However, I’d like the ability to provide a ‘holding message’ in some of those downstream cells (e.g. ‘Load a file to see the result here’). Is there a recommended pattern for doing this? Not sure if it is possible because in effect I want those cells to be able to say ‘resolve enough of this cell to display this holding message, but pause the rest until file is loaded’.

1 Like

If there are no cells downstream of (i.e. depending on) the cell in which you want to display the message then you can simply check for null and return an HTML text in that case.

If there are, then you’ll probably want to employ a viewof cell. This thread holds some suggestions: Showing cell evaluation progress - #2 by mootari

Thanks for the pointers. I’m not sure I can do a null check if required:true because the cell that would do that check isn’t evaluated until the file is loaded. But leaving the default required:false allows for null checking.

I’ve come up with the following helper function which works also for cells that might depend on more than one file load (the first parameter can be a single file loading cell or a list of such cells). The action parameter is a function that is called with a dependency on the input file(s) and msg the message to display while file loading is pending:

function waitForInput(inFiles, action, msg = "") {
  const hasNoNulls = (input) =>
    Array.isArray(input)
      ? input.every((item) => item !== null)
      : input !== null;

  return hasNoNulls(inFiles)
    ? action(...[inFiles].flat())
    : msg?.length > 0
    ? md`_${msg}_`
    : md``;
}

For example:

viewof myFile = Inputs.file({ label: "Load file" })
waitForInput(
  myFile,
  (file) => file.text().then((d) => d),
  "Choose a file above."
)

Perhaps there is a simpler approach than this, but it seems to work and avoids having difficult-to-interpret blank cells with an ambiguous grey 'yet to be evaluated` bar on the left. Suggestions for simplifications/improvements welcome.

1 Like