〇Jupyter Labの画面
構築方法
以下のVagrantfileで、Jupyter LabとPostgreSQL11 をインストールした仮想マシン(Ubuntu18.04)を構築できます。Vagrantfile
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "bento/ubuntu-18.04"
config.vm.hostname = "ub1804jupyterpg11"
config.vm.provider :virtualbox do |vbox|
vbox.name = "ub1804jupyterpg11"
vbox.cpus = 2
vbox.memory = 2048
vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
end
config.vm.network "private_network", ip: "192.168.55.106", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.106", :netmask => "255.255.255.0"
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get -y install language-pack-ja
sed -i.bak -e "s#http://archive.ubuntu.com/ubuntu/#http://ftp.riken.jp/pub/Linux/ubuntu/#g" /etc/apt/sources.list
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
timedatectl set-timezone Asia/Tokyo
# install postgresql
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
apt-get -y install wget ca-certificates
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
apt-get update
apt-get upgrade
apt-get -y install postgresql-11
echo "listen_addresses='*'" >> /etc/postgresql/11/main/postgresql.conf
#sed -i 's/host.*all.*all.*127.0.0.1/#host all all 127.0.0.1/g' /etc/postgresql/11/main/pg_hba.conf
echo "host all all 127.0.0.1/32 password" >> /etc/postgresql/11/main/pg_hba.conf
echo "host all all 192.168.1.0/24 password" >> /etc/postgresql/11/main/pg_hba.conf
echo "host all all 192.168.55.0/24 password" >> /etc/postgresql/11/main/pg_hba.conf
systemctl restart postgresql.service
su - postgres << EOF
createdb -T template0 --locale=ja_JP.UTF-8 --encoding=UTF8 test
psql -c "
alter user postgres with password 'postgres';
create user test with password 'test';
grant all privileges on database test to test;
"
EOF
export PGPASSWORD=test
psql -h localhost -d test -U test -c "
create table messages (message_id integer not null, message varchar(100));
insert into messages values (1, 'hello world.');
insert into messages values (2, 'test message.');
"
EOF
echo "postgres:postgres" | chpasswd
systemctl restart postgresql.service
# install anaconda
wget https://repo.anaconda.com/archive/Anaconda3-2018.12-Linux-x86_64.sh
chmod +x Anaconda3-2018.12-Linux-x86_64.sh
./Anaconda3-2018.12-Linux-x86_64.sh -b -p /opt/anaconda
source /opt/anaconda/bin/activate
pip install --upgrade pip
pip install jupyterlab
pip install pygal
# install psycopg2
apt-get -y install libpq-dev python-dev
pip install psycopg2-binary
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
c.JupyterApp.allow_remote_access = True
c.JupyterApp.ip = '0.0.0.0'
c.JupyterApp.open_browser = False
c.JupyterApp.port = 8080
c.JupyterApp.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.106:8080/?token=jupyter'
SHELL
end
〇動作検証用コード
import psycopg2
con = psycopg2.connect("dbname=test host=localhost user=test password=test")
try:
cur= con.cursor()
sql = "select message_id, message from messages"
cur.execute(sql)
for row in cur.fetchall():
print(row)
except psycopg2.Error as er:
print('psycopg2.Error:', er)
cur.close
con.close
関連情報
・Jupyterに関する他の情報はこちらを参照してください。・psycopg2に関する他の記事はこちらを参照してください。
0 件のコメント:
コメントを投稿