jupyterquiz icon indicating copy to clipboard operation
jupyterquiz copied to clipboard

Decouple Python from JavaScript

Open echarles opened this issue 2 years ago • 3 comments

I would like to implement my own UI in React.js. Looking at the source I see that the python code is generating the javascript https://github.com/jmshea/jupyterquiz/blob/d51c1547e9bb6ae331ae06d3fce830f5dc009e2b/jupyterquiz/dynamic.py#L185-L209.

Is there a way to decouple more Python from JavaScript so that 3rd party UI can be implemented on top of this library?

echarles avatar Jul 27 '22 05:07 echarles

I am definitely open to ideas. A few comments:

  1. That particular code is from capture_resonses() and is non-functional -- that was something that I was trying to get to work but couldn't because of the way that Jupyter Lab stores HTML from cell operations right after the cell is run but not later if that HTML is updated via Javascript. I left it in for now, but will probably delete it from the main branch for clarity.

  2. I am outputting Javascript in display_quiz() in order to allow the Python code to pass the quiz data to Javascript in lots of different ways (list of dicts, file name, URL, ...). This is implemented in the big if statement starting a line 67. One really important component to this is that in Jupyter Book, if the quiz data is specified to be pulled from a file, the quiz data from when the Jupyter notebook was run will be saved and used as a fallback if the page is accessed offline in the future.

  3. In addition, I'm creating a new HTML DIV from Python that will be in the output cell, and I am passing the ID of that div to the Javascript function by hard-coding it into the Javascript when the Python is run (lines 129, 148, and 151).

I am definitely a hack at all of the different components (Python, Javascript, HTML, CSS) that make this library work, so I am interested in any ideas you have.

jmshea avatar Jul 27 '22 16:07 jmshea

Thx for the inputs @jmshea. I need a bit more time to try and look at the internals.

To be sure, the library is running on jupyter notebook, jupyterlab and jupyterbook, right?

Have you considered ipywidgets as solution to implement the functionality? That may be a big machinery not needed, just curious if you have thoughts on that.

echarles avatar Jul 29 '22 06:07 echarles

This has been tested with Jupyter Notebook, Jupyter Lab, and Jupyter Book.

This library was made primarily to satisfy my own needs for embedding quizzes in a Jupyter Book document. That requires that the quiz can be delivered without any Python running. It does this by embedding JavaScript into the Jupyter Book page. As an additional requirement, I want it to be styleable so that I can make my online book look nice. I don't know if ipywidgets can achieve those goals.

jmshea avatar Aug 03 '22 14:08 jmshea

Hi All,

I think your package is very interesting and you guys deliver awesome work. Although I cannot help you solve this problem I was curious about the plugin you (@jmshea) mention in the README:

The requirement that the student copy and paste the text output to preserve it is because of limitations in the exchange of information from the JavaScript side to the Python side. As far as I know, the only way around this requires a plug-in, and I do not want to require that. I will continue to investigate solutions to this in the future.

Is this an existing plugin? And does this plugin work in combination with Jupyter Book?

Thanks in advance.

luukalt avatar Jan 23 '23 21:01 luukalt

There is not a Jupyter plugin to do this (as far as I know). I am just trying to convey that I don't think I can achieve the requested behavior with just Python and/or Javascript.

I don't understand what you mean about working in combination with Jupyter Book. The plug-in mentioned would be for saving responses in a Jupyter Notebook. But Jupyter Book is typically for making a web or pdf version of the Notebook. Where would the responses be saved? (I could save them in local web storage, but the original point of saving the responses was to allow them to be submitted for grading.)

jmshea avatar Jan 26 '23 15:01 jmshea

Closing this as there hasn't been any change in status on this for a long time.

jmshea avatar Apr 18 '23 16:04 jmshea