Skip to content

Commit cef775b

Browse files
committed
update wikipage name and instructions
1 parent c6077e0 commit cef775b

File tree

1 file changed

+106
-66
lines changed
  • docs/tutorials/python/tutorial_scripts

1 file changed

+106
-66
lines changed

docs/tutorials/python/tutorial_scripts/wiki.py

Lines changed: 106 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -37,46 +37,47 @@
3737

3838
# Section1: Create, read, and update wiki pages
3939
# Create a new wiki page for the project with plain text markdown
40-
wiki_page_1 = WikiPage(
40+
root_wiki_page = WikiPage(
4141
owner_id=my_test_project.id,
4242
title="My Root Wiki Page",
4343
markdown="# Welcome to My Root Wiki\n\nThis is a sample root wiki page created with the Synapse client.",
4444
).store()
4545

46-
# OR you can create a wiki page with an existing markdown file
46+
# OR you can create a wiki page with an existing markdown file. More instructions can be found in section 2.
4747
markdown_file_path = "path/to/your_markdown_file.md"
48-
wiki_page_1 = WikiPage(
48+
root_wiki_page = WikiPage(
4949
owner_id=my_test_project.id,
5050
title="My First Root Wiki Page Version with existing markdown file",
5151
markdown=markdown_file_path,
5252
).store()
5353

5454
# Create a new wiki page with updated content
55-
wiki_page_2 = WikiPage(
55+
root_wiki_page_new = WikiPage(
5656
owner_id=my_test_project.id,
57-
title="My First Root Wiki Page Version 1",
58-
markdown="# Welcome to My Root Wiki Version 1\n\nThis is a sample root wiki page created with the Synapse client.",
59-
id=wiki_page_1.id,
57+
title="My First Root Wiki Page NEW",
58+
markdown="# Welcome to My Root Wiki NEW\n\nThis is a sample root wiki page created with the Synapse client.",
59+
id=root_wiki_page.id,
6060
).store()
6161

6262
# Restore the wiki page to the original version
6363
wiki_page_restored = WikiPage(
64-
owner_id=my_test_project.id, id=wiki_page_1.id, wiki_version="0"
64+
owner_id=my_test_project.id, id=root_wiki_page.id, wiki_version="0"
6565
).restore()
6666

6767
# check if the content is restored
6868
comparisons = [
69-
wiki_page_1.markdown_file_handle_id == wiki_page_restored.markdown_file_handle_id,
70-
wiki_page_1.id == wiki_page_restored.id,
71-
wiki_page_1.title == wiki_page_restored.title,
69+
root_wiki_page.markdown_file_handle_id
70+
== wiki_page_restored.markdown_file_handle_id,
71+
root_wiki_page.id == wiki_page_restored.id,
72+
root_wiki_page.title == wiki_page_restored.title,
7273
]
7374
print(f"All fields match: {all(comparisons)}")
7475

7576
# Create a sub-wiki page
76-
sub_wiki = WikiPage(
77+
sub_wiki_1 = WikiPage(
7778
owner_id=my_test_project.id,
7879
title="Sub Wiki Page 1",
79-
parent_id=wiki_page_1.id, # Use the ID of the parent wiki page we created '633033'
80+
parent_id=root_wiki_page.id, # Use the ID of the parent wiki page we created '633033'
8081
markdown="# Sub Page 1\n\nThis is a sub-page of another wiki.",
8182
).store()
8283

@@ -85,18 +86,18 @@
8586
print(wiki_header_tree)
8687

8788
# Once you know the wiki page id, you can retrieve the wiki page with the id
88-
retrieved_wiki = WikiPage(owner_id=my_test_project.id, id=sub_wiki.id).get()
89+
retrieved_wiki = WikiPage(owner_id=my_test_project.id, id=sub_wiki_1.id).get()
8990
print(f"Retrieved wiki page with title: {retrieved_wiki.title}")
9091

9192
# Or you can retrieve the wiki page with the title
92-
retrieved_wiki = WikiPage(owner_id=my_test_project.id, title=wiki_page_1.title).get()
93+
retrieved_wiki = WikiPage(owner_id=my_test_project.id, title=sub_wiki_1.title).get()
9394
print(f"Retrieved wiki page with title: {retrieved_wiki.title}")
9495

9596
# Check if the retrieved wiki page is the same as the original wiki page
9697
comparisons = [
97-
wiki_page_1.markdown_file_handle_id == retrieved_wiki.markdown_file_handle_id,
98-
wiki_page_1.id == retrieved_wiki.id,
99-
wiki_page_1.title == retrieved_wiki.title,
98+
sub_wiki_1.markdown_file_handle_id == retrieved_wiki.markdown_file_handle_id,
99+
sub_wiki_1.id == retrieved_wiki.id,
100+
sub_wiki_1.title == retrieved_wiki.title,
100101
]
101102
print(f"All fields match: {all(comparisons)}")
102103

@@ -120,9 +121,9 @@ def hello_world():
120121
"""
121122

122123
# Create wiki page from markdown text
123-
markdown_wiki_1 = WikiPage(
124+
sub_wiki_2 = WikiPage(
124125
owner_id=my_test_project.id,
125-
parent_id=wiki_page_1.id,
126+
parent_id=root_wiki_page.id,
126127
title="Sub Page 2 created from markdown text",
127128
markdown=markdown_content,
128129
).store()
@@ -134,111 +135,150 @@ def hello_world():
134135
gz.write("This is a markdown file")
135136

136137
# Create wiki page from markdown file
137-
markdown_wiki_2 = WikiPage(
138+
sub_wiki_3 = WikiPage(
138139
owner_id=my_test_project.id,
139-
parent_id=wiki_page_1.id,
140+
parent_id=root_wiki_page.id,
140141
title="Sub Page 3 created from markdown file",
141142
markdown=markdown_file_path,
142143
).store()
143144

144145
# Download the markdown file
145-
# delete the markdown file after downloading
146+
# delete the markdown file after uploading to test the download function
147+
os.remove(markdown_file_path)
148+
# Note: If the markdown is generated from plain text using the client, the downloaded file will be named wiki_markdown_<wiki_page_title>.md.gz. If it is generated from an existing markdown file, the downloaded file will retain the original filename with the .gz suffix appended.
149+
# Download the markdown file for sub_wiki_2 that is created from markdown text
150+
wiki_page_markdown_2 = WikiPage(
151+
owner_id=my_test_project.id, id=sub_wiki_2.id
152+
).get_markdown(
153+
download_file=True,
154+
download_location=".",
155+
download_file_name=f"wiki_markdown_{sub_wiki_2.title}.md.gz",
156+
)
157+
print(
158+
f"Wiki page markdown for sub_wiki_2 successfully downloaded: {os.path.exists(f'wiki_markdown_{sub_wiki_2.title}.md.gz')}"
159+
)
160+
# clean up the downloaded markdown file
161+
os.remove(f"wiki_markdown_{sub_wiki_2.title}.md.gz")
162+
163+
# Download the markdown file for sub_wiki_3 that is created from a markdown file
164+
wiki_page_markdown_3 = WikiPage(
165+
owner_id=my_test_project.id, id=sub_wiki_3.id
166+
).get_markdown(
167+
download_file=True, download_location=".", download_file_name=markdown_file_path
168+
)
169+
print(
170+
f"Wiki page markdown for sub_wiki_3 successfully downloaded: {os.path.exists(markdown_file_path)}"
171+
)
172+
# clean up the downloaded markdown file
146173
os.remove(markdown_file_path)
147-
markdown_file_2 = WikiPage(
148-
owner_id=my_test_project.id, id=markdown_wiki_2.id
149-
).get_markdown(download_file=True, download_location=".")
150-
151-
print(f"Markdown file downloaded to: {markdown_file_2}")
152174

153175
# Section 3: WikiPage with Attachments
154176
# Create a temporary file for the attachment
155177
attachment_file_name = "temp_attachment.txt.gz"
156178
with gzip.open(attachment_file_name, "wt", encoding="utf-8") as gz:
157179
gz.write("This is a sample attachment.")
158180

159-
# reformat the attachment file name to be a valid attachment path
181+
# reformat '.' and '_' in the attachment file name to be a valid attachment path
160182
attachment_file_name_reformatted = attachment_file_name.replace(".", "%2E")
161183
attachment_file_name_reformatted = attachment_file_name_reformatted.replace("_", "%5F")
162184

163-
wiki_with_attachments = WikiPage(
185+
sub_wiki_4 = WikiPage(
164186
owner_id=my_test_project.id,
165-
parent_id=wiki_page_1.id,
187+
parent_id=root_wiki_page.id,
166188
title="Sub Page 4 with Attachments",
167189
markdown=f"# Sub Page 4 with Attachments\n\nThis is a attachment: ${{previewattachment?fileName={attachment_file_name_reformatted}}}",
168190
attachments=[attachment_file_name],
169191
).store()
170192

171193
# Get attachment handles
172194
attachment_handles = WikiPage(
173-
owner_id=my_test_project.id, id=wiki_with_attachments.id
195+
owner_id=my_test_project.id, id=sub_wiki_4.id
174196
).get_attachment_handles()
175-
print(f"Found {len(attachment_handles['list'])} attachments")
197+
print(f"Attachment handles: {attachment_handles['list']}")
198+
199+
# Get attachment URL without downloading
200+
wiki_page_attachment_url = WikiPage(
201+
owner_id=my_test_project.id, id=sub_wiki_4.id
202+
).get_attachment(
203+
file_name="temp_attachment.txt.gz",
204+
download_file=False,
205+
)
206+
print(f"Attachment URL: {wiki_page_attachment_url}")
176207

177-
# Delete the attachment file after uploading --> check if the file is deleted
178-
os.remove(attachment_file_name)
179208
# Download an attachment
180-
wiki_page = WikiPage(
181-
owner_id=my_test_project.id, id=wiki_with_attachments.id
209+
# Delete the attachment file after uploading to test the download function
210+
os.remove(attachment_file_name)
211+
wiki_page_attachment = WikiPage(
212+
owner_id=my_test_project.id, id=sub_wiki_4.id
182213
).get_attachment(
183214
file_name=attachment_file_name,
184215
download_file=True,
185216
download_location=".",
186217
)
187218
print(f"Attachment downloaded: {os.path.exists(attachment_file_name)}")
219+
os.remove(attachment_file_name)
188220

189-
# Get attachment URL without downloading
190-
wiki_page_url = WikiPage(
191-
owner_id=my_test_project.id, id=wiki_with_attachments.id
192-
).get_attachment(
221+
# Download an attachment preview. Instead of using the file_name from the attachmenthandle response when isPreview=True, you should use the original file name in the get_attachment_preview request. The downloaded file will still be named according to the file_name provided in the response when isPreview=True.
222+
# Get attachment preview URL without downloading
223+
attachment_preview_url = WikiPage(
224+
owner_id=my_test_project.id, id=sub_wiki_4.id
225+
).get_attachment_preview(
193226
file_name="temp_attachment.txt.gz",
194227
download_file=False,
195228
)
196-
print(f"Attachment URL: {wiki_page_url}")
229+
print(f"Attachment preview URL: {attachment_preview_url}")
197230

198-
# Download an attachment preview--? Failed to download the attachment preview, synapseclient.core.exceptions.SynapseHTTPError: 404 Client Error: Cannot find a wiki attachment for OwnerID: syn68493645, ObjectType: ENTITY, WikiPageId: 633100, fileName: preview.txt
199-
attachment_handles = WikiPage(
200-
owner_id=my_test_project.id, id=wiki_with_attachments.id
201-
).get_attachment_handles()
202-
print(f"Attachment handles: {attachment_handles}")
203-
wiki_page = WikiPage(
204-
owner_id=my_test_project.id, id=wiki_with_attachments.id
231+
# Download an attachment preview
232+
attachment_preview = WikiPage(
233+
owner_id=my_test_project.id, id=sub_wiki_4.id
205234
).get_attachment_preview(
206-
file_name="preview.txt",
235+
file_name="temp_attachment.txt.gz",
207236
download_file=True,
208237
download_location=".",
209238
)
239+
# From the attachment preview URL or attachment handle response, the downloaded preview file is preview.txt
240+
os.remove("preview.txt")
210241

211242
# Section 4: WikiHeader - Working with Wiki Hierarchy
212-
213243
# Get wiki header tree (hierarchy)
214-
# Note: Uncomment to actually get the header tree
215244
headers = WikiHeader.get(owner_id=my_test_project.id)
216245
print(f"Found {len(headers)} wiki pages in hierarchy")
246+
print(f"Wiki header tree: {headers}")
217247

218248
# Section 5. WikiHistorySnapshot - Version History
219-
# Get wiki history
220-
history = WikiHistorySnapshot.get(owner_id=my_test_project.id, id=wiki_page_1.id)
221-
222-
print(f"Found {len(history)} versions in history for {wiki_page_1.title}")
249+
# Get wiki history for root_wiki_page
250+
history = WikiHistorySnapshot.get(owner_id=my_test_project.id, id=root_wiki_page.id)
251+
print(f"History: {history}")
223252

224253
# Section 6. WikiOrderHint - Ordering Wiki Pages
225-
# Get wiki order hint --> failed to get the order hint
254+
# Set the wiki order hint
226255
order_hint = WikiOrderHint(owner_id=my_test_project.id).get()
256+
print(f"Order hint for {my_test_project.id}: {order_hint.id_list}")
257+
# As you can see from the printed message, the order hint is not set by default, so you need to set it explicitly at the beginning.
258+
order_hint.id_list = [
259+
root_wiki_page.id,
260+
sub_wiki_3.id,
261+
sub_wiki_4.id,
262+
sub_wiki_1.id,
263+
sub_wiki_2.id,
264+
]
265+
order_hint.store()
227266
print(f"Order hint for {my_test_project.id}: {order_hint}")
228267

229268
# Update wiki order hint
230-
order_hint.id_list = [wiki_page_1.id]
231-
232-
print(f"Created order hint for {len(order_hint.id_list)} wiki pages")
233-
234-
# Update order hint
235-
order_hint.id_list = ["633084", "633085", "633086", "633087", "633088"] # Reorder
269+
order_hint = WikiOrderHint(owner_id=my_test_project.id).get()
270+
order_hint.id_list = [
271+
root_wiki_page.id,
272+
sub_wiki_1.id,
273+
sub_wiki_2.id,
274+
sub_wiki_3.id,
275+
sub_wiki_4.id,
276+
]
236277
order_hint.store()
278+
print(f"Order hint for {my_test_project.id}: {order_hint}")
237279

238280
# Delete a wiki page
239-
wiki_page_to_delete = WikiPage(
240-
owner_id=my_test_project.id, id=wiki_with_attachments.id
241-
).delete()
281+
wiki_page_to_delete = WikiPage(owner_id=my_test_project.id, id=sub_wiki_3.id).delete()
242282

243283
# clean up
244284
my_test_project.delete()

0 commit comments

Comments
 (0)