fixed dockerfile, added fastapi

This commit is contained in:
2025-11-24 16:35:06 +03:00
parent 908b2e870d
commit 23e8b71776
5 changed files with 64 additions and 81 deletions

15
app.py
View File

@@ -1,15 +0,0 @@
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();

9
docker-compose.yaml Normal file
View File

@@ -0,0 +1,9 @@
services:
openbabel-app:
build:
context: .
environment:
PORT: 1654
ports:
- 1654:1654
tty: true

View File

@@ -1,75 +1,20 @@
FROM ubuntu:18.04
ARG DEBIAN_FRONTEND=noninteractive
FROM ubuntu:22.04
# 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 \
openbabel \
python3 \
python3-pip \
&& 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 . .
COPY requirements.txt .
CMD ["/usr/bin/python3", "app.py"]
# Install dependencies
RUN /usr/bin/python3 -m pip install --no-cache-dir -r requirements.txt
COPY src .
CMD fastapi run app.py --port ${PORT}

4
requirements.txt Normal file
View File

@@ -0,0 +1,4 @@
openbabel-wheel==3.1.1.22
fastapi[all]==0.121.3
fastapi-cache2==0.2.2
redis==7.1.0

40
src/app.py Normal file
View File

@@ -0,0 +1,40 @@
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from fastapi import FastAPI
from starlette.requests import Request
from starlette.responses import Response
from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend
from fastapi_cache.decorator import cache
from redis import asyncio as aioredis
from openbabel import pybel
@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
redis = aioredis.from_url("redis://localhost")
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
yield
app = FastAPI(lifespan=lifespan)
@app.get("/informats")
async def get_informats():
return pybel.informats
@app.get("/")
@cache(expire=60)
async def index():
return dict(hello="world")