diff --git a/nbs/09_magics.ipynb b/nbs/09_magics.ipynb index 57460f9..f85a6dc 100644 --- a/nbs/09_magics.ipynb +++ b/nbs/09_magics.ipynb @@ -1026,7 +1026,8 @@ "def _get_help_html(self, code):\n", " html_base = \"https://www.stata.com\"\n", " html_help = urllib.parse.urljoin(html_base, \"help.cgi?{}\")\n", - " reply = urllib.request.urlopen(html_help.format(code))\n", + " url_safe_code = urllib.parse.quote(code)\n", + " reply = urllib.request.urlopen(html_help.format(url_safe_code))\n", " html = reply.read().decode(\"utf-8\")\n", "\n", " # Remove excessive extra lines (Note css: \"white-space: pre-wrap\")\n", @@ -1129,28 +1130,145 @@ "metadata": {}, "outputs": [ { - "ename": "HTTPError", - "evalue": "HTTP Error 403: Forbidden", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mHTTPError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[52], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m#| eval: False\u001b[39;00m\n\u001b[0;32m 2\u001b[0m test_instance \u001b[38;5;241m=\u001b[39m StataMagics()\n\u001b[1;32m----> 3\u001b[0m out \u001b[38;5;241m=\u001b[39m \u001b[43mtest_instance\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_help_html\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43morder\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 4\u001b[0m HTML(out)\n", - "Cell \u001b[1;32mIn[49], line 6\u001b[0m, in \u001b[0;36m_get_help_html\u001b[1;34m(self, code)\u001b[0m\n\u001b[0;32m 4\u001b[0m html_base \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhttps://www.stata.com\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 5\u001b[0m html_help \u001b[38;5;241m=\u001b[39m urllib\u001b[38;5;241m.\u001b[39mparse\u001b[38;5;241m.\u001b[39murljoin(html_base, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhelp.cgi?\u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 6\u001b[0m reply \u001b[38;5;241m=\u001b[39m \u001b[43murllib\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43murlopen\u001b[49m\u001b[43m(\u001b[49m\u001b[43mhtml_help\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mformat\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcode\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 7\u001b[0m html \u001b[38;5;241m=\u001b[39m reply\u001b[38;5;241m.\u001b[39mread()\u001b[38;5;241m.\u001b[39mdecode(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mutf-8\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 9\u001b[0m \u001b[38;5;66;03m# Remove excessive extra lines (Note css: \"white-space: pre-wrap\")\u001b[39;00m\n", - "File \u001b[1;32mc:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\urllib\\request.py:216\u001b[0m, in \u001b[0;36murlopen\u001b[1;34m(url, data, timeout, cafile, capath, cadefault, context)\u001b[0m\n\u001b[0;32m 214\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 215\u001b[0m opener \u001b[38;5;241m=\u001b[39m _opener\n\u001b[1;32m--> 216\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mopener\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mopen\u001b[49m\u001b[43m(\u001b[49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[1;32mc:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\urllib\\request.py:525\u001b[0m, in \u001b[0;36mOpenerDirector.open\u001b[1;34m(self, fullurl, data, timeout)\u001b[0m\n\u001b[0;32m 523\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m processor \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprocess_response\u001b[38;5;241m.\u001b[39mget(protocol, []):\n\u001b[0;32m 524\u001b[0m meth \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(processor, meth_name)\n\u001b[1;32m--> 525\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[43mmeth\u001b[49m\u001b[43m(\u001b[49m\u001b[43mreq\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mresponse\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 527\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m response\n", - "File \u001b[1;32mc:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\urllib\\request.py:634\u001b[0m, in \u001b[0;36mHTTPErrorProcessor.http_response\u001b[1;34m(self, request, response)\u001b[0m\n\u001b[0;32m 631\u001b[0m \u001b[38;5;66;03m# According to RFC 2616, \"2xx\" code indicates that the client's\u001b[39;00m\n\u001b[0;32m 632\u001b[0m \u001b[38;5;66;03m# request was successfully received, understood, and accepted.\u001b[39;00m\n\u001b[0;32m 633\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m (\u001b[38;5;241m200\u001b[39m \u001b[38;5;241m<\u001b[39m\u001b[38;5;241m=\u001b[39m code \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m300\u001b[39m):\n\u001b[1;32m--> 634\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mparent\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43merror\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 635\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mhttp\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mresponse\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcode\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmsg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mhdrs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 637\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m response\n", - "File \u001b[1;32mc:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\urllib\\request.py:563\u001b[0m, in \u001b[0;36mOpenerDirector.error\u001b[1;34m(self, proto, *args)\u001b[0m\n\u001b[0;32m 561\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m http_err:\n\u001b[0;32m 562\u001b[0m args \u001b[38;5;241m=\u001b[39m (\u001b[38;5;28mdict\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdefault\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhttp_error_default\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;241m+\u001b[39m orig_args\n\u001b[1;32m--> 563\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call_chain\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[1;32mc:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\urllib\\request.py:496\u001b[0m, in \u001b[0;36mOpenerDirector._call_chain\u001b[1;34m(self, chain, kind, meth_name, *args)\u001b[0m\n\u001b[0;32m 494\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m handler \u001b[38;5;129;01min\u001b[39;00m handlers:\n\u001b[0;32m 495\u001b[0m func \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(handler, meth_name)\n\u001b[1;32m--> 496\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 497\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m result \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m 498\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n", - "File \u001b[1;32mc:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\urllib\\request.py:643\u001b[0m, in \u001b[0;36mHTTPDefaultErrorHandler.http_error_default\u001b[1;34m(self, req, fp, code, msg, hdrs)\u001b[0m\n\u001b[0;32m 642\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mhttp_error_default\u001b[39m(\u001b[38;5;28mself\u001b[39m, req, fp, code, msg, hdrs):\n\u001b[1;32m--> 643\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HTTPError(req\u001b[38;5;241m.\u001b[39mfull_url, code, msg, hdrs, fp)\n", - "\u001b[1;31mHTTPError\u001b[0m: HTTP Error 403: Forbidden" - ] + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "\n", + "StataNow 18 help for graph set\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "

\n", + "
\n", + "\n", + "\n", + "
\n",
+       "

[G-2] graph set -- Set graphics options\n", + "

Syntax\n", + "

Manage graph print settings\n", + "

graph set print [setopt setval]\n", + "

Manage graph export settings\n", + "

graph set [exporttype] [setopt setval]\n", + "

where exporttype is the export file type and may be one of \n", + "

ps | eps | svg | pdf\n", + "

and setopt is the option to set with the setting setval.\n", + "

Manage Graph window font settings\n", + "

graph set window fontface { fontname | default }\n", + " graph set window fontfacemono { fontname | default }\n", + " graph set window fontfacesans { fontname | default }\n", + " graph set window fontfaceserif { fontname | default }\n", + " graph set window fontfacesymbol { fontname | default }\n", + "

Description\n", + "

graph set without options lists the current graphics font, print, and\n", + " export settings for all exporttypes. graph set with window, print, or\n", + " exporttype lists the current settings for the Graph window, for printing,\n", + " or for the specified exporttype, respectively.\n", + "

graph set print allows you to change the print settings for graphics.\n", + "

graph set exporttype allows you to change the graphics export settings\n", + " for export file type exporttype.\n", + "

graph set window fontface* allows you to change the Graph window font\n", + " settings. (To change font settings for graphs exported to PostScript,\n", + " Encapsulated PostScript, Scalable Vector Graphic, or Portable Document\n", + " Format files, use graph set {ps|eps|svg|pdf} fontface*; see [G-3]\n", + " ps_options, [G-3] eps_options, [G-3] svg_options, or [G-3] pdf_options.)\n", + " If fontname contains spaces, enclose it in double quotes. If you specify\n", + " default for any of the fontface* settings, the default setting will be\n", + " restored.\n", + "

Remarks\n", + "

Remarks are presented under the following headings:\n", + "

Overview\n", + " Setting defaults\n", + "

Overview\n", + "

graph set allows you to permanently set the primary font face used in the\n", + " Graph window as well as the font faces to be used for the four Stata\n", + " \"font faces\" supported by the graph SMCL tags {stMono}, {stSans},\n", + " {stSerif}, and {stSymbol}. See [G-4] text for more details on these SMCL\n", + " tags.\n", + "

graph set also allows you to permanently set any of the options supported\n", + " by graph print (see [G-2] graph print) or by the specific export file\n", + " types provided by graph export (see [G-2] graph export).\n", + "

To find out more about the graph set print setopt options and their\n", + " associated values (setval), see [G-3] pr_options.\n", + "

Some graphics file types supported by graph export have options that can\n", + " be set. The file types that allow option settings and their associated\n", + " exporttypes are\n", + "

exporttype Description Available settings\n", + " ------------------------------------------------------------\n", + " ps PostScript [G-3] ps_options\n", + " eps Encapsulated PostScript [G-3] eps_options\n", + " svg Scalable Vector Graphics [G-3] svg_options\n", + " pdf Portable Document Format [G-3] pdf_options\n", + " ------------------------------------------------------------\n", + "

Setting defaults\n", + "

If you always want the Graph window to use Times New Roman as its default\n", + " font, you could type\n", + "

. graph set window fontface \"Times New Roman\"\n", + "

Later, you could type\n", + "

. graph set window fontface default\n", + "

to restore the factory setting.\n", + "

To change the font used by {stMono} in the Graph window, you could type\n", + "

. graph set window fontfacemono \"Lucida Console\"\n", + "

and to reset it, you could type\n", + "

. graph set window fontfacemono default\n", + "

You can list the current graph settings by typing\n", + "

. graph set\n", + "

\n", + "\n", + "
\n", + "\n", + "\n", + "\n", + "

\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "#| eval: False\n", "test_instance = StataMagics()\n", - "out = test_instance._get_help_html(\"order\")\n", + "out = test_instance._get_help_html(\"graph set\")\n", "HTML(out)" ] }, diff --git a/nbstata/magics.py b/nbstata/magics.py index 414d6de..62b880c 100644 --- a/nbstata/magics.py +++ b/nbstata/magics.py @@ -361,7 +361,8 @@ def _magic_frheadtail(self, code, kernel, cell, tail): def _get_help_html(self, code): html_base = "https://www.stata.com" html_help = urllib.parse.urljoin(html_base, "help.cgi?{}") - reply = urllib.request.urlopen(html_help.format(code)) + url_safe_code = urllib.parse.quote(code) + reply = urllib.request.urlopen(html_help.format(url_safe_code)) html = reply.read().decode("utf-8") # Remove excessive extra lines (Note css: "white-space: pre-wrap")