How to set-up?

In this post, I show how to toggle a code in a RISE presentation.
Firstly, you have to make "toggle_sing_code.py" described below.
Then put this file in a directory (for me /mnt/c/Users/mnaka/OneDrive/3_code_script/Python/).

In [1]:
%%bash
cat /mnt/c/Users/mnaka/OneDrive/3_code-script/Python/toggle_single_code.py
from IPython.display import HTML
from IPython.display import display
 
def toggle_code():
	tag = HTML('''<script>
	code_show=false; 
	function code_toggle() {
		if (code_show){
			$('div.cell.code_cell.rendered.selected div.input').hide('500');
		} else {
			$('div.cell.code_cell.rendered.selected div.input').show('500');
		}
		code_show = !code_show
	} 
	 
	$( document ).ready(code_toggle);
	</script>
	<div align="right"> 
	<form action="javascript:code_toggle()"><input
	type="submit"
	value="Toggle Code"
	style="background-color:#111111;
	edge-color:#ffffff;
	font-weight: bold;
	color: #ffffff"></form>
	</div>''')
	return display(tag)
 

How to import?

You import a function to toggle a code like below in a ipynb file.

In [2]:
import sys
sys.path.append("/mnt/c/Users/mnaka/OneDrive/3_code-script/Python")
from toggle_single_code import toggle_code

How to use it?

You put "toggle_code()" in a cell.
If you put it before defining figure, the buttom pops up at upper-right.
If you put it after showing figure, the buttom pops up at lower-right.

In [3]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

toggle_code()
fig=plt.figure()
x=np.linspace(0,2*np.pi,1000)
y=np.sin(x)

plt.plot(x,y)
plt.show()
toggle_code()