Memo to get the value on the html-javascript side with jupyter

Thing you want to do

I want to run html-javascript on jupyter and pass the resulting value to python for use (Example: I want to calculate on the python side using the coordinates drawn on javascript, etc.) When I was thinking about it, I found that there was Custom-Widget and tried it.

Example of use

The following one displays the html form, and the one entered in it is passed to the python side and displayed. [This example](https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Custom.html#Building-a-Custom-Widget --- Date-Picker) is slightly modified, but I'm using javascript, which I'm not used to, so there may be a better way.

無題の図形描画.png

Source

--html creation

%%html
<!DOCTYPE html><html lang="ja">
<head><mata charset="utf-8"/><title>widget test</title></head><body>
<form name="js"><input type="text" name="txt_form" value=""><br></form><script>
require.undef('hello');
define('hello', ["@jupyter-widgets/base"], function(widgets) {
    var HelloView = widgets.DOMWidgetView.extend({
        render: function() {
            this.model.on('change:value', this.value_changed, this);
        },
        value_changed: function() {
            this.model.set('html_form', document.js.txt_form.value);//Get the text in the form here
            this.touch();
        },
    });
    return {
        HelloView : HelloView
    };
});
</script></body></html>

--Create ipywidgets

import ipywidgets as widgets
from traitlets import Unicode
class HelloWidget(widgets.DOMWidget):
    _view_name = Unicode('HelloView').tag(sync=True)
    _view_module = Unicode('hello').tag(sync=True)
    value = Unicode('Hello World!').tag(sync=True)
    html_form = Unicode('').tag(sync=True)
my_widget = HelloWidget()
my_widget#You need to make a widget here

--Get the value in the form (It takes a little time to get it, so if you display the value immediately after it, it will not be updated)

my_widget.value += 'w'#to value[w]By adding, forcibly change the value, raise a synchronization event, and get the value in the form

--Display values in the form

print(my_widget.html_form)#Show values in the form

Recommended Posts

Memo to get the value on the html-javascript side with jupyter
I tried to get started with Bitcoin Systre on the weekend
Get the width of the div on the server side with Selenium + PhantomJS + Python
The easiest way to get started with Django
Drawing tips with matplotlib on the server side
Introduction to Python with Atom (on the way)
How to get the key on Amazon S3 with Boto 3, implementation example, notes
To output a value even in the middle of a cell with Jupyter Notebook
Implement Sign In With Google on the backend side
Monitor the training model with TensorBord on Jupyter Notebook
Try to get the contents of Word with Golang
How to operate Firefox with selenium on Windows Memo
I want to plot the location information of GTFS Realtime on Jupyter! (With balloon)
A memo on how to overcome the difficult problem of capturing FX with AI
How to deal with the phenomenon that Python (Jupyter notebook) executed on WSL becomes Aborted
read the tag assigned to you on ec2 with boto3
Minimum knowledge to get started with the Python logging module
Get the package version to register with PyPI from Git
How to get the last (last) value in a list in Python
The usual way to add a Kernel with Jupyter Notebook
Connect the Jupyter Notebook kernel to Spyder with Jupytext enabled
How to get into the python development environment with Vagrant
Save images on the web to Drive with Python (Colab)
[Introduction to Python] How to get data with the listdir function
Get the host name of the host PC with Docker on Linux
Get the source of the page to load infinitely with python.
Two-factor authentication with Cognito + Amplify (Enter ID / PW / "two-factor code value" on the login screen to authenticate)
Memo of the program to get the date in two digits with javascript, Ruby, Python, shell script
How is the progress? Let's get on with the boom ?? in Python
Get started with the Python framework Django on Mac OS X
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
[Python] A memo that I tried to get started with asyncio
How to customize U-Boot with OSD335X on a custom board (memo)
How to get the directory where the EXE built with Pyinstaller exists
How to run Jupyter and Spark on Mac with minimal settings
[Memo] How to use BeautifulSoup4 (2) Display the article headline with Requests
How to get all traffic through VPN with OpenVPN on Linux
[Python] How to get a value with a key other than value with Enum
How to use Jupyter on the front end of supercomputer ITO
[Memo] How to use BeautifulSoup4 (3) Display the article headline with class_
Machine learning model management to avoid quarreling with the business side
The fastest way to get camera images regularly with python opencv
The easiest way to get Chainer v1.5 + CUDA + cuDNN on Windows
PhytoMine-I tried to get the genetic information of plants with Python
Formatting with autopep8 on Jupyter notebook
Link to get started with python
Extract the maximum value with pandas.
Get started with MicroPython (on macOS)
Get the weather with Python requests
Get the weather with Python requests 2
How to get the Python version
How to get started with Scrapy
How to get started with Python
How to get started with Django
Get the value from the [Django] Form
[Memo] Tweet on twitter with python
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
Steps to get KeePassX key on OS X with one command line
Specify the browser to use with Jupyter Notebook. Especially Mac. (And Vivaldi)