|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Using Trio CDP Inside Jupyter Notebook\n", |
| 8 | + "\n", |
| 9 | + "Trio CDP can be used in Jupyter notebook, but some caveats are warranted. First, Trio support in Jupyter is experimental. Read [the instructions](https://github.com/ipython/ipykernel/pull/479) for setting up Trio inside Jupyter.\n", |
| 10 | + "\n", |
| 11 | + "The second caveat is that Trio CDP uses [context variables](https://docs.python.org/3.7/library/contextvars.html?highlight=contextvar#contextvars.ContextVar) to keep track of which connection and/or session are associated with each task. In Jupyter notebook, however, each cell executes in a separate task, so connections and sessions are not automatically shared between cells. This notebook contains a workaround that allows a single connection and session to be shared across cells." |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": 1, |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "from trio_cdp import connect_cdp, dom, page, target" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": 2, |
| 26 | + "metadata": {}, |
| 27 | + "outputs": [], |
| 28 | + "source": [ |
| 29 | + "conn = await connect_cdp(GLOBAL_NURSERY,\n", |
| 30 | + " 'ws://127.0.0.1:9000/devtools/browser/f01f56ba-993e-4c83-adc0-10a1feb56449')" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": 3, |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [], |
| 38 | + "source": [ |
| 39 | + "# Here is where we have to do some magic to make the connection from the\n", |
| 40 | + "# previous cell available to other cells.\n", |
| 41 | + "import trio_cdp.context\n", |
| 42 | + "trio_cdp.context.set_global_connection(conn)" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": 4, |
| 48 | + "metadata": {}, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "# Now we can run Trio CDP commands and they execute on the connection\n", |
| 52 | + "# automatically.\n", |
| 53 | + "targets = await target.get_targets()" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 5, |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [ |
| 61 | + { |
| 62 | + "data": { |
| 63 | + "text/plain": [ |
| 64 | + "[TargetInfo(target_id=TargetID('0AF1752B5834E8ED9F36D7BC3D1AEF51'), type='page', title='Hyperion Gray', url='https://www.hyperiongray.com/', attached=False, opener_id=None, browser_context_id=BrowserContextID('B2A138B23272D6E4920555C2DE424E05')),\n", |
| 65 | + " TargetInfo(target_id=TargetID('A0CF2CC043BDF2E4F8F8C0514B9A2FD2'), type='page', title='Hyperion Gray', url='https://www.hyperiongray.com/', attached=False, opener_id=None, browser_context_id=BrowserContextID('B2A138B23272D6E4920555C2DE424E05'))]" |
| 66 | + ] |
| 67 | + }, |
| 68 | + "execution_count": 5, |
| 69 | + "metadata": {}, |
| 70 | + "output_type": "execute_result" |
| 71 | + } |
| 72 | + ], |
| 73 | + "source": [ |
| 74 | + "targets" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": 6, |
| 80 | + "metadata": {}, |
| 81 | + "outputs": [], |
| 82 | + "source": [ |
| 83 | + "session = await conn.connect_session(targets[0].target_id)" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": 7, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [], |
| 91 | + "source": [ |
| 92 | + "# We have to do something similar for the session so that it can be\n", |
| 93 | + "# reused across multiple cells.\n", |
| 94 | + "trio_cdp.context.set_global_session(session)" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": 8, |
| 100 | + "metadata": {}, |
| 101 | + "outputs": [], |
| 102 | + "source": [ |
| 103 | + "async with session.page_enable():\n", |
| 104 | + " async with session.wait_for(page.LoadEventFired):\n", |
| 105 | + " await page.navigate('https://www.hyperiongray.com')" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "code", |
| 110 | + "execution_count": 9, |
| 111 | + "metadata": {}, |
| 112 | + "outputs": [], |
| 113 | + "source": [ |
| 114 | + "doc = await dom.get_document()" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "code", |
| 119 | + "execution_count": 10, |
| 120 | + "metadata": {}, |
| 121 | + "outputs": [], |
| 122 | + "source": [ |
| 123 | + "title = await dom.query_selector(doc.node_id, 'title')" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": 11, |
| 129 | + "metadata": {}, |
| 130 | + "outputs": [ |
| 131 | + { |
| 132 | + "data": { |
| 133 | + "text/plain": [ |
| 134 | + "'<title>Hyperion Gray</title>'" |
| 135 | + ] |
| 136 | + }, |
| 137 | + "execution_count": 11, |
| 138 | + "metadata": {}, |
| 139 | + "output_type": "execute_result" |
| 140 | + } |
| 141 | + ], |
| 142 | + "source": [ |
| 143 | + "await dom.get_outer_html(title)" |
| 144 | + ] |
| 145 | + } |
| 146 | + ], |
| 147 | + "metadata": { |
| 148 | + "kernelspec": { |
| 149 | + "display_name": "Python 3 Trio", |
| 150 | + "language": "python", |
| 151 | + "name": "python3-trio" |
| 152 | + }, |
| 153 | + "language_info": { |
| 154 | + "codemirror_mode": { |
| 155 | + "name": "ipython", |
| 156 | + "version": 3 |
| 157 | + }, |
| 158 | + "file_extension": ".py", |
| 159 | + "mimetype": "text/x-python", |
| 160 | + "name": "python", |
| 161 | + "nbconvert_exporter": "python", |
| 162 | + "pygments_lexer": "ipython3", |
| 163 | + "version": "3.7.5" |
| 164 | + } |
| 165 | + }, |
| 166 | + "nbformat": 4, |
| 167 | + "nbformat_minor": 4 |
| 168 | +} |
0 commit comments