Skip to content

Commit b0e0834

Browse files
docs: Update tutorials for DataJoint 2.0 API and add how-to guide
- Update dj-top.ipynb and json.ipynb for 2.0 API changes: - Replace fetch("KEY") with keys() - Replace fetch(as_dict=True) with to_dicts() - Replace fetch(column) with to_arrays(column) - Add schema cleanup at start of tutorials for re-runnability - Add datajoint.json and .secrets/ to .gitignore for local config - Add deferred schema activation how-to guide - Update mkdocs.yaml with How-To Guides section Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 7ca9b11 commit b0e0834

File tree

5 files changed

+175
-202
lines changed

5 files changed

+175
-202
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ cython_debug/
183183

184184
# Datajoint related files
185185
dj_local_conf.json
186+
datajoint.json
187+
.secrets/
186188
*.env
187189
!.vscode/launch.json
188190
# pixi environments

docs/mkdocs.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ repo_name: datajoint/datajoint-python
77
nav:
88
- Home: index.md
99
- Contributing: develop.md
10+
- How-To Guides:
11+
- Deferred Schema Activation: how-to/deferred-schema-activation.md
1012
- Architecture:
1113
- architecture/index.md
1214
- SQL Transpilation: architecture/transpilation.md

docs/src/archive/tutorials/dj-top.ipynb

