Skip to content

Commit 2a975fe

Browse files
Add Docker support for polygon-api-client usage example (#600)
* Add Docker support for polygon-api-client usage example * Added note re: rebuild on python script changes
1 parent e44ff30 commit 2a975fe

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

examples/tools/docker/Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Use an official Python runtime as a parent image
2+
FROM python:3.8-slim
3+
4+
# Set the working directory in the container
5+
WORKDIR /usr/src/app
6+
7+
# Copy the current directory contents into the container at /usr/src/app
8+
COPY . .
9+
10+
# Install any needed packages specified in requirements.txt
11+
RUN pip install --no-cache-dir polygon-api-client
12+
13+
# Set the environment variable for the Polygon API key
14+
# Note: Replace "<your_api_key>" with your actual API key or use Docker's --env flag when running the container to set it dynamically
15+
# Warning: Not recommended for production or shared environments
16+
ENV POLYGON_API_KEY=<your_api_key>
17+
18+
# Run the script when the container launches
19+
CMD ["python", "./app.py"]

examples/tools/docker/app.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from polygon import RESTClient
2+
3+
# docs
4+
# https://polygon.io/docs/stocks/get_v2_aggs_ticker__stocksticker__range__multiplier___timespan___from___to
5+
# https://polygon-api-client.readthedocs.io/en/latest/Aggs.html#polygon.RESTClient.list_aggs
6+
7+
client = RESTClient() # POLYGON_API_KEY environment variable is used
8+
9+
aggs = []
10+
for a in client.list_aggs(
11+
"AAPL",
12+
1,
13+
"hour",
14+
"2024-01-30",
15+
"2024-01-30",
16+
limit=50000,
17+
):
18+
aggs.append(a)
19+
20+
print(aggs)

examples/tools/docker/readme.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Dockerized Python Application with Polygon API Client
2+
3+
This Docker setup provides a ready-to-use environment for running Python scripts that utilize the `polygon-api-client` for financial data processing. It encapsulates the Python environment and the `polygon-api-client` library in a Docker container, making it easy to deploy and run the application consistently across any system with Docker installed. This approach is particularly useful for developers looking to integrate Polygon's financial data APIs into their applications without worrying about environment inconsistencies.
4+
5+
### Prerequisites
6+
7+
- [Docker](https://www.docker.com/) installed on your machine
8+
- [Polygon.io](https://polygon.io/) account and API key
9+
10+
### Setup and Configuration
11+
12+
1. Clone the repository or download the Dockerfile and your Python script into a directory.
13+
2. Use Docker's `--env` flag when running the container to set the `POLYGON_API_KEY` environment variable dynamically, or replace `<your_api_key>` in the Dockerfile with your Polygon API key (not recommended for production or shared environments).
14+
3. Ensure your Python script (e.g., `app.py`) is in the same directory as the Dockerfile.
15+
16+
### Building the Docker Image
17+
18+
Any modifications to the Python script will require rebuilding the Docker image to reflect the changes in the containerized environment. Use the docker build command each time your script is updated to ensure the latest version is used in your Docker container.
19+
20+
Navigate to the directory containing your Dockerfile and execute the following command to build your Docker image:
21+
22+
```
23+
docker build -t polygon-client-app .
24+
```
25+
26+
This command creates a Docker image named `polygon-client-app` based on the instructions in your Dockerfile.
27+
28+
### Running the Docker Container
29+
30+
Run your Docker container using the following command:
31+
32+
```
33+
docker run --env POLYGON_API_KEY="<your_api_key>" polygon-client-app
34+
```
35+
36+
Replace `<your_api_key>` with your actual Polygon API key. This command starts a Docker container based on the `polygon-client-app` image, sets the `POLYGON_API_KEY` environment variable to your provided API key, and runs your Python script inside the container.
37+
38+
### Additional Notes
39+
40+
- The Docker setup provided here is a very basic example. Depending on your specific requirements, you might need to customize the Dockerfile, such as adding volume mounts for persistent data or exposing ports for network communication.
41+
- For more details on using the Polygon API and the `polygon-api-client` library, please refer to the [Polygon documentation](https://polygon.io/docs), the [polygon-io/client-python](https://github.com/polygon-io/client-python) repo, or the [polygon-api-client documentation](https://polygon-api-client.readthedocs.io/en/latest/).

0 commit comments

Comments
 (0)