wtfpython icon indicating copy to clipboard operation
wtfpython copied to clipboard

Interactive online demo for Python snippets

Open martinzugnoni opened this issue 6 years ago • 9 comments

Hey folks!

I'm personally a big fan of wtfpython and I always keep an eye on your updates because I always learn something new with them (despite being a 10+ years Python coder). We also use them to teach our students at https://rmotr.com/ (co-founder and teacher here).

While looking at the Python snippets in the README file I thought it would be great to provide some interactive demo that people can use to play with them. Something we've learnt is that getting hands on code as quick as possible is the best way to learn to code.

I spent a few hours compiling all the Python snippets in a Jupyter Notebook file, and adapted it in a way that we can open it with a small service we have at RMOTR to launch Jupyter environments online. The result is what you can see in my fork of the repo (check the Demo section under the table of content): https://github.com/martinzugnoni/wtfpython

Do you agree that having such interactive demo would help all the folks reading wtfpython? Let's use this issue as a kick off to start a discussion. Nothing here is written in stone and we can change everything as you wish.

I hope you guys like it, and I truly appreciate any feedback.

thanks.

martinzugnoni avatar Dec 10 '18 17:12 martinzugnoni

Hi @martinzugnoni

The remote jupyter notebook you have put up is pretty cool. And I'm glad that this project is helpful to you in teaching.

I agree with you that having the snippets in README kills any chances of interactivity possible (interactive way is better for learning of course). For the same reason, few months back we decided to have a web version of wtfpython (sort of an interactive quizzing site). Since it was something that we planned for GSSoC, we weren't able to put much work into it due to lack of potential contributors.

We did realize that one important step to add any kind of interactivity or extensiblity was to strcuture the snippets in some standard format like JSON, and render everything else from it. Everything could be websites, pdfs, pypi packages, or jupyter notebooks. The rationale behind it was there are many minor updates and corrections that keep on happening, and it makes no sense to update all the linked projects over and over again. Here's some discussion for the same.