Lines changed: 10 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,10 @@
1818
},
1919
{
2020
"cell_type": "code",
21-
"execution_count": 1,
21+
"execution_count": null,
2222
"metadata": {},
23-
"outputs": [
24-
{
25-
"name": "stderr",
26-
"output_type": "stream",
27-
"text": [
28-
"[2024-12-20 11:10:20,120][INFO]: Connecting [email protected]:3306\n",
29-
"[2024-12-20 11:10:20,259][INFO]: Connected [email protected]:3306\n"
30-
]
31-
}
32-
],
33-
"source": [
34-
"import datajoint as dj\n",
35-
"\n",
36-
"dj.config[\"database.host\"] = \"127.0.0.1\"\n",
37-
"schema = dj.Schema(\"university\")"
38-
]
23+
"outputs": [],
24+
"source": "import datajoint as dj\n\n# Clean up any existing schema from previous runs\nschema = dj.Schema(\"tutorial_university\", create_tables=False)\nschema.drop()\n\n# Create fresh schema\nschema = dj.Schema(\"tutorial_university\")"
3925
},
4026
{
4127
"cell_type": "markdown",
@@ -236,144 +222,17 @@
236222
},
237223
{
238224
"cell_type": "code",
239-
"execution_count": 8,
225+
"execution_count": null,
240226
"metadata": {},
241227
"outputs": [],
242-
"source": [
243-
"Student.insert(dict(k, student_id=i) for i, k in zip(range(100, 300), yield_students()))\n",
244-
"\n",
245-
"Department.insert(\n",
246-
" dict(\n",
247-
" dept=dept,\n",
248-
" dept_name=name,\n",
249-
" dept_address=fake.address(),\n",
250-
" dept_phone=fake.phone_number()[:20],\n",
251-
" )\n",
252-
" for dept, name in [\n",
253-
" [\"CS\", \"Computer Science\"],\n",
254-
" [\"BIOL\", \"Life Sciences\"],\n",
255-
" [\"PHYS\", \"Physics\"],\n",
256-
" [\"MATH\", \"Mathematics\"],\n",
257-
" ]\n",
258-
")\n",
259-
"\n",
260-
"StudentMajor.insert(\n",
261-
" {**s, **d, \"declare_date\": fake.date_between(start_date=datetime.date(1999, 1, 1))}\n",
262-
" for s, d in zip(Student.fetch(\"KEY\"), random.choices(Department.fetch(\"KEY\"), k=len(Student())))\n",
263-
" if random.random() < 0.75\n",
264-
")\n",
265-
"\n",
266-
"# from https://www.utah.edu/\n",
267-
"Course.insert(\n",
268-
" [\n",
269-
" [\"BIOL\", 1006, \"World of Dinosaurs\", 3],\n",
270-
" [\"BIOL\", 1010, \"Biology in the 21st Century\", 3],\n",
271-
" [\"BIOL\", 1030, \"Human Biology\", 3],\n",
272-
" [\"BIOL\", 1210, \"Principles of Biology\", 4],\n",
273-
" [\"BIOL\", 2010, \"Evolution & Diversity of Life\", 3],\n",
274-
" [\"BIOL\", 2020, \"Principles of Cell Biology\", 3],\n",
275-
" [\"BIOL\", 2021, \"Principles of Cell Science\", 4],\n",
276-
" [\"BIOL\", 2030, \"Principles of Genetics\", 3],\n",
277-
" [\"BIOL\", 2210, \"Human Genetics\", 3],\n",
278-
" [\"BIOL\", 2325, \"Human Anatomy\", 4],\n",
279-
" [\"BIOL\", 2330, \"Plants & Society\", 3],\n",
280-
" [\"BIOL\", 2355, \"Field Botany\", 2],\n",
281-
" [\"BIOL\", 2420, \"Human Physiology\", 4],\n",
282-
" [\"PHYS\", 2040, \"Classcal Theoretical Physics II\", 4],\n",
283-
" [\"PHYS\", 2060, \"Quantum Mechanics\", 3],\n",
284-
" [\"PHYS\", 2100, \"General Relativity and Cosmology\", 3],\n",
285-
" [\"PHYS\", 2140, \"Statistical Mechanics\", 4],\n",
286-
" [\"PHYS\", 2210, \"Physics for Scientists and Engineers I\", 4],\n",
287-
" [\"PHYS\", 2220, \"Physics for Scientists and Engineers II\", 4],\n",
288-
" [\"PHYS\", 3210, \"Physics for Scientists I (Honors)\", 4],\n",
289-
" [\"PHYS\", 3220, \"Physics for Scientists II (Honors)\", 4],\n",
290-
" [\"MATH\", 1250, \"Calculus for AP Students I\", 4],\n",
291-
" [\"MATH\", 1260, \"Calculus for AP Students II\", 4],\n",
292-
" [\"MATH\", 1210, \"Calculus I\", 4],\n",
293-
" [\"MATH\", 1220, \"Calculus II\", 4],\n",
294-
" [\"MATH\", 2210, \"Calculus III\", 3],\n",
295-
" [\"MATH\", 2270, \"Linear Algebra\", 4],\n",
296-
" [\"MATH\", 2280, \"Introduction to Differential Equations\", 4],\n",
297-
" [\"MATH\", 3210, \"Foundations of Analysis I\", 4],\n",
298-
" [\"MATH\", 3220, \"Foundations of Analysis II\", 4],\n",
299-
" [\"CS\", 1030, \"Foundations of Computer Science\", 3],\n",
300-
" [\"CS\", 1410, \"Introduction to Object-Oriented Programming\", 4],\n",
301-
" [\"CS\", 2420, \"Introduction to Algorithms & Data Structures\", 4],\n",
302-
" [\"CS\", 2100, \"Discrete Structures\", 3],\n",
303-
" [\"CS\", 3500, \"Software Practice\", 4],\n",
304-
" [\"CS\", 3505, \"Software Practice II\", 3],\n",
305-
" [\"CS\", 3810, \"Computer Organization\", 4],\n",
306-
" [\"CS\", 4400, \"Computer Systems\", 4],\n",
307-
" [\"CS\", 4150, \"Algorithms\", 3],\n",
308-
" [\"CS\", 3100, \"Models of Computation\", 3],\n",
309-
" [\"CS\", 3200, \"Introduction to Scientific Computing\", 3],\n",
310-
" [\"CS\", 4000, \"Senior Capstone Project - Design Phase\", 3],\n",
311-
" [\"CS\", 4500, \"Senior Capstone Project\", 3],\n",
312-
" [\"CS\", 4940, \"Undergraduate Research\", 3],\n",
313-
" [\"CS\", 4970, \"Computer Science Bachelors Thesis\", 3],\n",
314-
" ]\n",
315-
")\n",
316-
"\n",
317-
"Term.insert(dict(term_year=year, term=term) for year in range(1999, 2019) for term in [\"Spring\", \"Summer\", \"Fall\"])\n",
318-
"\n",
319-
"Term().fetch(order_by=(\"term_year DESC\", \"term DESC\"), as_dict=True, limit=1)[0]\n",
320-
"\n",
321-
"CurrentTerm().insert1({**Term().fetch(order_by=(\"term_year DESC\", \"term DESC\"), as_dict=True, limit=1)[0]})\n",
322-
"\n",
323-
"\n",
324-
"def make_section(prob):\n",
325-
" for c in (Course * Term).proj():\n",
326-
" for sec in \"abcd\":\n",
327-
" if random.random() < prob:\n",
328-
" break\n",
329-
" yield {\n",
330-
" **c,\n",
331-
" \"section\": sec,\n",
332-
" \"auditorium\": random.choice(\"ABCDEF\") + str(random.randint(1, 100)),\n",
333-
" }\n",
334-
"\n",
335-
"\n",
336-
"Section.insert(make_section(0.5))"
337-
]
228+
"source": "Student.insert(dict(k, student_id=i) for i, k in zip(range(100, 300), yield_students()))\n\nDepartment.insert(\n dict(\n dept=dept,\n dept_name=name,\n dept_address=fake.address(),\n dept_phone=fake.phone_number()[:20],\n )\n for dept, name in [\n [\"CS\", \"Computer Science\"],\n [\"BIOL\", \"Life Sciences\"],\n [\"PHYS\", \"Physics\"],\n [\"MATH\", \"Mathematics\"],\n ]\n)\n\nStudentMajor.insert(\n {**s, **d, \"declare_date\": fake.date_between(start_date=datetime.date(1999, 1, 1))}\n for s, d in zip(Student().keys(), random.choices(Department().keys(), k=len(Student())))\n if random.random() < 0.75\n)\n\n# from https://www.utah.edu/\nCourse.insert(\n [\n [\"BIOL\", 1006, \"World of Dinosaurs\", 3],\n [\"BIOL\", 1010, \"Biology in the 21st Century\", 3],\n [\"BIOL\", 1030, \"Human Biology\", 3],\n [\"BIOL\", 1210, \"Principles of Biology\", 4],\n [\"BIOL\", 2010, \"Evolution & Diversity of Life\", 3],\n [\"BIOL\", 2020, \"Principles of Cell Biology\", 3],\n [\"BIOL\", 2021, \"Principles of Cell Science\", 4],\n [\"BIOL\", 2030, \"Principles of Genetics\", 3],\n [\"BIOL\", 2210, \"Human Genetics\", 3],\n [\"BIOL\", 2325, \"Human Anatomy\", 4],\n [\"BIOL\", 2330, \"Plants & Society\", 3],\n [\"BIOL\", 2355, \"Field Botany\", 2],\n [\"BIOL\", 2420, \"Human Physiology\", 4],\n [\"PHYS\", 2040, \"Classcal Theoretical Physics II\", 4],\n [\"PHYS\", 2060, \"Quantum Mechanics\", 3],\n [\"PHYS\", 2100, \"General Relativity and Cosmology\", 3],\n [\"PHYS\", 2140, \"Statistical Mechanics\", 4],\n [\"PHYS\", 2210, \"Physics for Scientists and Engineers I\", 4],\n [\"PHYS\", 2220, \"Physics for Scientists and Engineers II\", 4],\n [\"PHYS\", 3210, \"Physics for Scientists I (Honors)\", 4],\n [\"PHYS\", 3220, \"Physics for Scientists II (Honors)\", 4],\n [\"MATH\", 1250, \"Calculus for AP Students I\", 4],\n [\"MATH\", 1260, \"Calculus for AP Students II\", 4],\n [\"MATH\", 1210, \"Calculus I\", 4],\n [\"MATH\", 1220, \"Calculus II\", 4],\n [\"MATH\", 2210, \"Calculus III\", 3],\n [\"MATH\", 2270, \"Linear Algebra\", 4],\n [\"MATH\", 2280, \"Introduction to Differential Equations\", 4],\n [\"MATH\", 3210, \"Foundations of Analysis I\", 4],\n [\"MATH\", 3220, \"Foundations of Analysis II\", 4],\n [\"CS\", 1030, \"Foundations of Computer Science\", 3],\n [\"CS\", 1410, \"Introduction to Object-Oriented Programming\", 4],\n [\"CS\", 2420, \"Introduction to Algorithms & Data Structures\", 4],\n [\"CS\", 2100, \"Discrete Structures\", 3],\n [\"CS\", 3500, \"Software Practice\", 4],\n [\"CS\", 3505, \"Software Practice II\", 3],\n [\"CS\", 3810, \"Computer Organization\", 4],\n [\"CS\", 4400, \"Computer Systems\", 4],\n [\"CS\", 4150, \"Algorithms\", 3],\n [\"CS\", 3100, \"Models of Computation\", 3],\n [\"CS\", 3200, \"Introduction to Scientific Computing\", 3],\n [\"CS\", 4000, \"Senior Capstone Project - Design Phase\", 3],\n [\"CS\", 4500, \"Senior Capstone Project\", 3],\n [\"CS\", 4940, \"Undergraduate Research\", 3],\n [\"CS\", 4970, \"Computer Science Bachelors Thesis\", 3],\n ]\n)\n\nTerm.insert(dict(term_year=year, term=term) for year in range(1999, 2019) for term in [\"Spring\", \"Summer\", \"Fall\"])\n\nTerm().to_dicts(order_by=(\"term_year DESC\", \"term DESC\"), limit=1)[0]\n\nCurrentTerm().insert1({**Term().to_dicts(order_by=(\"term_year DESC\", \"term DESC\"), limit=1)[0]})\n\n\ndef make_section(prob):\n for c in (Course * Term).proj():\n for sec in \"abcd\":\n if random.random() < prob:\n break\n yield {\n **c,\n \"section\": sec,\n \"auditorium\": random.choice(\"ABCDEF\") + str(random.randint(1, 100)),\n }\n\n\nSection.insert(make_section(0.5))"
338229
},
339230
{
340231
"cell_type": "code",
341-
"execution_count": 9,
232+
"execution_count": null,
342233
"metadata": {},
343-
"outputs": [
344-
{
345-
"name": "stderr",
346-
"output_type": "stream",
347-
"text": [
348-
"100%|██████████| 200/200 [00:27<00:00, 7.17it/s]\n"
349-
]
350-
}
351-
],
352-
"source": [
353-
"# Enrollment\n",
354-
"terms = Term().fetch(\"KEY\")\n",
355-
"quit_prob = 0.1\n",
356-
"for student in tqdm(Student.fetch(\"KEY\")):\n",
357-
" start_term = random.randrange(len(terms))\n",
358-
" for term in terms[start_term:]:\n",
359-
" if random.random() < quit_prob:\n",
360-
" break\n",
361-
" else:\n",
362-
" sections = ((Section & term) - (Course & (Enroll & student))).fetch(\"KEY\")\n",
363-
" if sections:\n",
364-
" Enroll.insert(\n",
365-
" {**student, **section} for section in random.sample(sections, random.randrange(min(5, len(sections))))\n",
366-
" )\n",
367-
"\n",
368-
"# assign random grades\n",
369-
"grades = LetterGrade.fetch(\"grade\")\n",
370-
"\n",
371-
"grade_keys = Enroll.fetch(\"KEY\")\n",
372-
"random.shuffle(grade_keys)\n",
373-
"grade_keys = grade_keys[: len(grade_keys) * 9 // 10]\n",
374-
"\n",
375-
"Grade.insert({**key, \"grade\": grade} for key, grade in zip(grade_keys, random.choices(grades, k=len(grade_keys))))"
376-
]
234+
"outputs": [],
235+
"source": "# Enrollment\nterms = Term().keys()\nquit_prob = 0.1\nfor student in tqdm(Student().keys()):\n start_term = random.randrange(len(terms))\n for term in terms[start_term:]:\n if random.random() < quit_prob:\n break\n else:\n sections = ((Section & term) - (Course & (Enroll & student))).keys()\n if sections:\n Enroll.insert(\n {**student, **section} for section in random.sample(sections, random.randrange(min(5, len(sections))))\n )\n\n# assign random grades\ngrades = LetterGrade().to_arrays(\"grade\")[0]\n\ngrade_keys = Enroll().keys()\nrandom.shuffle(grade_keys)\ngrade_keys = grade_keys[: len(grade_keys) * 9 // 10]\n\nGrade.insert({**key, \"grade\": grade} for key, grade in zip(grade_keys, random.choices(grades, k=len(grade_keys))))"
377236
},
378237
{
379238
"cell_type": "markdown",
@@ -988,7 +847,7 @@
988847
"execution_count": null,
989848
"metadata": {},
990849
"outputs": [],
991-
"source": []
850+
"source": "# Cleanup\nschema.drop()"
992851
}
993852
],
994853
"metadata": {
@@ -1012,4 +871,4 @@
1012871
},
1013872
"nbformat": 4,
1014873
"nbformat_minor": 2
1015-
}
874+
}

