jupyter-widgets / ipywidgets

Interactive Widgets for the Jupyter Notebook
https://ipywidgets.readthedocs.io
BSD 3-Clause "New" or "Revised" License
3.17k stars 950 forks source link

Better error messaging for "model not found" #3891

Open paddymul opened 8 months ago

paddymul commented 8 months ago

For new users of my custom widget, the experience of seeing a JS based error saying "widget: model not found" is frustrating and confusing.

I want to display a message like

"You seem to have installed Buckaroo after you started your jupyterlab server,  please restart your jupyter lab server for buckaroo to function properly"

From my understanding the python package installs properly, but the js is registered as a lab extenstion. By checking the server start time, and the package MTime, we can decide if a user is in this state and message appropriately when the package is imported or the widget used.

I don't know how to find the controlling jupyterlab server, and have filed a bug asking for help with that.

This code will be useful for all custom widgets.

https://github.com/jupyterlab/jupyterlab/issues/15918

paddymul commented 8 months ago

After more investigation I was able to make this work.

    parent_process = psutil.Process().parent()
    server_start_time = dtdt.fromtimestamp(parent_process.create_time())

    buckaroo_mtime = dtdt.fromtimestamp(os.path.getmtime(__file__))

    if buckaroo_mtime > server_start_time:
        print("""It looks like you installed Buckaroo after you started this notebook server. If you see a message like "Failed to load model class 'DCEFWidgetModel' from module 'buckaroo'", restart the jupyter server and try again.  If you have furter errors, please file a bug report at https://github.com/paddymul/buckaroo""")
        print("buckaroo_mtime", buckaroo_mtime, "server_start_time", server_start_time)