News for hackers. Stuff that matters.
Re:JupyterLab is ready for users by danielvf on Tuesday February 20, 2018 @10:12AM (#16420887)
I'll be teaching a group of nine and ten years olds to code. I'm planning on using Jupyter.

What I'd really like to do is make a multiplayer naval game, with each player controlling their ship from their own notebook. Players would start out by running commands like fire(range=400, bearing=120) right from a cell, but would later be able automate their ship - for example, pick the nearest enemy, get the range, and plug that into firing automatically at it.

My server would be projecting a big map of the world up on the wall.

However, to do this nicely, I need the ability to make a cell (or a function defined in a cell) run every X milliseconds. I know I can do this for one cell, with a loop and sleep function, but I'd really rather have multiple cells/functions "running", so we can break the code into smaller chunks, and to let them build their own ship UIs.

Any advice on how to do this in Juptyer with Python? In an ideal world, I'd just "tag" a cell somehow so that it ran periodically.

Re:JupyterLab is ready for users by in9 on Tuesday February 20, 2018 @12:32PM (#16422111)
To be totally honest, even though I love Jupyter notebooks, I wouldn't teach someone to code using it. See, as someone who's not, originally a programmer, I believe that teaching someone to code is intertwined with teaching (gradually) workflows and environments.

Now, Jupyter is great (I'm a statistician btw) precisely because you have a presentable literate programming tool. That is extremely valuable for data analysis and model building.

Now, If you are teaching programming in it self, I think the terminal + text editor is important since that is a step towards how things are done irl. But if you think that the terminal is too much for your audience, just go for IDLE, which has many advantages for python development + teaching, and is already a step towards text editor + terminal or IDE level development.

Re:JupyterLab is ready for users by dragonwriter on Tuesday February 20, 2018 @03:42PM (#16424000)
> Now, If you are teaching programming in it self, I think the terminal + text editor is important since that is a step towards how things are done irl.

Jupyter (and similar) Notebooks are a way things are done "in real life"; they aren't just for entertainment purposes; and JupyterLab blends that into an IDE and the IDE with notebooks approach is also a way things have been done "in real life"; both with third-party tools incorporating Jupyter (and earlier IPython) notebooks, and with other IDE's incorporating notebook-style interaction.

Which isn't to say that there aren't arguments for teaching program outside of the notebook environment rather than within it (though the notebook is kind of a super REPL, and REPLs seem to me to be great tools for teaching), but that not using notebooks is better because it is how things are done "in real life" is just taking a highly selective view of real life.

Re:JupyterLab is ready for users by jasongrout on Tuesday February 20, 2018 @01:24PM (#16422576)
I'm teaching someone to code right now on the Raspberry Pi, and JupyterLab is working far better for us than IDLE or a separate text file and terminal. We use the workflow described in http://jupyterlab.readthedocs.io/en/stable/user/documents_ke.... We open up our python file and a console side-by-side, and pressing shift-enter in the file sends a line or selection to a console. We can easily investigate it in the console outside of the file too.

You can just "pip install jupyterlab" on a Raspberry Pi and it works great!

Re:JupyterLab is ready for users by gldnspud on Tuesday February 20, 2018 @02:19PM (#16423194)
These criticisms could be valid for Jupyter Notebook, but remember that JupyterLab is what's under discussion in this post. JuptyerLab is much more of an IDE-style than Notebook, and it includes very capable terminal and text editor components.

Although I'm new to Jupyter Lab, I just tried it and it does feel like it could be a very approachable IDE for educational use.

Re:JupyterLab is ready for users by psykotic on Tuesday February 20, 2018 @07:22PM (#16425664)
Your question relates more to IPython Kernel than Jupyter per se. Fortunately ipykernel has out of the box integration with event loops, e.g. https://github.com/ipython/ipykernel/blob/master/ipykernel/e... for asyncio integration, and you can then schedule cell updates on the asyncio event loop as you would normally. To have events on the Python side generate visible updates in the Jupyter notebook you can use ipywidgets, for which the Python widget state in the kernel is automatically synchronized to the JavaScript widget state.
Re:JupyterLab is ready for users by danielvf on Wednesday February 21, 2018 @07:49AM (#16429038)
Thanks! This looks like exactly what I was looking for.
Re:JupyterLab is ready for users by pbhjpbhj on Tuesday February 20, 2018 @11:01AM (#16421292)
FWIW there's a Snoopy/Woodstock game on code.org that's a much simplified version of this where players control avatars who throw snowballs. It's turn based, each player programs their avatar with 6 different commands (IIRC, u d l r jump fire). Fun and very accessible for my 8yo and I.

Can't find it on CODE.org any more, it's mentioned here http://www.gameinformer.com/b/news/archive/2017/11/10/little....

Re:JupyterLab is ready for users by Tenoke on Tuesday February 20, 2018 @10:27AM (#16421004)
The simplest (and still educational) way would be to have the stuff in the different cells as functions, and to collect all the functions that should be re-run in a single cell.

To do it more properly, however, you can have a post_save_hook for the whole notebook, which runs the code from whichever cells however often you want outside of the notebooks. Of course, this way you wont see the output in the notebook.

Re:JupyterLab is ready for users by danielvf on Tuesday February 20, 2018 @10:32AM (#16421058)
Yeah, output right in the notebook is important. I'm wanting them to be able to do something like this in a cell:

    for ship in myShip.radarScan():
        if ship.is_enemy:
            display([ship.type, ship.range, ship.bearing])
And then have that cells results always showing the current data as they play the game.
Re:JupyterLab is ready for users by Tenoke on Tuesday February 20, 2018 @10:42AM (#16421144)
I haven't used it much, but I believe you can do this by using (or more likely slightly extending) the init_cell nbextension.

https://jupyter-contrib-nbextensions.readthedocs.io/en/lates...

Re:JupyterLab is ready for users by danielvf on Tuesday February 20, 2018 @10:52AM (#16421223)
Oh, that's cool - it's just some javascript that's essentially clicking execute on the selected cells. That makes sense, and hacked up version of this could work fine! Thanks!
Re:JupyterLab is ready for users by lowpro on Tuesday February 20, 2018 @11:28AM (#16421511)
If you get this working my cousin teaches elementary school and I'd love to show it to her! Please upload it if you're feeling generous :)
Re:JupyterLab is ready for users by danielvf on Tuesday February 20, 2018 @11:53AM (#16421753)
I'll definitely post it to GitHub, and I'll do a Show HN after I've used it in class.
Re:JupyterLab is ready for users by po84 on Wednesday February 21, 2018 @07:22AM (#16428902)
> And then have that cells results always showing the current data as they play the game.

display handles that you can update from elsewhere in the notebook might be of interest. Here's an example:

https://github.com/ipython/ipython/blob/master/examples/IPyt...

Re:JupyterLab is ready for users by cat199 on Tuesday February 20, 2018 @10:20AM (#16420950)
> multiplayer navel game

I don't think you mean this, though it could be intriguing just as well..

https://en.wikipedia.org/wiki/Omphaloskepsis

Re:JupyterLab is ready for users by danielvf on Tuesday February 20, 2018 @10:26AM (#16421002)
Nope! Thanks for the catch!
Re:JupyterLab is ready for users by highesttide on Tuesday February 20, 2018 @12:32PM (#16422109)
Related to that idea, there is a game called screeps [0] that has a lot of that general idea and executes it fairly well. The payment model discouraged me from sticking with it, sadly.

[0] https://screeps.com

Re:JupyterLab is ready for users by tcskeptic on Wednesday February 21, 2018 @11:22AM (#16430569)
What you describe sounds quite similar to the old Robocode project for Java programming, it may be worth taking a look at it for ideas/implementation: http://robowiki.net/wiki/Robocode
Re:JupyterLab is ready for users by coding123 on Tuesday February 20, 2018 @01:46PM (#16422791)
This is actually a good idea for this age range. If this was a college class I would recommend a more traditional programming environment, but this should be just about as cool as you can get if you can pull off the networking.
Re:JupyterLab is ready for users by kd5bjo on Tuesday February 20, 2018 @12:26PM (#16422053)
I haven't looked a Jupyter at all; in plain Python, I'd start with something like this[1]:

  schedule = set() # (time, function) pairs
  while True:
    next=min(schedule)
    schedule.remove(next)
    sleep(next[0] - now())
    next[1]()
    schedule.add((now() + interval, next[1]))
    
[1] Pseudocode only; I don't have the API reference in front of me to verify method names and such. Several edge cases are unhandled.
Re:JupyterLab is ready for users by mrbarletta on Wednesday February 21, 2018 @06:25PM (#16433884)
Hi Teacher - please teach them without war/combat games, there are so many fun things to do that don't involve guns, battles, fire, enemies :)
Re:JupyterLab is ready for users by Quenty on Tuesday February 20, 2018 @11:00AM (#16421278)
I know that matplotlib.pyplot has a sleep function used to animate graphs. May be worth investigating.
Re:JupyterLab is ready for users by monkmartinez on Tuesday February 20, 2018 @01:21PM (#16422543)
> Any advice on how to do this in Juptyer with Python? In an ideal world, I'd just "tag" a cell somehow so that it ran periodically.

Don't. Use something that will engage them visually and mentally. Kids are not impressed with text, that ship has sailed.

Re:JupyterLab is ready for users by icebraining on Tuesday February 20, 2018 @02:18PM (#16423182)
Isn't that what the map on the wall is for?