Approach for re-executing notebooks on a schedule?

I am looking to setup an nbdev2 repository where the notebooks are re-executed on a regular basis. A few use cases for this are:

Triggering re-execution can be configured by the deploy workflow:

workflows/deploy.yaml

on:
  schedule:
    # Runs "at 0 minutes past the hour" (see https://crontab.guru)
    - cron: '0 * * * *'

However, as discussed in the issue evaluating ipywidget code block in nbdev_preview does not result in output · Issue #1042 · fastai/nbdev · GitHub, widget outputs are not regenerated unless execute: true is set, and that setting causes “confusion between nbdev and Quarto’s processors”.

Does anyone have alternative approaches for how notebook content, including widget outputs, could be regenerated on a regular schedule?

2 Likes

Have you tried the exec_doc directive in the cells you want executed? nbdev - Directives

I really like this use-case btw! :slight_smile:

I tried the #|exec_doc directive, but it doesn’t seem to work.
Test repo: GitHub - tylere/test-evaluate-code-4: Test out evaluating code in nbdev notebooks.

Here is the source notebook, saved with outputs (including a widget):

Here is what nbdev_preview produces in the _proc folder:

Below is the rendered page, which is:

  1. Missing the widget output, and
  2. Does not update the datetime.now() value when the deploy workflow is rerun.

Thanks for the clear repro (as always) and sorry for the struggles on this one! :slight_smile:

  1. Taking a look now…
  2. It looks like datetime.now() is updating in your screenshots, unless I’m missing something. I also forked your repo and it’s updated to 2022-09-23 05:18:14.238525 on mine. (Note that this is in the timezone of the computer used to run the GH Action, so you’d have to ensure your code does any timezone conversion for you.)

Hi Tyler as an aside, if you want some inspiration from the past, I have an old fastpages project that does something similar to what you were thinking GitHub - github/covid19-dashboard: A site that displays up to date COVID-19 stats, powered by fastpages. that might be helpful for some ideas. It used papermill to refresh notebooks, however I think the approach you are trying is better.

@seem I think this problem might have to do with the cache? seems like we aren’t exposing the force flag to the user but maybe we should

@tylere one hack would be to touch settings.ini to invalidate the cache

@seem yes, datetime.now() seems to be updating correctly, I think it was a problem with caching.

@hamelsmu I think the caching confusion that I encountered was related my web browser was caching the GitHub page (because I was comparing the results of the “Deploy” workflow), rather than nbdev caching. If I run into this again I will probably try adding some logging statements in the workflow yaml to verify that new content is being generated.

So now we’re left with trying to get the widget to execute and render output, for which I see there is a PR: `exec_doc` supports re-rendering widgets by seeM · Pull Request #1113 · fastai/nbdev · GitHub

@hamelsmu thanks for the Covid dashboard example. In particular, I think the examples of passing keys/tokens using GitHub secrets could be useful when re-executing nbdev pages. Could you confirm that this remains a best practice for working with sensitive data with nbdev? (The Quarto docs do suggest using GitHub Actions encrypted secrets.)

1 Like

I’m going to merge this PR now so you can test it out

Yes this is what we do, and it’s been working well – but if you come across any other good ways, do let us know.

PR has been merged. It looks to be working on my fork of your repo: test-evaluate-code-4 - core

Note: I needed to update the deploy workflow to use the nbdev latest master in use nbdev latest for deploy · seeM/test-evaluate-code-4@c8f1b5e · GitHub

Confirming that it appears to work for me. Thank you!

1 Like