Josverl / micropython-magic

MicroPython integrated into Jupyter notebooks
MIT License
22 stars 2 forks source link

Returning data from the device #43

Closed rdlaner closed 1 month ago

rdlaner commented 1 month ago

Hi, I'm attempting to explore ways in which I can use pytest to write tests for my micropython code. Ideally, I'd like to use ipytest to write tests in a Jupyter notebook that also leverages micropython-magic to run the test code on device. However, I'm not totally sure if this is possible with the given micropython-magic feature set.

Is there a way to run code on device (ie. %%mpy or %mpy --eval) that can return data back to the notebook to be used by non-micropython code? In other words, does micropython-magic support any method for serializing/deserializing any return data from the device?

Hope that makes sense, thanks!

rdlaner commented 1 month ago

ah, I see an example illustrating this here: https://github.com/Josverl/micropython-magic/blob/083fcc9898caab294fb8895d8d5bb6508cbed861/samples/plot_cpu_temp_rp2.ipynb#L139

Looks like the strategy is to define a %%mpy cell that defines all the necessary variables and likely helper functions on the device and then subsequent cells can run expressions or invoke functions with %mpy --eval. I think I can get this to work with ipytest pretty well!

Josverl commented 1 month ago

You may also want to look at the tests in this project, and specially the ones using testbook.

I found that quite usefull where essentially I just write a notebook and pytest just runs that, and PASSES when there is not an error in any cell.

You can do multi device tests as well.