2018年10月16日火曜日

VagrantでJupyter Lab、foliumとMariaDBをインストールした仮想マシン(Ubuntu18.04)を構築する

Jupyter Labでインタラクティブなコンピューティング環境を提供する事ができます。foliumで簡単に地図を表示できます。

○Jupyter Labとfoliumの画面


仮想マシンの構築方法

以下のVagrantfileで、Jupyter LabとMariaDBをインストールした仮想マシンを構築できます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = "ub1804jupyterlabfoliummariadb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1804jupyterlabfoliummariadb"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.104", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.104", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
# update packages
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
locale-gen ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8

# install mariadb
echo "mariadb-server-10.3 mysql-server/root_password password root" | sudo debconf-set-selections
echo "mariadb-server-10.3 mysql-server/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mariadb-server
mysql -uroot -proot -e "CREATE DATABASE test DEFAULT CHARACTER SET utf8mb4;"
mysql -uroot -proot -e "CREATE USER test@localhost IDENTIFIED BY 'test';"
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON test.* TO 'test'@'localhost';"
mysql -uroot -proot -e "FLUSH PRIVILEGES;"
mysql -utest -ptest test -e "create table messages (message_id integer not null, message varchar(100));"
mysql -utest -ptest test -e "insert into messages value (1, 'hello world.');"
mysql -utest -ptest test -e "insert into messages value (2, 'test message.');"

# install anaconda & jupyterlab
wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
chmod +x Anaconda3-5.1.0-Linux-x86_64.sh
./Anaconda3-5.1.0-Linux-x86_64.sh -b -p /opt/anaconda
source /opt/anaconda/bin/activate
pip install --upgrade pip
pip install jupyterlab
pip install folium

# install mysqlclient
apt-get -y install libmariadbclient-dev build-essential
pip install mysqlclient

useradd py
mkdir -p /home/py
chown -R py:py /home/py
sudo -u py bash -c "mkdir /home/py/.jupyter"
sudo -u py bash -c "cat << EOF > /home/py/.jupyter/jupyter_notebook_config.py
conf = get_config()
conf.NotebookApp.ip = '*'
conf.NotebookApp.open_browser = False
conf.NotebookApp.port = 8080
conf.NotebookApp.token = 'jupyter'
EOF"

cat << EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
EnvironmentFile=/opt/anaconda/bin/activate
ExecStart=/opt/anaconda/bin/jupyter lab
User=py
Group=py
WorkingDirectory=/home/py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable jupyter
sudo systemctl start jupyter

echo 'access -> http://192.168.55.104:8080/?token=jupyter'
SHELL
end

○動作確認用コード(MariaDB接続確認)
import MySQLdb

con = MySQLdb.connect(
  user='test',
  passwd='test',
  host='localhost',
  db='test')

try:
  cur= con.cursor()
  sql = "select message_id, message from messages"
  cur.execute(sql)

  for row in cur.fetchall():
    print(row)

except MySQLdb.Error as er:
    print('MySQLdb.Error:', er)
cur.close
con.close

〇動作確認用コード(folium動作確認)
import folium

fm = folium.Map(location=[35.856999, 139.648849], zoom_start=10)

folium.Marker(location=[35.861729,139.645482], popup='さいたま市').add_to(fm)
folium.Marker(location=[35.975198, 139.752301], popup='春日部市').add_to(fm)
    
fm

関連情報

・Jupyterに関する他の情報はこちらを参照してください。

0 件のコメント:

コメントを投稿