adafruit / circuitpython_jupyter_kernel

Jupyter kernel to interact with CircuitPython board over USB
https://circuitpython-kernel.readthedocs.io/en/latest/
Other
37 stars 27 forks source link

Making use of Python magics #31

Open psychemedia opened 3 years ago

psychemedia commented 3 years ago

Hi

We're currently evaluating the potential of using something like Circuit Playground Express as a home experiment kit and I've been looking at what support there may be around for using Jupyter notebooks to work with them (my early notes here: https://ouseful-testing.github.io/circuit-playground-express/

The magics look like they could be interesting, but they still seem to be early days?

(The following is intended to be constructive rather than overly critical.)

One way I imagined using magics was to eg:

BUT:

ladyada commented 3 years ago

you're may be better off not running the kernel on the cpx but on the computer. program the cpx with arduino code that outputs the sensor data on the serial port, then read it with pyserial into cpython jupytr

psychemedia commented 3 years ago

@ladyada I am a complete novice to Circuit Python and devices such as Circuit Playground Express. Our evaluation is focussing on the Mu editor at the moment (we need a really simple user interface; we need an easy to install setup for independent use by hundreds (at least) of novices working independently, at a distance (i.e. working on their own at home with only occasion forum support), on a wide range of computers.

The notebook experience itself is at the upper limit of complexity for our use case and the study time we can allocate to related activities.

However, the notebook does allow us to structure activities as part of a linear narrative, with interactive inline code execution, which is a style we have used for other programming related courses.

ladyada commented 3 years ago

notebooks and juptyr are very hard to set up. docker is even harder. we really do recommend sticking to mu. or go with makecode. https://learn.adafruit.com/search?q=makecode

psychemedia commented 3 years ago

We already use notebooks in several introductory courses, with more to follow, which is why we keep looking to see what other things we might be able to do with them.

I get the feeling that circuitpython_jupyter_kernel is currently quite a way away from where we'd need it to be to run a full range of activities, though it would work as a simple guide for downloading prepared or easily edited programs to run on the device using LEDs as output indicators.

If I get time to look at the code to see if I could contribute eg to the magics, I will, but I get the sense it might also take some work on the CircuitPython side to make it play nicely with the notebooks.

ladyada commented 3 years ago

any PR's are welcome!

jposada202020 commented 3 years ago

@psychemedia please take a look a this https://github.com/adafruit/circuitpython_jupyter_kernel/pull/30. Magics we used here in a different context , but could be what you are looking for.

psychemedia commented 3 years ago

@jposada202020 The "functionality to allow the user to interact with the microcontroller by collecting and parsing data in real time and creating plots and figures from the data during development" is the sort of thing I'm after but I don't see how the example does that?

Eg, i'm looking for something with the feel of the following (not that the following is probably broken in lots of ways and won't work but its the style of interaction I'm after)

In a Circuit Python kernel code cell, collect some data:

import time

import analogio
import board

vals=[]
light_=analogio.AnalogIn(board.LIGHT)
for i in range(20):
    vals.append((light_.value,))
    time.sleep(0.1)

print(values)
# Ideally this would just be:
values
#but there is no cell output from that?

Then in a magic cell in full py interpreter, some way to pass in or access that var:

%%python myvar=$values
 import pandas as pd

df = pd.DataFrame(myvar)
df.plot()
# plot appears as output
jposada202020 commented 3 years ago

@psychemedia I see, I tried to the same as you, because in a raspberry pi you could do it, but that use native python I explored that possibility, the problem I found was the way we made aware that library that code execution is finished. I tried different things but with no success, To be fair I am not very familiar with serial communication, so maybe there is something that I am missing. But I do not know if the computer is required but Adafruit has other products better that could be just that, interact with the microcontroler. There are tools also inside Circuitpython libraries for that purpose, not as easy as a panda plot, but easy enough to just give the points to plot.

psychemedia commented 3 years ago

@jposada202020 Hmmm.. Are you saying I can run a "normal" Jupyter notebook on RPi and somehow run that code against a connected Circuit Playground Express? I could see it would be possible if you were addressing the RPi I/O pins directly, but donlt see how that would work with a connected device?

jposada202020 commented 3 years ago

Not tested in RPI, so not sure, but I guess I use Ubuntu and it works. Supported boards are Circuit Playground Express, Feather RP2040, Raspberry Pi Pico, Teensy. We could add others taking into account that they do not use the screen, as REPL use the serial. Take at look at the examples.. They are the basic ones, but I have tested this one also neopixel_simpletest.py in the feather RP2040. I have tested also taking temperature from a I2C sensor, etc, but Used more like a repl, not with longer code.

jposada202020 commented 3 years ago

question here.. so if you are using a RPi why dont you use Blinka? is because of the Jupyter Notebooks? and their control widgets? and the animations or the real time. What I try to understan here is what is a must, and what is a good to have..

brentru commented 3 years ago

agreed - if you're using a Raspberry Pi, you may be better off running blinka alongside CPython packages such as pandas.