programatically creating draw.io macros with python. Is there a guideline ?

Vilius Kukanauskas
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 11, 2024

Hello,

I was looking for a solution but couldn’t find it anywhere, so I’m posting my question here. If there’s an existing answer, please feel free to share the link!

I need to programmatically create draw.io diagrams. I’m retrieving data from PlantUML and converting it to Draw.io XML format. I can open the converted files in Draw.io in the browser, and everything works well.

Now, I need to edit existing pages and insert the Draw.io macro in specific locations.

From my understanding, I likely need to upload the Draw.io file first and then create a macro that references it ?

I can't seem to find a well-documented workflow for automating this process.

I’m using Python. Any help would be greatly appreciated!

1 answer

1 accepted

2 votes
Answer accepted
Vilius Kukanauskas
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 11, 2024

nwm, i found the solution. For those that were in search.

first upload the file as atachement. i have base_url in my config dict.

def upload_file(page_id, file_path):
"""Uploads a file to Confluence as an attachment to the specified page."""
base_url = cfg["site_url"] + "/wiki"

# Define the endpoint for uploading an attachment to the page
url = f"{base_url}/rest/api/content/{page_id}/child/attachment"

# Set the file data for upload
with open(file_path, 'rb') as file_data:
files = {'file': (os.path.basename(file_path), file_data, 'application/xml')}
response = requests.post(url, headers={"X-Atlassian-Token": "no-check"}, auth=get_auth(), files=files)



then create an html snippet for draw.io and use the file name in it. (file name, not the atachement id, without path, only filename)

def generate_drawio_html(file_name):
"""Generates HTML to embed a Draw.io diagram from an attached file."""
drawio_html = f"""
<ac:structured-macro ac:name="drawio">
<ac:parameter ac:name="diagramName">{file_name}</ac:parameter>
<ac:parameter ac:name="pageSize">false</ac:parameter>
<ac:parameter ac:name="attachment">{file_name}</ac:parameter>
</ac:structured-macro>
"""
return drawio_html

 then insert  the snipet in your html and update the confluence page with something like

response = requests.put(url, headers=get_headers(use_admin_cookie), auth=get_auth(), json=body)

solved, archive it for future searches :) 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events