docs/src/archive/tutorials/json.ipynb

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,11 @@
6666
},
6767
{
6868
"cell_type": "code",
69-
"execution_count": 2,
69+
"execution_count": null,
7070
"id": "dc318298-b819-4f06-abbd-7bb7544dd431",
7171
"metadata": {},
72-
"outputs": [
73-
{
74-
"name": "stderr",
75-
"output_type": "stream",
76-
"text": [
77-
"[2023-02-12 00:14:33,027][INFO]: Connecting [email protected]:3306\n",
78-
"[2023-02-12 00:14:33,039][INFO]: Connected [email protected]:3306\n"
79-
]
80-
}
81-
],
82-
"source": [
83-
"schema = dj.Schema(f\"{dj.config['database.user']}_json\")"
84-
]
72+
"outputs": [],
73+
"source": "# Clean up any existing schema from previous runs\nschema_name = f\"{dj.config['database.user']}_json\"\nschema = dj.Schema(schema_name, create_tables=False)\nschema.drop()\n\n# Create fresh schema\nschema = dj.Schema(schema_name)"
8574
},
8675
{
8776
"cell_type": "code",
@@ -828,26 +817,11 @@
828817
},
829818
{
830819
"cell_type": "code",
831-
"execution_count": 12,
820+
"execution_count": null,
832821
"id": "bb5f0448",
833822
"metadata": {},
834-
"outputs": [
835-
{
836-
"data": {
837-
"text/plain": [
838-
"[{'name': 'business', 'car_name': 'Chaching', 'car_length': '100'},\n",
839-
" {'name': 'engineering', 'car_name': 'Rever', 'car_length': '20.5'},\n",
840-
" {'name': 'marketing', 'car_name': None, 'car_length': None}]"
841-
]
842-
},
843-
"execution_count": 12,
844-
"metadata": {},
845-
"output_type": "execute_result"
846-
}
847-
],
848-
"source": [
849-
"q_untyped.fetch(as_dict=True)"
850-
]
823+
"outputs": [],
824+
"source": "q_untyped.to_dicts()"
851825
},
852826
{
853827
"cell_type": "code",
@@ -958,26 +932,11 @@
958932
},
959933
{
960934
"cell_type": "code",
961-
"execution_count": 14,
935+
"execution_count": null,
962936
"id": "8a93dbf9",
963937
"metadata": {},
964-
"outputs": [
965-
{
966-
"data": {
967-
"text/plain": [
968-
"[{'name': 'business', 'car_name': 'Chaching', 'car_length': 100.0},\n",
969-
" {'name': 'engineering', 'car_name': 'Rever', 'car_length': 20.5},\n",
970-
" {'name': 'marketing', 'car_name': None, 'car_length': None}]"
971-
]
972-
},
973-
"execution_count": 14,
974-
"metadata": {},
975-
"output_type": "execute_result"
976-
}
977-
],
978-
"source": [
979-
"q_typed.fetch(as_dict=True)"
980-
]
938+
"outputs": [],
939+
"source": "q_typed.to_dicts()"
981940
},
982941
{
983942
"cell_type": "markdown",
@@ -1077,4 +1036,4 @@
10771036
},
10781037
"nbformat": 4,
10791038
"nbformat_minor": 5
1080-
}
1039+
}

0 commit comments

Comments
 (0)