I'm not sure if which approach is better amongst web and jupyter notebook (or maybe an intreactive pypi module), but there are a few things which can be done in web which I don't know how to do it in jupyter. Couple of them are:

  • Specify an interpreter version for a cell. Because there are some snippets which work exclusively on certain ptyhon versions.
  • Fetch data from a JSON file and then render everything in the form of cells (for the same reason we don't want to update it over and over again corresponding to any change here in master)
  • Scoring. Adding socres makes it more interesting, I did come across some universities using Jupyter notebooks for their assignments and grading them (they had some cells with unmodifiable/hidden code which evaluated the output of the questions), but will need to research about how to implement similar functionality.

Looking forward to your thoughts on this.

satwikkansal avatar Dec 11 '18 06:12 satwikkansal

Hi @satwikkansal, thanks for your answer.

Jupyter Notebook files are plain JSON files underneath (see the raw version of the demos here). I believe that notebooks is one of the most convenient formats to store code snippets. Not only because the structure of the file was designed for that purpose, but also because it provides the possibility of making cells executable. Something that won't be achieved with any other static format.

About the Python versions, in Jupyter it's possible to change the execution "Kernel" to different languages or versions. This service is running with Python3.x by default, but we could enable the Python2.7 kernel as well. I think it's not possible to define a different kernel on a cell-basis, but we could split the examples into two different notebooks, one for 3.x and one for 2.7. Not sure if this could be an option. Alternatively we can leave all snippets in a single notebook and the user can switch the kernel to 2.7 manually before executing a py2.7 code example (we can foster that by adding some comments or instructions in the notebook).

What would be the goal of scoring? Is it to differentiate "popular" snippets from others or is it for some kind of grading?

thanks.

martinzugnoni avatar Dec 15 '18 12:12 martinzugnoni

Hi @martinzugnoni

It's interesting to know that jupyter notebooks are store information in JSON format. For multiple python versions, I think adding a note for users to change the kernel version manually should be sufficiect to begin with.

There are two goals behind scoring that I can think of,

First, we wanted a sort of quizzing like site where users can test their familiarity with Python. We felt that adding scores and some sort of interactivity would make the project more engaging for the readers.

Secondly, the scoring data would enable us to do snippet-level analysis. We can figure out the snippets which most people get wrong, the snippets which most of the people get right (hinting that they're not WTFs), perecentage of people actually completely reading the collection, time taken by them to do so, hotspots where most people stop reading and leave, and so on. We had certain hypothesis about such stats and wanted to see the actual numbers just out of curiousity.

Anyways, if you feel that scoring and analysis are a bit far-fetched, I totally understand, and having a jupyter version of the project seems like a good idea nonetheless.

satwikkansal avatar Dec 16 '18 11:12 satwikkansal

Sounds good! I will see how to solve the Python2.7 execution and come back with a proposal. I can directly open a PR with the DEMO notebook so you can directly review it there.

martinzugnoni avatar Dec 16 '18 12:12 martinzugnoni

Awesome, looking forward to it!

On Sun, 16 Dec 2018 at 6:24 PM, Martin Zugnoni [email protected] wrote:

Sounds good! I will see how to solve the Python2.7 execution and come back with a proposal. I can directly open a PR with the DEMO notebook so you can directly review it there.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/satwikkansal/wtfpython/issues/110#issuecomment-447641104, or mute the thread https://github.com/notifications/unsubscribe-auth/AJvoP4I5lxv3xyMbMsje_4xHb6bJV7laks5u5kJ8gaJpZM4ZL145 .

-- Thanks and Regards,

Satwik Kansal Linkedin https://linkedin.com/in/satwikkansal | https://satwikkansal.xyz

satwikkansal avatar Dec 16 '18 13:12 satwikkansal

Hi @martinzugnoni

Just checking, Were you able to figure out?

satwikkansal avatar May 01 '19 15:05 satwikkansal

Hello @satwikkansal, sorry about the late response. We've been working in some changes about the Notebook service and moved it as a completely separate product on its own page: https://notebooks.ai/

About the Python 2.7 execution, it's not as simple as I thought initially. We are running the code in Docker containers that are built either with Python 2.7 or 3.6, but not both versions together.

The easiest way I can imagine is to separate the 2.7 and 3.6 in two separated Notebook files and run them in separated environments. Would that work?

Otherwise we can think about including only Python 3.6 examples for now until we find a better solution.

If any of the options work, I can create a PR in the upcoming days.

Thank you.

martinzugnoni avatar May 01 '19 15:05 martinzugnoni

@martinzugnoni Gotcha, I think we can start wtih Python 3.6 examples only.

Also, I did some work on json-generator script, that'll parse the README and convert the examples to json format, here's the python file. The task is half-done till now. What I've planned is to create a json of the form

[
  {
    "id": "some_integer_specific to the order",
    "category": "section_to_which_it_belongs",
    "title": "ofcourse_the_title",
    "parts": {
      "build_up": [
          {
            "type": "text",
            "sequence_num": 1,
            "value": "Markdown here",
            "meta": {}
          },
          {
            "type": "code",
            "sequence_num": 2,
            "value": "unescaped snippet of code here"
            "meta": {
              "language": "sh/py",
              "version": "all/ < / > / exact",
              "contains_wtf": false
            },
            "parsed_values": [
                {
                  "type": "statement/output",
                  "value": [],
                  "is_wtf": false
                }
              ]
          }
        ],
      "explanation": [
        ...
        similar to code.
        ...
        ]
    }
    "keywords": []
  }]

And once this is done, the next task I have in mind is to write another script that can convert this json into a jupyter notebook. If you'd like to work on the later task (to convert that generated json into ipynb for notebooks.ai), then do let me know.

satwikkansal avatar May 01 '19 17:05 satwikkansal

Hey, have you guys looked at binder for hosting notebooks straight from the repo?

  • https://mybinder.org
  • https://mybinder.readthedocs.io/en/latest/
  • https://github.com/binder-examples/
  • https://gitter.im/jupyterhub/binder

I think this repo is great btw and making it interactive would be super cool!

EDIT: RE: Python 2 vs 3. When you request a Python 2 environment from Binder, it is installed as a kernel inside a Python 3 environment. Which means it's totally possible to have a v3 notebook and v2 notebook running in the same instance 🙂

sgibson91 avatar May 15 '19 09:05 sgibson91