From 66348ccc0a816c3da87b9bfe841f30dd9a95f1e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20S=C3=A1nchez=20Alba?= Date: Thu, 18 May 2023 07:06:06 -0600 Subject: [PATCH] Added example with Docker and Flask (#519) * Added example with Docker and Flask * set flask debug flag to False * new line at the end of file * format with black --------- Co-authored-by: Ricardo Sanchez Alba --- examples/docker-flask/Dockerfile | 28 ++++++++++++++++++++++++++ examples/docker-flask/README.md | 6 ++++++ examples/docker-flask/app.py | 22 ++++++++++++++++++++ examples/docker-flask/requirements.txt | 21 +++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 examples/docker-flask/Dockerfile create mode 100644 examples/docker-flask/README.md create mode 100644 examples/docker-flask/app.py create mode 100644 examples/docker-flask/requirements.txt diff --git a/examples/docker-flask/Dockerfile b/examples/docker-flask/Dockerfile new file mode 100644 index 0000000..666f358 --- /dev/null +++ b/examples/docker-flask/Dockerfile @@ -0,0 +1,28 @@ +# Use the official Python image as the base image +FROM python:3.9-slim + +# Set the working directory +WORKDIR /app + +# Copy the requirements file +COPY requirements.txt . + +#Install the libcups library +RUN apt-get update -y && apt-get install libcups2-dev -y +# Install the Python packages +RUN pip install --no-cache-dir -r requirements.txt +RUN pip install python-escpos --pre + +# Install Git +RUN apt-get update && \ + apt-get install -y git && \ + rm -rf /var/lib/apt/lists/* + +# Copy the Flask app +COPY app.py . + +# Expose the port the Flask app will run on +EXPOSE 8080 + +# Run the Flask app +CMD ["python", "app.py"] diff --git a/examples/docker-flask/README.md b/examples/docker-flask/README.md new file mode 100644 index 0000000..a2dd254 --- /dev/null +++ b/examples/docker-flask/README.md @@ -0,0 +1,6 @@ +Simple example on how to use it inside a web service + +``` +docker build . -t escpos-web +docker run --network=host -p 9999:9999 escpos +``` \ No newline at end of file diff --git a/examples/docker-flask/app.py b/examples/docker-flask/app.py new file mode 100644 index 0000000..7febda9 --- /dev/null +++ b/examples/docker-flask/app.py @@ -0,0 +1,22 @@ +import escpos +from escpos.printer import * +from flask import Flask, jsonify, request, redirect, session, url_for +import sys +from io import BytesIO + +# Initialize Flask app +app = Flask(__name__) + + +@app.route("/", methods=["GET"]) +def do_print(): + # p = Usb(0x04b8, 0x0e28, 0) + p = CupsPrinter(host="localhost", port=631, printer_name="TM-T20III") + p.text("Hello World\n") + p.cut() + p.close() + return "OK" + + +if __name__ == "__main__": + app.run(debug=False, host="0.0.0.0", port=9999) diff --git a/examples/docker-flask/requirements.txt b/examples/docker-flask/requirements.txt new file mode 100644 index 0000000..bb44932 --- /dev/null +++ b/examples/docker-flask/requirements.txt @@ -0,0 +1,21 @@ +appdirs==1.4.4 +argcomplete==3.0.8 +blinker==1.6.2 +click==8.1.3 +Flask==2.3.2 +future==0.18.3 +itsdangerous==2.1.2 +Jinja2==3.1.2 +MarkupSafe==2.1.2 +Pillow==9.5.0 +pycups==2.0.1 +pypng==0.20220715.0 +pyserial==3.5 +python-barcode==0.14.0 +python-escpos==3.0a9 +pyusb==1.2.1 +PyYAML==6.0 +qrcode==7.4.2 +six==1.16.0 +typing_extensions==4.5.0 +Werkzeug==2.3.4 \ No newline at end of file