import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(%0A%20%20%20%20width%3D%22medium%22%2C%0A%20%20%20%20app_title%3D%22Share%3A%20a%20data%20portal%20with%20Datasette%22%2C%0A)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20settings%20every%20other%20cell%20uses.%0A%20%20%20%20import%20json%0A%20%20%20%20import%20sqlite3%0A%20%20%20%20import%20urllib.error%0A%20%20%20%20import%20urllib.request%0A%20%20%20%20from%20contextlib%20import%20closing%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20file%20locations%20and%20exercise%20checks%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20PATHS%2C%20attempt%2C%20check_cases%2C%20waiting_for%0A%0A%20%20%20%20ROUND_ID%20%3D%20%222024Q2%22%20%20%23%20the%20release%20to%20publish%0A%20%20%20%20PORT%20%3D%208001%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Share%3A%20a%20data%20portal%20with%20Datasette%0A%0A%20%20%20%20**Where%20you%20are%3A**%20the%20release%20is%20built%20and%20checked%20(%6031_share_round.py%60).%20Now%20you%20put%20it%20where%20people%20can%20use%20it%20without%20Python%3A%20a%20**data%20portal**%2C%20a%20website%20where%20anyone%20can%20browse%20the%20table%2C%20filter%20it%2C%20run%20the%20questions%20you%20prepared%2C%20and%20download%20what%20they%20see.%0A%0A%20%20%20%20You%20will%3A%0A%0A%20%20%20%201.%20describe%20the%20release%2C%20so%20the%20portal%20explains%20itself%3B%0A%20%20%20%202.%20write%20a%20**canned%20query**%3A%20a%20question%20you%20prepare%20once%2C%20that%20any%20visitor%20can%20ask%20with%20their%20own%20value%3B%0A%20%20%20%203.%20start%20**Datasette**%20in%20a%20second%20terminal%20and%20use%20the%20portal%20as%20a%20visitor%20would%3B%0A%20%20%20%204.%20read%20the%20portal%20back%20from%20Python%2C%20as%20another%20program%20would.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20What%20Datasette%20is%0A%0A%20%20%20%20**Datasette**%20turns%20a%20SQLite%20file%20into%20a%20website%20and%20a%20JSON%20API%20(a%20web%20address%20a%20program%20can%20read).%20Nothing%20is%20copied%20or%20converted%3A%20it%20reads%20%60release.sqlite%60%20as%20it%20is%2C%20and%20two%20small%20files%20tell%20it%20what%20to%20say%20about%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.mermaid(%22%22%22%0A%20%20%20%20flowchart%20LR%0A%20%20%20%20%20%20%20%20A%5B%22release.sqlite%3Cbr%2F%3Ethe%20data%22%5D%20--%3E%20D((%22Datasette%22))%0A%20%20%20%20%20%20%20%20B%5B%22metadata.json%3Cbr%2F%3Ewhat%20it%20means%22%5D%20--%3E%20D%0A%20%20%20%20%20%20%20%20C%5B%22datasette.json%3Cbr%2F%3Ehow%20to%20show%20it%2C%20prepared%20questions%22%5D%20--%3E%20D%0A%20%20%20%20%20%20%20%20D%20--%3E%20E%5B%22pages%20to%20browse%2C%20filter%2C%20facet%22%5D%0A%20%20%20%20%20%20%20%20D%20--%3E%20F%5B%22canned%20queries%3Cbr%2F%3Ea%20form%20for%20each%20question%22%5D%0A%20%20%20%20%20%20%20%20D%20--%3E%20G%5B%22JSON%20for%20programs%3Cbr%2F%3Eadd%20.json%20to%20any%20page%22%5D%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Which%20file%20holds%20what%20is%20a%20Datasette%201.0%20rule%3A%20**metadata**%20(%60-m%60)%20is%20what%20the%20data%20*means*%3A%20titles%2C%20descriptions%2C%20source%20and%20license%3B%20**configuration**%20(%60-c%60)%20is%20how%20Datasette%20*behaves*%3A%20facets%2C%20canned%20queries%2C%20permissions.%20This%20course%20uses%20Datasette%20**1.0a41**%2C%20a%201.0%20alpha%3A%20the%20stable%200.x%20versions%20kept%20both%20in%20one%20file%2C%20so%20instructions%20written%20for%20them%20differ.%0A%0A%20%20%20%20Pick%20the%20release%20to%20publish%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20releases()%20-%3E%20list%5Bstr%5D%3A%0A%20%20%20%20%22%22%22The%20rounds%20whose%20release%20has%20been%20built%20(31_share_round.py%2C%20or%20run.py).%22%22%22%0A%20%20%20%20if%20not%20PATHS.release.exists()%3A%0A%20%20%20%20%20%20%20%20return%20%5B%5D%0A%20%20%20%20return%20sorted(p.parent.name%20for%20p%20in%20PATHS.release.glob(%22*%2Frelease.sqlite%22))%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_built%20%3D%20releases()%0A%20%20%20%20round_choice%20%3D%20mo.ui.dropdown(options%3D_built%20or%20%5BROUND_ID%5D%2C%20value%3DROUND_ID%20if%20ROUND_ID%20in%20_built%20or%20not%20_built%20else%20_built%5B-1%5D%2C%20label%3D%22Release%3A%22)%0A%20%20%20%20mo.vstack(%5Bround_choice%5D%20%2B%20(%5B%5D%20if%20_built%20else%20%5Bmo.callout(mo.md(%22No%20release%20is%20built%20yet.%20Build%20one%20first%3A%20%60uv%20run%20python%20run.py%20--round%202024Q2%60%2C%20or%20section%205%20of%20%6031_share_round.py%60.%22)%2C%20kind%3D%22warn%22)%5D))%0A%20%20%20%20return%20(round_choice%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(round_choice)%3A%0A%20%20%20%20release_folder%20%3D%20PATHS.release%20%2F%20round_choice.value%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20book%20%3D%20pd.read_csv(release_folder%20%2F%20%22codebook.csv%22%2C%20keep_default_na%3DFalse)%0A%20%20%20%20except%20FileNotFoundError%3A%0A%20%20%20%20%20%20%20%20book%20%3D%20NOT_YET%0A%20%20%20%20mo.stop(book%20is%20NOT_YET%2C%20mo.callout(mo.md(f%22%60%7Brelease_folder.relative_to(PATHS.root).as_posix()%7D%2Fcodebook.csv%60%20is%20not%20there%20yet%3A%20build%20the%20release%20first.%22)%2C%20kind%3D%22neutral%22))%0A%20%20%20%20mo.vstack(%5Bmo.md(f%22The%20codebook%20of%20release%20**%7Bround_choice.value%7D**%2C%20written%20by%20the%20Share%20stage%3A%22)%2C%20book%5D)%0A%20%20%20%20return%20book%2C%20release_folder%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202%20%C2%B7%20Describe%20the%20release%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**In%20this%20section**%20(about%2015%20minutes)%0A%0A%20%20%20%20-%20**You%20learn%3A**%20to%20build%20nested%20dictionaries%2C%20the%20shape%20of%20JSON%2C%20from%20a%20table.%0A%20%20%20%20-%20**You%20build%3A**%20%60metadata_for%60%2C%20which%20turns%20the%20codebook%20into%20Datasette's%20%60metadata.json%60.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%202.1.%20The%20portal's%20metadata%2C%20from%20the%20codebook%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20visitor%20reads%20the%20descriptions%20on%20the%20page%2C%20not%20your%20notebook.%20Taken%20from%20the%20codebook%2C%20they%20are%20the%20same%20words%20the%20contract%20enforces%2C%20and%20they%20change%20when%20it%20changes.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20round%20ID%20and%20its%20codebook%20%E2%86%92%20a%20dictionary%20in%20the%20shape%20Datasette%20reads.%20The%20database%20is%20called%20%60release%60%20(from%20the%20file%20name%20%60release.sqlite%60)%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22title%22%3A%20%22Labour%20survey%20release%2C%20round%202024Q2%22%2C%0A%20%20%20%20%20%20%20%20%22source%22%3A%20%22Course%20training%20data%20(synthetic)%22%2C%0A%20%20%20%20%20%20%20%20%22databases%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22release%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22tables%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22release_table%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22description%22%3A%20%22Persons%20by%20province%2C%20sex%20and%20age%20group%2C%20round%202024Q2%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22columns%22%3A%20%7B%22province%22%3A%20%22Province%20code%20(10%20characters%2C%20...%22%2C%20%22sex%22%3A%20%22Sex%2C%20from%20the%20questionnaire%22%2C%20...%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22codebook%22%3A%20%7B%22description%22%3A%20%22What%20each%20column%20of%20release_table%20means%2C%20its%20type%2C%20and%20its%20rules%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%7D%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20The%20round%20ID%20goes%20into%20the%20title%20and%20the%20table's%20description%20with%20an%20f-string%20(%60f%22...%20round%20%7Bround_id%7D%22%60).%20%60columns%60%20has%20one%20entry%20per%20row%20of%20the%20codebook%3A%20%60dict(zip(book%5B%22column%22%5D%2C%20book%5B%22description%22%5D))%60%20pairs%20each%20column%20with%20its%20description.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20metadata_for(round_id%3A%20str%2C%20book%3A%20pd.DataFrame)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22Datasette's%20metadata%20for%20a%20release%3A%20a%20title%2C%20the%20source%2C%20and%20a%20description%20of%20each%20table%20and%20column.%22%22%22%0A%20%20%20%20%23%20Exercise%202.1%3A%20the%20task%20and%20the%20shape%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20columns%20%3D%20one%20entry%20per%20codebook%20row%3A%20column%20-%3E%20description%0A%20%20%20%20%23%20Step%202%3A%20return%20the%20nested%20dictionary%20shown%20above%2C%20with%20round_id%20in%20the%20title%20and%20the%20table's%20description%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.1%22)%0A%0A%0A%40app.function%0Adef%20codebook_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20two-column%20codebook%2C%20for%20the%20checks.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%22column%22%3A%20%5B%22region%22%2C%20%22total%22%5D%2C%20%22description%22%3A%20%5B%22Region%20code%2C%20two%20digits%22%2C%20%22Estimated%20persons%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22type%22%3A%20%5B%22text%22%2C%20%22number%22%5D%2C%20%22may%20be%20missing%22%3A%20%5B%22no%22%2C%20%22yes%22%5D%2C%20%22rules%22%3A%20%5B%22%22%2C%20%22greater_than_or_equal_to(0)%22%5D%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%22Exercise%202.1%22%2C%20metadata_for%2C%20%5B(%22round%202024Q2%2C%20a%20two-column%20codebook%22%2C%20(%222024Q2%22%2C%20codebook_fixture()))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22round%202023Q4%2C%20the%20same%20codebook%22%2C%20(%222023Q4%22%2C%20codebook_fixture()))%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20what%3D%22releases%22%2C%20hint%3D%22Compare%20your%20dictionary%20with%20the%20shape%20above%2C%20key%20by%20key%3A%20spelling%20and%20nesting%20both%20count.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20a%20two-column%20codebook.%0A%20%20%20%20course.try_it(metadata_for%2C%20%222024Q2%22%2C%20codebook_fixture())%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%202.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'description'*%20%7C%20The%20codebook%20was%20read%20without%20the%20description%20column%2C%20or%20the%20column%20name%20is%20misspelt.%20%7C%20%60book%5B%22description%22%5D%60%20%7C%0A%20%20%20%20%7C%20The%20title%20shows%20%60%7Bround_id%7D%60%20%7C%20The%20%60f%60%20before%20the%20quotes%20is%20missing.%20%7C%20%60f%22...%20round%20%7Bround_id%7D%22%60%20%7C%0A%20%20%20%20%7C%20Right%20content%2C%20still%20red%20%7C%20A%20key%20is%20spelt%20or%20nested%20differently%3A%20%60%22tables%22%60%20inside%20%60%22release%22%60%2C%20inside%20%60%22databases%22%60.%20%7C%20the%20shape%20above%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(book%2C%20round_choice)%3A%0A%20%20%20%20metadata%20%3D%20attempt(metadata_for%2C%20round_choice.value%2C%20book)%0A%20%20%20%20mo.stop(metadata%20is%20NOT_YET%2C%20waiting_for(%22Exercise%202.1%22))%0A%20%20%20%20mo.vstack(%5Bmo.md(f%22%60metadata.json%60%20for%20release%20**%7Bround_choice.value%7D**%2C%20as%20Datasette%20will%20read%20it%3A%22)%2C%20mo.tree(metadata)%5D)%0A%20%20%20%20return%20(metadata%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203%20%C2%B7%20A%20canned%20query%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**In%20this%20section**%20(about%2020%20minutes)%0A%0A%20%20%20%20-%20**You%20learn%3A**%20a%20question%20written%20once%20in%20SQL%2C%20with%20a%20**parameter**%20the%20visitor%20fills%20in.%0A%20%20%20%20-%20**You%20build%3A**%20%60region_sql%60%2C%20the%20portal's%20region%20summary.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20A%20**canned%20query**%20is%20a%20SQL%20query%20saved%20in%20the%20configuration%2C%20with%20a%20name%20and%20a%20description.%20Datasette%20shows%20it%20as%20a%20page%20with%20a%20form%3A%20a%20word%20written%20%60%3Aregion%60%20in%20the%20SQL%20becomes%20a%20box%20where%20the%20visitor%20types%20a%20region%2C%20and%20Datasette%20passes%20the%20value%20safely%3A%20whatever%20is%20typed%20is%20a%20value%2C%20never%20a%20piece%20of%20SQL.%0A%0A%20%20%20%20It%20is%20how%20a%20portal%20answers%20the%20common%20questions%20without%20asking%20visitors%20to%20write%20SQL%2C%20and%20it%20is%20how%20you%20can%20turn%20off%20arbitrary%20SQL%20for%20the%20public%20while%20keeping%20the%20questions%20you%20vetted%20(section%206).%0A%0A%20%20%20%20%23%23%23%20Exercise%203.1.%20People%20by%20province%20and%20sex%2C%20for%20one%20region%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20no%20input%3B%20the%20function%20gives%20back%20the%20SQL%20text.%20Run%20on%20%60release_table%60%20with%20a%20value%20for%20%60%3Aregion%60%2C%20the%20query%20gives%20one%20row%20per%20province%20of%20that%20region%20and%20sex%2C%20sorted%20by%20province%20then%20sex%2C%20with%20three%20columns%3A%0A%0A%20%20%20%20%7C%20column%20%7C%20what%20it%20is%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60province%60%2C%20%60sex%60%20%7C%20the%20group%20%7C%0A%20%20%20%20%7C%20%60weighted_persons%60%20%7C%20%60SUM(weighted_persons)%60%3A%20suppressed%20cells%20are%20empty%2C%20and%20a%20sum%20skips%20them%20%7C%0A%20%20%20%20%7C%20%60cells_suppressed%60%20%7C%20%60SUM(suppressed)%60%3A%20how%20many%20cells%20are%20missing%20from%20that%20sum.%20A%20reader%20must%20see%20that%20a%20total%20is%20incomplete.%20%7C%0A%0A%20%20%20%20A%20province%20belongs%20to%20a%20region%20when%20its%20first%20two%20characters%20are%20the%20region%3A%20%60substr(province%2C%201%2C%202)%20%3D%20%3Aregion%60.%20You%20met%20%60SELECT%60%2C%20%60WHERE%60%20and%20%60GROUP%20BY%60%20in%20the%20Get%20module%20(%6010_get_round.py%60%2C%20section%202)%3B%20%60ORDER%20BY%20province%2C%20sex%60%20sorts%20the%20rows%2C%20and%20%60AS%60%20names%20a%20column.%20SQLite%20writes%20them%20as%20DuckDB%20does.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20region_sql()%20-%3E%20str%3A%0A%20%20%20%20%22%22%22The%20canned%20query%3A%20one%20region's%20provinces%20by%20sex%2C%20with%20how%20many%20suppressed%20cells%20each%20total%20is%20missing.%22%22%22%0A%20%20%20%20%23%20Exercise%203.1%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Write%20the%20query%20between%20the%20triple%20quotes.%0A%20%20%20%20sql%20%3D%20%22%22%22%0A%20%20%20%20SELECT%20province%2C%20sex%0A%20%20%20%20FROM%20release_table%0A%20%20%20%20%22%22%22%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.1%22)%20%20%23%20delete%20this%20line%20when%20your%20query%20is%20ready%0A%20%20%20%20return%20sql%0A%0A%0A%40app.function%0Adef%20query_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20small%20release%20in%20two%20regions%2C%20with%20suppressed%20cells%2C%20for%20the%20checks.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%220100100000%22%5D%20*%204%20%2B%20%5B%220100200000%22%5D%20*%202%20%2B%20%5B%220600300000%22%5D%20*%204%2C%0A%20%20%20%20%20%20%20%20%22sex%22%3A%20%5B%22Female%22%2C%20%22Female%22%2C%20%22Male%22%2C%20%22Male%22%2C%20%22Female%22%2C%20%22Male%22%2C%20%22Female%22%2C%20%22Female%22%2C%20%22Male%22%2C%20%22Male%22%5D%2C%0A%20%20%20%20%20%20%20%20%22age_group%22%3A%20%5B%220-14%22%2C%20%2265%2B%22%5D%20*%202%20%2B%20%5B%220-14%22%2C%20%220-14%22%5D%20%2B%20%5B%220-14%22%2C%20%2265%2B%22%5D%20*%202%2C%0A%20%20%20%20%20%20%20%20%22weighted_persons%22%3A%20%5B1200.0%2C%20300.0%2C%201150.0%2C%20None%2C%20800.0%2C%20790.0%2C%20None%2C%20None%2C%20410.0%2C%20None%5D%2C%0A%20%20%20%20%20%20%20%20%22suppressed%22%3A%20%5B0%2C%200%2C%200%2C%201%2C%200%2C%200%2C%201%2C%201%2C%200%2C%201%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.function%0Adef%20region_query_story(region%3A%20str)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22Run%20your%20region_sql%20on%20a%20small%20release%20for%20one%20region%3A%20the%20column%20names%2C%20then%20the%20rows.%20Used%20by%20the%20check.%22%22%22%0A%20%20%20%20with%20closing(sqlite3.connect(%22%3Amemory%3A%22))%20as%20con%3A%0A%20%20%20%20%20%20%20%20query_fixture().to_sql(%22release_table%22%2C%20con%2C%20index%3DFalse)%0A%20%20%20%20%20%20%20%20cursor%20%3D%20con.execute(region_sql()%2C%20%7B%22region%22%3A%20region%7D)%0A%20%20%20%20%20%20%20%20return%20%5B%5Bd%5B0%5D%20for%20d%20in%20cursor.description%5D%5D%20%2B%20%5Blist(row)%20for%20row%20in%20cursor.fetchall()%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%22Exercise%203.1%22%2C%20region_query_story%2C%20%5B(%22region%2001%22%2C%20(%2201%22%2C))%2C%20(%22region%2006%22%2C%20(%2206%22%2C))%2C%20(%22region%2004%20(no%20provinces)%22%2C%20(%2204%22%2C))%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20what%3D%22regions%22%2C%20hint%3D%22The%20names%20count%20too%3A%20AS%20weighted_persons%2C%20AS%20cells_suppressed.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20query%20on%20a%20small%20release%2C%20region%2006.%20The%20first%20list%20is%20the%20column%20names.%0A%20%20%20%20course.try_it(region_query_story%2C%20%2206%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%203.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*OperationalError%3A%20near%20%22%E2%80%A6%22%3A%20syntax%20error*%20%7C%20A%20missing%20comma%20between%20columns%2C%20or%20a%20keyword%20out%20of%20order.%20%7C%20%60SELECT%20%E2%80%A6%20FROM%20%E2%80%A6%20WHERE%20%E2%80%A6%20GROUP%20BY%20%E2%80%A6%20ORDER%20BY%20%E2%80%A6%60%20%7C%0A%20%20%20%20%7C%20*ProgrammingError%3A%20You%20did%20not%20supply%20a%20value%20for%20binding%20parameter%20%3Aregion*%20%7C%20The%20name%20after%20%60%3A%60%20is%20not%20%60region%60.%20%7C%20%60%3Aregion%60%20%7C%0A%20%20%20%20%7C%20Every%20province%20of%20every%20region%20%7C%20The%20%60WHERE%60%20line%20is%20missing.%20%7C%20%60substr(province%2C%201%2C%202)%20%3D%20%3Aregion%60%20%7C%0A%20%20%20%20%7C%20One%20row%20per%20cell%2C%20not%20per%20province%20and%20sex%20%7C%20%60GROUP%20BY%60%20is%20missing%2C%20or%20groups%20by%20more%20columns.%20%7C%20%60GROUP%20BY%20province%2C%20sex%60%20%7C%0A%20%20%20%20%7C%20Right%20numbers%2C%20still%20red%20%7C%20A%20column%20is%20named%20differently.%20%7C%20%60AS%20weighted_persons%60%2C%20%60AS%20cells_suppressed%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(release_folder)%3A%0A%20%20%20%20_sql%20%3D%20attempt(region_sql)%0A%20%20%20%20mo.stop(_sql%20is%20NOT_YET%2C%20waiting_for(%22Exercise%203.1%22))%0A%20%20%20%20with%20closing(sqlite3.connect(release_folder%20%2F%20%22release.sqlite%22))%20as%20_con%3A%0A%20%20%20%20%20%20%20%20_preview%20%3D%20pd.read_sql_query(_sql%2C%20_con%2C%20params%3D%7B%22region%22%3A%20%2206%22%7D)%0A%20%20%20%20mo.vstack(%5Bmo.md(%22Your%20query%20on%20the%20real%20release%2C%20region%20**06**.%20The%20province%20with%20suppressed%20cells%20shows%20a%20smaller%20total%2C%20and%20says%20so%3A%22)%2C%20_preview%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%204%20%C2%B7%20Start%20the%20portal%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**In%20this%20section**%20(about%2025%20minutes)%0A%0A%20%20%20%20-%20**You%20learn%3A**%20to%20run%20a%20second%20program%20beside%20marimo%2C%20and%20to%20use%20a%20portal%20the%20way%20its%20visitors%20will.%0A%20%20%20%20-%20**You%20do%3A**%20write%20the%20two%20files%2C%20start%20Datasette%2C%20and%20work%20through%20the%20visitor's%20checklist.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20datasette_config(sql%3A%20str)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22Datasette's%20configuration%3A%20facets%20on%20the%20release%20table%2C%20and%20the%20canned%20query.%22%22%22%0A%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%22databases%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22release%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22tables%22%3A%20%7B%22release_table%22%3A%20%7B%22facets%22%3A%20%5B%22sex%22%2C%20%22age_group%22%5D%7D%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22queries%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22region_summary%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22People%20by%20province%20and%20sex%2C%20for%20one%20region%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22description%22%3A%20%22Enter%20a%20two-digit%20region%20code%2C%20like%2006.%20Totals%20skip%20suppressed%20cells%3A%20cells_suppressed%20says%20how%20many%20each%20total%20is%20missing.%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22sql%22%3A%20sql.strip()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%7D%0A%0A%0A%40app.function%0Adef%20write_datasette_files(folder%3A%20Path%2C%20metadata%3A%20dict%2C%20sql%3A%20str)%20-%3E%20list%5BPath%5D%3A%0A%20%20%20%20%22%22%22Write%20metadata.json%20and%20datasette.json%20beside%20release.sqlite.%22%22%22%0A%20%20%20%20written%20%3D%20%5Bfolder%20%2F%20%22metadata.json%22%2C%20folder%20%2F%20%22datasette.json%22%5D%0A%20%20%20%20written%5B0%5D.write_text(json.dumps(metadata%2C%20indent%3D2%2C%20ensure_ascii%3DFalse)%20%2B%20%22%5Cn%22%2C%20encoding%3D%22utf-8%22)%0A%20%20%20%20written%5B1%5D.write_text(json.dumps(datasette_config(sql)%2C%20indent%3D2%2C%20ensure_ascii%3DFalse)%20%2B%20%22%5Cn%22%2C%20encoding%3D%22utf-8%22)%0A%20%20%20%20return%20written%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(round_choice)%3A%0A%20%20%20%20write_button%20%3D%20mo.ui.run_button(label%3Df%22Write%20metadata.json%20and%20datasette.json%20for%20release%20%7Bround_choice.value%7D%22)%0A%20%20%20%20write_button%0A%20%20%20%20return%20(write_button%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(metadata%2C%20release_folder%2C%20round_choice%2C%20write_button)%3A%0A%20%20%20%20mo.stop(not%20write_button.value)%0A%20%20%20%20_sql%20%3D%20attempt(region_sql)%0A%20%20%20%20mo.stop(_sql%20is%20NOT_YET%2C%20waiting_for(%22Exercise%203.1%22))%0A%20%20%20%20_files%20%3D%20write_datasette_files(release_folder%2C%20metadata%2C%20_sql)%0A%20%20%20%20_r%20%3D%20f%22release%2F%7Bround_choice.value%7D%22%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.callout(mo.md(%22Written%3A%20%22%20%2B%20%22%2C%20%22.join(f%22%60%7Bp.relative_to(PATHS.root).as_posix()%7D%60%22%20for%20p%20in%20_files))%2C%20kind%3D%22success%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20**Start%20Datasette%20in%20a%20second%20terminal.**%20marimo%20keeps%20running%20in%20the%20first%20one%3B%20leave%20it%20open.%0A%0A%20%20%20%201.%20Open%20a%20**new**%20terminal%20window%20(or%20a%20new%20tab)%2C%20and%20go%20to%20the%20course%20folder%2C%20as%20in%20%60SETUP.md%60.%20(On%20molab%2C%20there%20is%20no%20terminal%20for%20a%20server%3A%20pair%20with%20a%20tablemate%20who%20runs%20the%20course%20on%20a%20laptop.)%0A%20%20%20%202.%20Paste%20this%20line%20and%20press%20Enter%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20uv%20run%20datasette%20serve%20%7B_r%7D%2Frelease.sqlite%20-m%20%7B_r%7D%2Fmetadata.json%20-c%20%7B_r%7D%2Fdatasette.json%20-p%20%7BPORT%7D%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%203.%20When%20it%20prints%20%60Uvicorn%20running%20on%20http%3A%2F%2F127.0.0.1%3A%7BPORT%7D%60%2C%20open%20**http%3A%2F%2F127.0.0.1%3A%7BPORT%7D**%20in%20your%20browser.%0A%0A%20%20%20%20To%20stop%20it%20later%3A%20click%20in%20that%20terminal%20and%20press%20**Ctrl%20%2B%20C**.%20After%20changing%20a%20file%2C%20stop%20it%20and%20start%20it%20again%3A%20Datasette%20reads%20the%20two%20files%20when%20it%20starts.%0A%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20%23%23%23%20The%20visitor's%20checklist%0A%0A%20%20%20%20Work%20through%20it%20in%20the%20browser%2C%20at%20your%20table.%20Each%20step%20is%20something%20a%20visitor%20to%20a%20PSA%20portal%20would%20do.%0A%0A%20%20%20%201.%20**Find%20the%20table.**%20Open%20%60release%60%2C%20then%20%60release_table%60.%20Your%20title%20and%20descriptions%20are%20on%20the%20page%3A%20hover%20over%20a%20column%20heading%20to%20see%20its%20description.%0A%20%20%20%202.%20**Facet.**%20Under%20the%20table's%20heading%20are%20counts%20by%20%60sex%60%20and%20%60age_group%60%3A%20the%20facets%20from%20%60datasette.json%60.%20Click%20**Female**%3A%20the%20table%20is%20filtered%2C%20and%20the%20address%20bar%20shows%20the%20filter%20as%20%60%3Fsex%3DFemale%60.%20That%20address%20is%20a%20link%20you%20can%20send.%0A%20%20%20%203.%20**Filter.**%20Use%20the%20filter%20row%3A%20%60province%60%20%C2%B7%20%60starts%20with%60%20%C2%B7%20%6006%60.%20Which%20cells%20are%20suppressed%3F%0A%20%20%20%204.%20**Ask%20the%20canned%20query.**%20Go%20back%20to%20%60release%60%2C%20open%20**People%20by%20province%20and%20sex%2C%20for%20one%20region**%2C%20type%20%6006%60%2C%20and%20press%20Run.%20Which%20province's%20totals%20are%20incomplete%2C%20and%20by%20how%20many%20cells%3F%0A%20%20%20%205.%20**Get%20the%20data.**%20On%20any%20page%2C%20click%20**json**%20(or%20add%20%60.json%60%20before%20the%20%60%3F%60%20in%20the%20address)%3A%20the%20same%20rows%2C%20for%20a%20program.%20Click%20**CSV**%20to%20download%20them.%0A%20%20%20%206.%20**Read%20the%20codebook.**%20Open%20the%20%60codebook%60%20table%3A%20the%20contract%2C%20in%20words%2C%20beside%20the%20data.%0A%0A%20%20%20%20Then%2C%20with%20your%20table%3A%20what%20would%20a%20visitor%20misread%20if%20%60cells_suppressed%60%20were%20not%20there%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%205%20%C2%B7%20Read%20the%20portal%20from%20Python%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**In%20this%20section**%20(about%2020%20minutes)%0A%0A%20%20%20%20-%20**You%20learn%3A**%20that%20a%20portal%20is%20also%20an%20API%2C%20read%20page%20by%20page%20like%20the%20OpenSTAT%20pages%20in%20the%20Get%20module.%0A%20%20%20%20-%20**You%20build%3A**%20%60read_datasette%60.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20Add%20%60.json%60%20to%20a%20Datasette%20page%20and%20it%20answers%20in%20JSON.%20A%20table's%20answer%20comes%20in%20**pages**%20of%20up%20to%20100%20rows%3A%0A%0A%20%20%20%20%60%60%60json%0A%20%20%20%20%7B%7B%0A%20%20%20%20%20%20%22ok%22%3A%20true%2C%0A%20%20%20%20%20%20%22rows%22%3A%20%5B%7B%7B%22province%22%3A%20%220100100000%22%2C%20%22sex%22%3A%20%22Female%22%2C%20%22age_group%22%3A%20%220-14%22%2C%20%22weighted_persons%22%3A%2045684.0%2C%20%22suppressed%22%3A%200%7D%7D%2C%20...%5D%2C%0A%20%20%20%20%20%20%22next_url%22%3A%20%22http%3A%2F%2F127.0.0.1%3A%7BPORT%7D%2Frelease%2Frelease_table.json%3F_next%3D...%22%2C%0A%20%20%20%20%20%20...%0A%20%20%20%20%7D%7D%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20%60next_url%60%20is%20the%20address%20of%20the%20next%20page%3A%20%60null%60%20(Python%3A%20%60None%60)%20on%20the%20last%20one%2C%20and%20missing%20altogether%20from%20a%20canned%20query's%20answer%2C%20which%20is%20always%20one%20page.%20%60page.get(%22next_url%22)%60%20gives%20%60None%60%20in%20both%20cases.%20This%20is%20the%20loop%20you%20wrote%20in%20the%20Get%20module%20for%20the%20OpenSTAT%20pages%20(%60read_api%60)%2C%20with%20an%20address%20in%20place%20of%20a%20file%20name.%0A%0A%20%20%20%20Two%20differences%20from%20files%3A%20a%20web%20request%20can%20fail%2C%20and%20Datasette%20says%20so%20with%20%60%22ok%22%3A%20false%60%20and%20an%20%60%22error%22%60%20message%3B%20and%20a%20canned%20query's%20answer%20stops%20at%20Datasette's%20row%20limit%20(1%2C000%20rows)%2C%20with%20%60%22truncated%22%3A%20true%60%20to%20say%20that%20rows%20are%20missing.%0A%0A%20%20%20%20%23%23%23%20Exercise%205.1.%20Every%20page%20of%20a%20Datasette%20table%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20function%20%60fetch%60%20(give%20it%20an%20address%2C%20it%20gives%20back%20the%20answer%20as%20a%20dictionary)%20and%20the%20first%20page's%20address%20%E2%86%92%20one%20table%20with%20the%20rows%20of%20every%20page.%0A%0A%20%20%20%20-%20follow%20%60page.get(%22next_url%22)%60%20until%20it%20is%20%60None%60%3B%0A%20%20%20%20-%20when%20a%20page%20says%20%60%22ok%22%3A%20false%60%2C%20stop%20with%20%60raise%20RuntimeError(page%5B%22error%22%5D)%60%3B%0A%20%20%20%20-%20when%20a%20page%20says%20%60%22truncated%22%3A%20true%60%2C%20stop%20with%20%60raise%20RuntimeError(%22truncated%22)%60.%0A%0A%20%20%20%20In%20both%20cases%2C%20a%20half-read%20table%20must%20not%20look%20complete.%0A%0A%20%20%20%20%60fetch%60%20is%20an%20input%2C%20so%20the%20check%20can%20give%20your%20function%20pretend%20pages%2C%20and%20the%20live%20cell%20below%20can%20give%20it%20the%20real%20portal.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20read_datasette(fetch%2C%20url%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Every%20row%20of%20a%20Datasette%20table%2C%20following%20next_url%20from%20page%20to%20page.%22%22%22%0A%20%20%20%20%23%20Exercise%205.1%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20rows%20%3D%20an%20empty%20list%0A%20%20%20%20%23%20Step%202%3A%20while%20url%3A%20page%20%3D%20fetch(url)%3B%20if%20the%20page%20is%20not%20ok%2C%20raise%20RuntimeError(page%5B%22error%22%5D)%3B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20if%20page.get(%22truncated%22)%2C%20raise%20RuntimeError(%22truncated%22)%3B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20add%20the%20page's%20rows%20to%20your%20list%20(rows.extend(...))%3B%20url%20%3D%20page.get(%22next_url%22)%0A%20%20%20%20%23%20Step%203%3A%20return%20pd.DataFrame(rows)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.1%22)%0A%0A%0A%40app.function%0Adef%20pretend_portal(pages%3A%20dict%5Bstr%2C%20dict%5D)%3A%0A%20%20%20%20%22%22%22A%20fetch%20function%20that%20answers%20from%20a%20dictionary%20of%20address%20-%3E%20page%2C%20as%20Datasette%20would.%20For%20the%20checks.%22%22%22%0A%20%20%20%20def%20fetch(url%3A%20str)%20-%3E%20dict%3A%0A%20%20%20%20%20%20%20%20return%20pages%5Burl%5D%0A%20%20%20%20return%20fetch%0A%0A%0A%40app.function%0Adef%20portal_story(case%3A%20str)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22Read%20pretend%20Datasette%20pages%20with%20your%20read_datasette.%20Used%20by%20the%20check.%22%22%22%0A%20%20%20%20row%20%3D%20lambda%20p%2C%20s%3A%20%7B%22province%22%3A%20p%2C%20%22sex%22%3A%20s%2C%20%22weighted_persons%22%3A%20100.0%2C%20%22suppressed%22%3A%200%7D%0A%20%20%20%20pages%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22three%20pages%22%3A%20%7B%22%2Ft.json%22%3A%20%7B%22ok%22%3A%20True%2C%20%22rows%22%3A%20%5Brow(%2201%22%2C%20%22Female%22)%2C%20row(%2201%22%2C%20%22Male%22)%5D%2C%20%22next_url%22%3A%20%22%2Ft.json%3F_next%3D2%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%2Ft.json%3F_next%3D2%22%3A%20%7B%22ok%22%3A%20True%2C%20%22rows%22%3A%20%5Brow(%2202%22%2C%20%22Female%22)%2C%20row(%2202%22%2C%20%22Male%22)%5D%2C%20%22next_url%22%3A%20%22%2Ft.json%3F_next%3D4%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%2Ft.json%3F_next%3D4%22%3A%20%7B%22ok%22%3A%20True%2C%20%22rows%22%3A%20%5Brow(%2203%22%2C%20%22Female%22)%5D%2C%20%22next_url%22%3A%20None%7D%7D%2C%0A%20%20%20%20%20%20%20%20%22one%20page%22%3A%20%7B%22%2Ft.json%22%3A%20%7B%22ok%22%3A%20True%2C%20%22rows%22%3A%20%5Brow(%2206%22%2C%20%22Male%22)%5D%2C%20%22next_url%22%3A%20None%7D%7D%2C%0A%20%20%20%20%20%20%20%20%22a%20canned%20query's%20answer%20(no%20next_url)%22%3A%20%7B%22%2Ft.json%22%3A%20%7B%22ok%22%3A%20True%2C%20%22rows%22%3A%20%5Brow(%2206%22%2C%20%22Female%22)%2C%20row(%2206%22%2C%20%22Male%22)%5D%2C%20%22truncated%22%3A%20False%7D%7D%2C%0A%20%20%20%20%20%20%20%20%22a%20canned%20query%20cut%20at%20the%20row%20limit%22%3A%20%7B%22%2Ft.json%22%3A%20%7B%22ok%22%3A%20True%2C%20%22rows%22%3A%20%5Brow(%2206%22%2C%20%22Female%22)%5D%20*%203%2C%20%22truncated%22%3A%20True%7D%7D%2C%0A%20%20%20%20%20%20%20%20%22an%20error%20on%20the%20second%20page%22%3A%20%7B%22%2Ft.json%22%3A%20%7B%22ok%22%3A%20True%2C%20%22rows%22%3A%20%5Brow(%2201%22%2C%20%22Female%22)%5D%2C%20%22next_url%22%3A%20%22%2Ft.json%3F_next%3D1%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%2Ft.json%3F_next%3D1%22%3A%20%7B%22ok%22%3A%20False%2C%20%22error%22%3A%20%22Table%20not%20found%3A%20release_tabel%22%2C%20%22status%22%3A%20404%7D%7D%2C%0A%20%20%20%20%7D%5Bcase%5D%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20table%20%3D%20read_datasette(pretend_portal(pages)%2C%20%22%2Ft.json%22)%0A%20%20%20%20except%20NotImplementedError%3A%20%20%23%20a%20kind%20of%20RuntimeError%3A%20let%20it%20through%2C%20so%20the%20check%20shows%20%22not%20attempted%20yet%22%0A%20%20%20%20%20%20%20%20raise%0A%20%20%20%20except%20RuntimeError%20as%20error%3A%20%20%23%20what%20a%20correct%20function%20does%20on%20an%20error%20page%3A%20stop%2C%20with%20Datasette's%20message%0A%20%20%20%20%20%20%20%20return%20%5Bf%22stopped%3A%20%7Berror%7D%22%5D%0A%20%20%20%20return%20table.astype(object).values.tolist()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%22Exercise%205.1%22%2C%20portal_story%2C%20%5B(c%2C%20(c%2C))%20for%20c%20in%20%5B%22three%20pages%22%2C%20%22one%20page%22%2C%20%22a%20canned%20query's%20answer%20(no%20next_url)%22%2C%20%22a%20canned%20query%20cut%20at%20the%20row%20limit%22%2C%20%22an%20error%20on%20the%20second%20page%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20what%3D%22portals%22%2C%20hint%3D%22Three%20pages%20hold%20five%20rows.%20A%20missing%20next_url%20means%20the%20last%20page.%20On%20an%20error%20page%20or%20a%20truncated%20one%2C%20raise%20RuntimeError.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20three%20pretend%20pages%2C%20five%20rows.%0A%20%20%20%20course.try_it(portal_story%2C%20%22three%20pages%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%205.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Only%20the%20first%20page's%20rows%20%7C%20The%20loop%20does%20not%20move%20to%20the%20next%20address.%20%7C%20%60url%20%3D%20page%5B%22next_url%22%5D%60%20inside%20the%20loop%20%7C%0A%20%20%20%20%7C%20The%20cell%20never%20finishes%20%7C%20%60url%60%20never%20becomes%20%60None%60%3A%20it%20is%20set%20before%20the%20loop%20but%20not%20inside%20it.%20%7C%20the%20last%20line%20of%20the%20loop%20%7C%0A%20%20%20%20%7C%20The%20error%20page%20gives%20a%20table%20%7C%20%60ok%60%20is%20not%20checked%20before%20using%20%60rows%60.%20%7C%20%60if%20not%20page%5B%22ok%22%5D%3A%20raise%20RuntimeError(page%5B%22error%22%5D)%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'next_url'*%20on%20a%20canned%20query%20%7C%20A%20canned%20query's%20answer%20has%20no%20%60next_url%60.%20%7C%20%60page.get(%22next_url%22)%60%20%7C%0A%20%20%20%20%7C%20The%20cut%20answer%20gives%20a%20table%20%7C%20%60truncated%60%20is%20not%20checked.%20%7C%20%60if%20page.get(%22truncated%22)%3A%20raise%20RuntimeError(%22truncated%22)%60%20%7C%0A%20%20%20%20%7C%20Rows%20inside%20rows%20%7C%20%60rows.append(page%5B%22rows%22%5D)%60%20adds%20the%20list%20as%20one%20item.%20%7C%20%60rows.extend(...)%60%20adds%20each%20row%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20fetch_json(url%3A%20str)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22Ask%20a%20web%20address%20for%20JSON%2C%20and%20give%20back%20the%20answer%20as%20a%20dictionary.%20Datasette's%20error%20answers%20are%20JSON%20too.%22%22%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20with%20urllib.request.urlopen(url%2C%20timeout%3D10)%20as%20response%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20json.load(response)%0A%20%20%20%20except%20urllib.error.HTTPError%20as%20error%3A%0A%20%20%20%20%20%20%20%20return%20json.load(error)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20portal_url%20%3D%20mo.ui.text(value%3Df%22http%3A%2F%2F127.0.0.1%3A%7BPORT%7D%2Frelease%2Frelease_table.json%22%2C%20label%3D%22Address%3A%22%2C%20full_width%3DTrue)%0A%20%20%20%20read_button%20%3D%20mo.ui.run_button(label%3D%22Read%20every%20page%20from%20the%20portal%22)%0A%20%20%20%20mo.vstack(%5Bmo.md(%22**Live%3A**%20read%20the%20running%20portal%20with%20your%20%60read_datasette%60.%20Try%20a%20filtered%20address%20too%2C%20like%20%60...release_table.json%3Fsex%3DFemale%60%2C%20or%20the%20canned%20query%3A%20%60...%2Frelease%2Fregion_summary.json%3Fregion%3D06%60.%22)%2C%20portal_url%2C%20read_button%5D)%0A%20%20%20%20return%20portal_url%2C%20read_button%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(portal_url%2C%20read_button)%3A%0A%20%20%20%20mo.stop(not%20read_button.value)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20_table%20%3D%20attempt(read_datasette%2C%20fetch_json%2C%20portal_url.value)%0A%20%20%20%20%20%20%20%20_out%20%3D%20waiting_for(%22Exercise%205.1%22)%20if%20_table%20is%20NOT_YET%20else%20mo.vstack(%5Bmo.md(f%22**%7Blen(_table)%3A%2C%7D%20rows**%20from%20the%20portal%3A%22)%2C%20_table%5D)%0A%20%20%20%20except%20urllib.error.URLError%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(f%22Nothing%20answers%20at%20that%20address.%20Is%20Datasette%20running%20in%20the%20second%20terminal%20(section%204)%2C%20on%20port%20%7BPORT%7D%3F%22)%2C%20kind%3D%22warn%22)%0A%20%20%20%20except%20Exception%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(f%22%7Btype(_error).__name__%7D%3A%20%7B_error%7D%22)%2C%20kind%3D%22warn%22)%0A%20%20%20%20_out%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%206%20%C2%B7%20Publishing%20for%20real%0A%0A%20%20%20%20What%20you%20ran%20is%20the%20portal%20on%20your%20own%20computer%3A%20only%20you%20can%20open%20%60127.0.0.1%60.%20Putting%20it%20on%20the%20internet%20is%20a%20deployment%20decision%20for%20your%20office's%20IT%20and%20data-release%20policy%2C%20not%20a%20notebook%20step%2C%20but%20the%20questions%20are%20the%20same%20everywhere.%20Discuss%20them%20at%20your%20table%3A%0A%0A%20%20%20%20%7C%20question%20%7C%20what%20this%20course's%20release%20already%20answers%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20**What%20goes%20up%3F**%20%7C%20Only%20%60release.sqlite%60%3A%20suppressed%20cells%2C%20no%20respondent%20counts%2C%20no%20microdata.%20Never%20%60survey.duckdb%60.%20%7C%0A%20%20%20%20%7C%20**Who%20may%20write%20SQL%3F**%20%7C%20By%20default%20anyone%20may%20type%20their%20own%20SQL.%20For%20the%20public%2C%20many%20portals%20turn%20that%20off%20(%60--setting%20default_allow_sql%20off%60)%20and%20keep%20the%20canned%20queries%2C%20which%20still%20work.%20%7C%0A%20%20%20%20%7C%20**Where%20does%20it%20run%3F**%20%7C%20Datasette%20is%20a%20small%20web%20server%3A%20%60datasette%20publish%60%20can%20send%20a%20file%20and%20its%20two%20configuration%20files%20to%20a%20cloud%20service%3B%20an%20office%20server%20works%20too.%20**Datasette%20Lite**%20runs%20it%20entirely%20in%20a%20visitor's%20browser%2C%20from%20a%20SQLite%20file%20at%20a%20public%20address%3A%20no%20server%2C%20but%20everyone%20downloads%20the%20whole%20file.%20%7C%0A%20%20%20%20%7C%20**How%20is%20it%20updated%3F**%20%7C%20%60run.py%60%20builds%20the%20next%20round's%20release%3B%20the%20portal%20is%20restarted%20with%20the%20new%20file.%20Nobody%20edits%20the%20portal%20by%20hand.%20%7C%0A%20%20%20%20%7C%20**Who%20keeps%20it%20running%3F**%20%7C%20A%20public%20portal%20is%20a%20service%2C%20not%20a%20file.%20The%20EITI%20portal%20taught%20its%20team%20the%20usual%20lessons%3A%20automated%20crawlers%20can%20follow%20every%20facet%20and%20filter%20link%20and%20load%20the%20server%3B%20memory%20needs%20a%20limit%3B%20and%20something%20must%20watch%20it%20and%20say%20when%20it%20is%20down.%20That%20is%20a%20development%20and%20deployment%20job%20of%20its%20own%2C%20beyond%20this%20course.%20%7C%0A%0A%20%20%20%20**And%20marimo%3F**%20A%20notebook%20is%20not%20a%20portal%2C%20but%20it%20can%20be%20published%20too%3A%20a%20marimo%20notebook%20can%20be%20exported%20as%20a%20page%20(%60uv%20run%20marimo%20export%20html%60)%2C%20or%20shared%20on%20**molab**%2C%20marimo's%20online%20notebook%20service%2C%20where%20it%20runs%20in%20the%20browser.%20A%20published%20notebook%20should%20read%20the%20portal's%20JSON%2C%20as%20your%20%60read_datasette%60%20does%2C%20never%20the%20microdata.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**Recap**%0A%0A%20%20%20%20-%20Datasette%20turns%20a%20SQLite%20file%20into%20a%20website%20and%20a%20JSON%20API%3B%20metadata%20(%60-m%60)%20says%20what%20the%20data%20means%2C%20configuration%20(%60-c%60)%20how%20to%20show%20it.%0A%20%20%20%20-%20The%20portal's%20descriptions%20come%20from%20the%20codebook%2C%20so%20they%20come%20from%20the%20contract.%0A%20%20%20%20-%20A%20canned%20query%20is%20a%20vetted%20question%20with%20a%20parameter%3A%20visitors%20fill%20in%20a%20value%2C%20never%20SQL.%0A%20%20%20%20-%20A%20portal%20is%20an%20API%3A%20read%20it%20page%20by%20page%2C%20and%20stop%20on%20an%20error%20rather%20than%20keep%20half%20a%20table.%0A%0A%20%20%20%20Key%20terms%20(%5Bglossary%5D(%2F%3Ffile%3D91_glossary.py))%3A%20data%20portal%2C%20canned%20query%2C%20parameter%2C%20facet%2C%20API%2C%20JSON.%20**Next%3A**%20the%20capstone%2C%20in%20groups%3A%20%5B33_capstone_next_round.py%5D(%2F%3Ffile%3D33_capstone_next_round.py)%2C%20%5B34_capstone_own_data.py%5D(%2F%3Ffile%3D34_capstone_own_data.py)%20or%20%5B35_capstone_explore.py%5D(%2F%3Ffile%3D35_capstone_explore.py).%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%226.3%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
d64dd1f2643323b90a9fe49909d0ac57