Since I received a comment in the Last time article, I will show you how to execute the script from Jupyter. Your shell art will shine even more with Jupyter. : star2:
Use cell magic %% script
to execute the script passed as an argument. For example, %% script python3.7
runs Python3.7, and %% script bash
runs bash. Enter the contents of the script directly in the cell.
I will try running bash.
%%script bash
for i in 1 2 3 ; do
echo $i
done
1
2
3
I was able to execute the bash I wrote in the Jupyter cell. The advantage of running with Jupyter is that you can process and visualize the results.
As an example, let's run a script to get the number of processes and visualize the result in pandas.
%%script bash --out bash_out
echo "date,procs"
for i in `seq 10` ; do
echo "`date +%Y-%m-%d-%H:%M:%S`,`ps -ef | grep -cv grep`"
sleep 5
done
You can store it in a Python object with the name you passed in the --out
option.
print(bash_out)
date,procs
2019-12-06-10:49:21,337
2019-12-06-10:49:26,340
2019-12-06-10:49:31,340
2019-12-06-10:49:36,342
2019-12-06-10:49:41,340
2019-12-06-10:49:46,341
2019-12-06-10:49:51,344
2019-12-06-10:49:56,343
2019-12-06-10:50:01,342
2019-12-06-10:50:06,341
Let's load this into a pandas DataFrame.
from io import StringIO
import pandas as pd
df = pd.read_csv(StringIO(bash_out), index_col=["date"], parse_dates=["date"])
print(df)
procs
date
2019-12-06 10:49:21 337
2019-12-06 10:49:26 340
2019-12-06 10:49:31 340
2019-12-06 10:49:36 342
2019-12-06 10:49:41 340
2019-12-06 10:49:46 341
2019-12-06 10:49:51 344
2019-12-06 10:49:56 343
2019-12-06 10:50:01 342
2019-12-06 10:50:06 341
Visualize quickly with the plot
method.
%matplotlib inline
df.plot()
In this way, Jupyter can execute arbitrary scripts and process the results with Python objects. Have a good Jupyter life!
Recommended Posts