Skip to content

Commit 3054869

Browse files
committed
added graphviz server and client
1 parent 0275aa5 commit 3054869

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

containers/docker-compose.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ services:
55
dockerfile: graphviz.Dockerfile
66
volumes:
77
- ../cache:/data
8+
graphviz-server:
9+
build:
10+
context: ./dockerfiles/graphviz-server
11+
dockerfile: Dockerfile
12+
ports:
13+
- 8080:8080
814
apache:
915
image: httpd:latest
1016
volumes:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use an official Python runtime as a parent image
2+
FROM python:3.10-slim
3+
4+
# Set the working directory in the container
5+
WORKDIR /app
6+
7+
# Install graphviz
8+
RUN apt-get update && apt-get install -y graphviz && rm -rf /var/lib/apt/lists/*
9+
10+
# Copy the current directory contents into the container at /app
11+
COPY . /app
12+
13+
# Install any needed packages specified in requirements.txt
14+
RUN pip install --no-cache-dir -r requirements.txt
15+
16+
# Define environment variable
17+
ENV NAME World
18+
19+
# Run app.py when the container launches
20+
CMD ["python", "server.py"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Flask
2+
graphviz
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from flask import Flask, request, send_file
2+
import graphviz
3+
4+
app = Flask(__name__)
5+
6+
@app.route('/graph', methods=['POST'])
7+
def graph():
8+
# Get the dot format graph from the request
9+
dot_string = request.data.decode()
10+
11+
try:
12+
# Create a graph from the dot string
13+
dot = graphviz.Source(dot_string)
14+
# Render the graph to a file
15+
output_path = '/tmp/graph'
16+
dot.render(output_path, format='png', cleanup=True)
17+
18+
# Send the file back
19+
return send_file(output_path + '.png', mimetype='image/png')
20+
except Exception as e:
21+
return str(e), 400
22+
23+
if __name__ == '__main__':
24+
app.run(debug=True, host='0.0.0.0', port=8080)

containers/graphviz-client.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import requests
2+
3+
# URL of the graph endpoint
4+
url = 'http://localhost:8080/graph'
5+
6+
# DOT language description of the graph
7+
dot_text = """
8+
digraph G {
9+
A -> B;
10+
B -> C;
11+
C -> A;
12+
}
13+
"""
14+
15+
# Send the DOT string as a POST request to the server
16+
response = requests.post(url, data=dot_text)
17+
18+
# Check if the request was successful
19+
if response.status_code == 200:
20+
# Save the image
21+
with open('output_graph.png', 'wb') as f:
22+
f.write(response.content)
23+
print("Graph image saved as 'output_graph.png'.")
24+
else:
25+
print("Failed to generate graph:", response.text)

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pyyaml
22
networkx
3+
requests

0 commit comments

Comments
 (0)