Skip to content

Commit

Permalink
added graphviz server and client
Browse files Browse the repository at this point in the history
  • Loading branch information
wassfila committed Apr 21, 2024
1 parent 0275aa5 commit 3054869
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions containers/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ services:
dockerfile: graphviz.Dockerfile
volumes:
- ../cache:/data
graphviz-server:
build:
context: ./dockerfiles/graphviz-server
dockerfile: Dockerfile
ports:
- 8080:8080
apache:
image: httpd:latest
volumes:
Expand Down
20 changes: 20 additions & 0 deletions containers/dockerfiles/graphviz-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official Python runtime as a parent image
FROM python:3.10-slim

# Set the working directory in the container
WORKDIR /app

# Install graphviz
RUN apt-get update && apt-get install -y graphviz && rm -rf /var/lib/apt/lists/*

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "server.py"]
2 changes: 2 additions & 0 deletions containers/dockerfiles/graphviz-server/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Flask
graphviz
24 changes: 24 additions & 0 deletions containers/dockerfiles/graphviz-server/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from flask import Flask, request, send_file
import graphviz

app = Flask(__name__)

@app.route('/graph', methods=['POST'])
def graph():
# Get the dot format graph from the request
dot_string = request.data.decode()

try:
# Create a graph from the dot string
dot = graphviz.Source(dot_string)
# Render the graph to a file
output_path = '/tmp/graph'
dot.render(output_path, format='png', cleanup=True)

# Send the file back
return send_file(output_path + '.png', mimetype='image/png')
except Exception as e:
return str(e), 400

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)
25 changes: 25 additions & 0 deletions containers/graphviz-client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import requests

# URL of the graph endpoint
url = 'http://localhost:8080/graph'

# DOT language description of the graph
dot_text = """
digraph G {
A -> B;
B -> C;
C -> A;
}
"""

# Send the DOT string as a POST request to the server
response = requests.post(url, data=dot_text)

# Check if the request was successful
if response.status_code == 200:
# Save the image
with open('output_graph.png', 'wb') as f:
f.write(response.content)
print("Graph image saved as 'output_graph.png'.")
else:
print("Failed to generate graph:", response.text)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pyyaml
networkx
requests

0 comments on commit 3054869

Please sign in to comment.