openbable dockerfile and sample code

This commit is contained in:
2025-10-15 21:09:09 +03:00
parent 742db8a37d
commit 908b2e870d
2 changed files with 90 additions and 0 deletions

15
app.py Normal file
View File

@@ -0,0 +1,15 @@
from openbabel import openbabel
mol = openbabel.OBMol()
print(mol.NumAtoms()) #Should print 0 (atoms)
a = mol.NewAtom()
a.SetAtomicNum(6) # carbon atom
a.SetVector(0.0, 1.0, 2.0) # coordinates
b = mol.NewAtom()
mol.AddBond(1, 2, 1) # atoms indexed from 1
print(mol.NumAtoms()) #Should print 2 (atoms)
print(mol.NumBonds())
mol.Clear();

75
dockerfile Normal file
View File

@@ -0,0 +1,75 @@
FROM ubuntu:18.04
ARG DEBIAN_FRONTEND=noninteractive
# Install openbabel dependencies
RUN apt-get update \
&& apt-get install --yes --quiet --no-install-recommends \
libboost-filesystem1.65.1 \
libboost-iostreams1.65.1 \
libboost-program-options1.65.1 \
libboost-regex1.65.1 \
libboost-system1.65.1 \
libboost-test1.65.1 \
libpython3.6 \
libxml2 \
python3 \
python3-cairo \
swig \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
ARG OPENBABEL_VERSION=3.1.1
ARG OPENBABEL_HOME=/usr/local/openbabel/$OPENBABEL_VERSION
SHELL ["/bin/bash", "-c"]
# Install openbabel
RUN build_deps="\
build-essential \
cmake \
libboost-filesystem1.65-dev \
libboost-iostreams1.65-dev \
libboost-program-options1.65-dev \
libboost-regex1.65-dev \
libboost-system1.65-dev \
libboost-test1.65-dev \
libboost1.65-dev \
libcairo2-dev \
libeigen3-dev \
libxml2-dev \
python3-dev \
rapidjson-dev \
wget \
zlib1g-dev" \
&& apt-get update \
&& apt-get install --yes --quiet $build_deps \
&& wget --quiet --no-hsts --output-document=- https://github.com/openbabel/openbabel/archive/openbabel-${OPENBABEL_VERSION//./-}.tar.gz | tar -zxvf - -C /tmp \
&& mkdir -p /tmp/openbabel-openbabel-${OPENBABEL_VERSION//./-}/build \
&& cd /tmp/openbabel-openbabel-${OPENBABEL_VERSION//./-}/build \
&& cmake .. \
-Wno-dev \
-DRUN_SWIG=ON \
-DPYTHON_BINDINGS=ON \
-DPYTHON_EXECUTABLE=/usr/bin/python3.6 \
-DCMAKE_INSTALL_PREFIX=$OPENBABEL_HOME \
-DCMAKE_BUILD_TYPE=Release \
&& make -j $(nproc) && make install \
# This is a heck to pass Test #218, which fails
# because \N in a string is interpreted for unicode
# but in our case \N has nothing to do with unicode.
# So we make the string as a raw string.
&& sed -i -e "45s|'C|r'C|" -e "50s|'C|r'C|" /tmp/openbabel-openbabel-${OPENBABEL_VERSION//./-}/test/testdistgeom.py \
&& make test \
&& ln -s $OPENBABEL_HOME/lib/python3.6/site-packages/openbabel /usr/local/lib/python3.6/dist-packages/openbabel \
&& cd / && rm -rf /tmp/* \
&& apt-get purge --yes --auto-remove $build_deps \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
# Set environment variables
ENV LD_LIBRARY_PATH=$OPENBABEL_HOME/lib:$LD_LIBRARY_PATH
ENV PATH=$OPENBABEL_HOME/bin:$PATH
WORKDIR /var/local
COPY . .
CMD ["/usr/bin/python3", "app.py"]