# Jupyter Display Playground Nextjournal’s Jupyter runtimes now fully support all of Jupyter’s [display kernel messages](https://jupyter-client.readthedocs.io/en/stable/messaging.html#display-data). Remix this notebook to try them out yourself. This notebook already uses a *Python Jupyter runtime*. You can select a runtime’s type (e.g. Jupyter or Nextjournal) via the *runtime settings menu* (available from the sidebar or from the runtimes section listed at the bottom of this document). # Showing multiple results Let’s start by running this cell to import `IPython.display` and also `numpy` for some of the following examples: ```python id=94751a7e-f3f7-44b4-93f6-f2c5fbe9354c from IPython.display import display, HTML, Audio import numpy as np ``` As you run the next cell, you will see that calling [`display`](https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html#IPython.display.display) multiple times (each time passing a rich media/MIME bundle object) will stack corresponding results below the cell: ```python id=1ca7868b-0e57-4498-8317-7ea7e676a01e dynamite = "

🧨

" display(HTML(dynamite)) d1 = display(HTML(dynamite), display_id=True) d2 = display(HTML(dynamite), display_id=True) sample_rate=44100 noise = np.random.normal(0, 1, size=sample_rate) display(Audio(noise, rate=sample_rate)) '✅ last return will also be shown as last result' ``` # Updating results In addition to that, we also support changing display objects in place by calling `update` on the [display handle](https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html#IPython.display.DisplayHandle). Note that, when running the following cell, the last 🧨 from the above cell’s results will turn into a 💥. ```python id=1d0aceb4-465c-4f64-af81-ac3a22c9d6c3 import time time.sleep(0.5) d2.update(HTML("

💥

")) ``` To demonstrate a more elaborate version of this, let’s implement *[Conway's Game of Life](http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life)* based on code by [@gvwilson](http://twitter.com/gvwilson) & [@jiffyclub](https://twitter.com/jiffyclub). First, let’s set up a function that draws the cells: ```python id=104bf0ba-43c9-44d6-a015-7b8bee506e27 def board2html(board): nx, ny = board.shape table = '\n' for y in range(ny-1, -1, -1): table += '' for x in range(0, nx): if board[x, y]: table += '' else: table += '' table += '\n' table += '
' return table ``` We will also need functions to evolve and advance the cells based on their neighbours: ```python id=169d4032-1b44-471a-9e48-5288710e32be import time def evolve(length, generations): current = np.random.randint(2, size=(length, length)) next = np.empty_like(current) h = display(HTML(board2html(current)), display_id=True) for _ in range(generations): advance(current, next) current, next = next, current h.update(HTML(board2html(current))) time.sleep(0.5) ``` ```python id=a25d1279-9a38-47e1-bfeb-246e1fd2f482 from scipy.signal import convolve # used for counting the number of living neighbors each cell has FILTER = np.array([[1, 1, 1], [1, 100, 1], [1, 1, 1]], dtype=np.uint8) def advance(current, next): assert current.shape[0] == current.shape[1], 'Expected square universe' next[:] = 0 count = convolve(current, FILTER, mode='same') next[(count == 3) | (count == 102) | (count == 103)] = 1 ``` And finally, let’s call evolve to draw the evolution: ```python id=2aeeb1a5-bcdc-4b60-a460-5ec1a595a5d2 evolve(30, 20) ``` That’s it for now. You can read up on Jupyter’s kernel display messages in the [Project Jupyter Docs](https://jupyter-client.readthedocs.io/en/stable/messaging.html#display-data) and if you have feedback for us, please [get in touch](#intercom)!
This notebook was exported from https://nextjournal.com/a/MFXzU9rygYcZX8AAg2kJh?change-id=CuJDUfvhCfLZXXcAbX45y6 ```edn nextjournal-metadata {:article {:nodes {"104bf0ba-43c9-44d6-a015-7b8bee506e27" {:compute-ref #uuid "c1d91282-1f7c-43f4-a927-42eafe481f6f", :exec-duration 38, :id "104bf0ba-43c9-44d6-a015-7b8bee506e27", :kind "code", :output-log-lines {}, :runtime [:runtime "2cadeb60-b168-49e7-afc4-f2e7b59e16a7"]}, "169d4032-1b44-471a-9e48-5288710e32be" {:compute-ref #uuid "3de74fba-8f13-47cb-b3e9-577c9c98e7a3", :exec-duration 45, :id "169d4032-1b44-471a-9e48-5288710e32be", :kind "code", :output-log-lines {}, :runtime [:runtime "2cadeb60-b168-49e7-afc4-f2e7b59e16a7"]}, "1ca7868b-0e57-4498-8317-7ea7e676a01e" {:compute-ref #uuid "bd4cc2dc-2504-4cb0-a733-acf953b37b1b", :exec-duration 245, :id "1ca7868b-0e57-4498-8317-7ea7e676a01e", :kind "code", :output-log-lines {}, :runtime [:runtime "2cadeb60-b168-49e7-afc4-f2e7b59e16a7"]}, "1d0aceb4-465c-4f64-af81-ac3a22c9d6c3" {:compute-ref #uuid "20cb704e-925e-4ec7-a777-9a4b38173d9c", :exec-duration 538, :id "1d0aceb4-465c-4f64-af81-ac3a22c9d6c3", :kind "code", :output-log-lines {}, :runtime [:runtime "2cadeb60-b168-49e7-afc4-f2e7b59e16a7"]}, "2aeeb1a5-bcdc-4b60-a460-5ec1a595a5d2" {:compute-ref #uuid "0e7e926f-2a20-425c-a76e-afdfd746815c", :exec-duration 10248, :id "2aeeb1a5-bcdc-4b60-a460-5ec1a595a5d2", :kind "code", :output-log-lines {}, :runtime [:runtime "2cadeb60-b168-49e7-afc4-f2e7b59e16a7"]}, "2cadeb60-b168-49e7-afc4-f2e7b59e16a7" {:environment [:environment {:article/nextjournal.id #uuid "5b45e08b-5b96-413e-84ed-f03b5b65bd66", :change/nextjournal.id #uuid "5df5e18c-0be4-4d8d-b099-6ce55ca12cf4", :node/id "0149f12a-08de-4f3d-9fd3-4b7a665e8624"}], :id "2cadeb60-b168-49e7-afc4-f2e7b59e16a7", :kind "runtime", :language "python", :type :jupyter}, "94751a7e-f3f7-44b4-93f6-f2c5fbe9354c" {:compute-ref #uuid "9ea16b8e-cca4-4145-9334-feb7ffb225fa", :exec-duration 850, :id "94751a7e-f3f7-44b4-93f6-f2c5fbe9354c", :kind "code", :output-log-lines {}, :runtime [:runtime "2cadeb60-b168-49e7-afc4-f2e7b59e16a7"]}, "a25d1279-9a38-47e1-bfeb-246e1fd2f482" {:compute-ref #uuid "7039b262-dfbd-482b-ba01-111c1750df27", :exec-duration 1302, :id "a25d1279-9a38-47e1-bfeb-246e1fd2f482", :kind "code", :output-log-lines {}, :runtime [:runtime "2cadeb60-b168-49e7-afc4-f2e7b59e16a7"]}}, :nextjournal/id #uuid "02d3d51e-722b-4f18-9e6d-ba2dda018706", :article/change {:nextjournal/id #uuid "6061cf7c-a898-4753-b06c-5df18ea7a09d"}}} ```