File size: 29,689 Bytes
c7f4bd0 |
1 2 3 4 5 6 7 8 |
{"nwo":"CermakM\/jupyter-datatables","sha":"59b4880f926caaecb862477f3f1c2e7bbebb4939","path":"jupyter_datatables\/__init__.py","language":"python","identifier":"enable_datatables_mode","parameters":"()","argument_list":"","return_statement":"","docstring":"Enable Jupyter DataTables.","docstring_summary":"Enable Jupyter DataTables.","docstring_tokens":["Enable","Jupyter","DataTables","."],"function":"def enable_datatables_mode():\n \"\"\"Enable Jupyter DataTables.\"\"\"\n global _IS_ENABLED\n\n if not _IS_INITIALIZED:\n init_datatables_mode()\n else:\n pd.DataFrame._repr_javascript_ = __REPR__\n\n _IS_ENABLED = True","function_tokens":["def","enable_datatables_mode","(",")",":","global","_IS_ENABLED","if","not","_IS_INITIALIZED",":","init_datatables_mode","(",")","else",":","pd",".","DataFrame",".","_repr_javascript_","=","__REPR__","_IS_ENABLED","=","True"],"url":"https:\/\/github.com\/CermakM\/jupyter-datatables\/blob\/59b4880f926caaecb862477f3f1c2e7bbebb4939\/jupyter_datatables\/__init__.py#L62-L71"}
{"nwo":"CermakM\/jupyter-datatables","sha":"59b4880f926caaecb862477f3f1c2e7bbebb4939","path":"jupyter_datatables\/__init__.py","language":"python","identifier":"disable_datatables_mode","parameters":"()","argument_list":"","return_statement":"","docstring":"Disable Jupyter DataTables.","docstring_summary":"Disable Jupyter DataTables.","docstring_tokens":["Disable","Jupyter","DataTables","."],"function":"def disable_datatables_mode():\n \"\"\"Disable Jupyter DataTables.\"\"\"\n global _IS_ENABLED\n\n if _IS_INITIALIZED:\n del pd.DataFrame._repr_javascript_\n\n _IS_ENABLED = False","function_tokens":["def","disable_datatables_mode","(",")",":","global","_IS_ENABLED","if","_IS_INITIALIZED",":","del","pd",".","DataFrame",".","_repr_javascript_","_IS_ENABLED","=","False"],"url":"https:\/\/github.com\/CermakM\/jupyter-datatables\/blob\/59b4880f926caaecb862477f3f1c2e7bbebb4939\/jupyter_datatables\/__init__.py#L74-L81"}
{"nwo":"CermakM\/jupyter-datatables","sha":"59b4880f926caaecb862477f3f1c2e7bbebb4939","path":"jupyter_datatables\/__init__.py","language":"python","identifier":"init_datatables_mode","parameters":"(options: dict = None, classes: list = None)","argument_list":"","return_statement":"","docstring":"Initialize DataTable mode for pandas DataFrame representation.","docstring_summary":"Initialize DataTable mode for pandas DataFrame representation.","docstring_tokens":["Initialize","DataTable","mode","for","pandas","DataFrame","representation","."],"function":"def init_datatables_mode(options: dict = None, classes: list = None):\n \"\"\"Initialize DataTable mode for pandas DataFrame representation.\"\"\"\n global _IS_INITIALIZED\n global __REPR__\n\n if not _IS_ENABLED:\n raise Exception(\n \"Jupyter DataTables are disabled. Use `enable_datatables_mode()` instead.\")\n\n # extensions to be loaded\n extensions = config.defaults.extensions\n\n require(\"d3\", \"https:\/\/d3js.org\/d3.v5.min\")\n require(\"d3-array\", \"https:\/\/d3js.org\/d3-array.v2.min\")\n\n require.config(**{\n \"paths\": {\n \"moment\": \"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/moment.js\/2.8.0\/moment\",\n \"chartjs\": \"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/Chart.js\/2.8.0\/Chart\"\n },\n \"shim\": {\n \"chartjs\": {\n # enforce moment to be loaded before chartjs\n \"deps\": [\"moment\"]\n }\n },\n })\n\n # configure path to the datatables library using requireJS\n libs = OrderedDict(\n {\n # FIXME: minified version on prod\n \"datatables.net\": \"https:\/\/cdn.datatables.net\/1.10.18\/js\/jquery.dataTables\"\n }\n )\n shim = OrderedDict({\"datatables.net\": {\"exports\": \"$.fn.dataTable\"}})\n\n bundles = OrderedDict()\n\n if extensions.buttons:\n lib = \"datatables.net-buttons\"\n\n libs[lib] = \"https:\/\/cdn.datatables.net\/buttons\/1.5.6\/js\/dataTables.buttons.min\"\n shim[lib] = {\"deps\": [\"datatables.net\"]}\n\n # required to export Excel file, must be loaded first\n libs[\"jszip\"] = \"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/jszip\/2.5.0\/jszip.min\"\n\n bundles[lib] = {\n \"buttons.colvis\": \"https:\/\/cdn.datatables.net\/buttons\/1.5.6\/js\/buttons.colVis.min\",\n \"buttons.flash\": \"https:\/\/cdn.datatables.net\/buttons\/1.5.6\/js\/buttons.flash.min\",\n \"buttons.html5\": \"https:\/\/cdn.datatables.net\/buttons\/1.5.6\/js\/buttons.html5.min\",\n \"buttons.print\": \"https:\/\/cdn.datatables.net\/buttons\/1.5.6\/js\/buttons.print.min\",\n }\n for bundle, path in bundles[lib].items():\n libs[bundle] = path\n shim[bundle] = {\"deps\": [\"jszip\", lib]}\n\n # requirements for correct Buttons functionality\n libs[\n \"pdfmake\"\n ] = \"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/pdfmake\/0.1.36\/pdfmake.min\"\n shim[\"pdfmake\"] = {\"deps\": [\"datatables.net\"]}\n libs[\n \"vfsfonts\"\n ] = \"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/pdfmake\/0.1.36\/vfs_fonts\"\n shim[\"vfsfonts\"] = {\"deps\": [\"datatables.net\"]}\n\n if extensions.responsive:\n libs[\"datatables.responsive\"] = (\n \"https:\/\/cdn.datatables.net\/\" \"responsive\/2.2.2\/js\/dataTables.responsive.min\"\n ) # Responsive\n\n if extensions.scroller:\n libs[\"datatables.scroller\"] = (\n \"https:\/\/cdn.datatables.net\/\" \"scroller\/2.0.0\/js\/dataTables.scroller.min\"\n ) # Scroll\n\n if extensions.select:\n libs[\"datatables.select\"] = (\n \"https:\/\/cdn.datatables.net\/\" \"select\/1.3.0\/js\/dataTables.select.min\"\n ) # Select\n\n require.config(paths=libs, shim=shim)\n\n # link stylesheets\n link_css(\n \"https:\/\/cdn.datatables.net\/v\/dt\/\"\n \"dt-1.10.18\/\" # DataTables\n \"af-2.3.3\/\" # AutoFill\n \"b-1.5.6\/\" # Buttons\n \"b-colvis-1.5.6\/\" # Buttons - Column Visibility\n \"b-flash-1.5.6\/\" # Buttons - Flash\n \"b-html5-1.5.6\/\" # Buttons - HTML5\n \"b-print-1.5.6\/\" # Buttons - Print View\n \"cr-1.5.0\/\" # ColReorder\n \"fc-3.2.5\/\" # FixedColumns\n \"fh-3.1.4\/\" # FixedHeader\n \"kt-2.5.0\/\" # KeyTable\n \"r-2.2.2\/\" # Responsive\n \"rg-1.1.0\/\" # RowGroup\n \"rr-1.2.4\/\" # RowReorder\n \"sc-2.0.0\/\" # Scroll\n \"sl-1.3.0\/\" # Select\n \"datatables.min.css\",\n {\"id\": \"datatables.min.css\"},\n )\n\n # load custom style\n load_css(\n Path(_HERE, \"css\/jupyter-datatables.css\").read_text(encoding=\"utf-8\"),\n {\"id\": \"jupyter-datatables-css\"},\n )\n\n load_js(\n Path(_HERE, \"js\/config.js\").read_text(encoding=\"utf-8\"),\n {\"id\": \"dt-config-js\"})\n\n load_js(\n Path(_HERE, \"js\/components.js\").read_text(encoding=\"utf-8\"),\n {\"id\": \"dt-components-js\"})\n\n load_js(\n Path(_HERE, \"js\/graph-objects.js\").read_text(encoding=\"utf-8\"),\n {\"id\": \"dt-graph-objects-js\"})\n\n load_js(\n Path(_HERE, \"js\/toolbar.js\").read_text(encoding=\"utf-8\"),\n {\"id\": \"dt-toolbar-js\"})\n\n load_js(\n Path(_HERE, \"js\/tooltips.js\").read_text(encoding=\"utf-8\"),\n {\"id\": \"dt-tooltips-js\"})\n\n load_js(\n Path(_HERE, \"js\/jupyter-datatables.js\").read_text(encoding=\"utf-8\"),\n {\"id\": \"jupyter-datatables-js\"})\n\n __REPR__ = partialmethod(\n _repr_datatable_, options=options, classes=classes\n )\n\n _IS_INITIALIZED = True\n\n pd.DataFrame._repr_javascript_ = __REPR__","function_tokens":["def","init_datatables_mode","(","options",":","dict","=","None",",","classes",":","list","=","None",")",":","global","_IS_INITIALIZED","global","__REPR__","if","not","_IS_ENABLED",":","raise","Exception","(","\"Jupyter DataTables are disabled. Use `enable_datatables_mode()` instead.\"",")","# extensions to be loaded","extensions","=","config",".","defaults",".","extensions","require","(","\"d3\"",",","\"https:\/\/d3js.org\/d3.v5.min\"",")","require","(","\"d3-array\"",",","\"https:\/\/d3js.org\/d3-array.v2.min\"",")","require",".","config","(","*","*","{","\"paths\"",":","{","\"moment\"",":","\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/moment.js\/2.8.0\/moment\"",",","\"chartjs\"",":","\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/Chart.js\/2.8.0\/Chart\"","}",",","\"shim\"",":","{","\"chartjs\"",":","{","# enforce moment to be loaded before chartjs","\"deps\"",":","[","\"moment\"","]","}","}",",","}",")","# configure path to the datatables library using requireJS","libs","=","OrderedDict","(","{","# FIXME: minified version on prod","\"datatables.net\"",":","\"https:\/\/cdn.datatables.net\/1.10.18\/js\/jquery.dataTables\"","}",")","shim","=","OrderedDict","(","{","\"datatables.net\"",":","{","\"exports\"",":","\"$.fn.dataTable\"","}","}",")","bundles","=","OrderedDict","(",")","if","extensions",".","buttons",":","lib","=","\"datatables.net-buttons\"","libs","[","lib","]","=","\"https:\/\/cdn.datatables.net\/buttons\/1.5.6\/js\/dataTables.buttons.min\"","shim","[","lib","]","=","{","\"deps\"",":","[","\"datatables.net\"","]","}","# required to export Excel file, must be loaded first","libs","[","\"jszip\"","]","=","\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/jszip\/2.5.0\/jszip.min\"","bundles","[","lib","]","=","{","\"buttons.colvis\"",":","\"https:\/\/cdn.datatables.net\/buttons\/1.5.6\/js\/buttons.colVis.min\"",",","\"buttons.flash\"",":","\"https:\/\/cdn.datatables.net\/buttons\/1.5.6\/js\/buttons.flash.min\"",",","\"buttons.html5\"",":","\"https:\/\/cdn.datatables.net\/buttons\/1.5.6\/js\/buttons.html5.min\"",",","\"buttons.print\"",":","\"https:\/\/cdn.datatables.net\/buttons\/1.5.6\/js\/buttons.print.min\"",",","}","for","bundle",",","path","in","bundles","[","lib","]",".","items","(",")",":","libs","[","bundle","]","=","path","shim","[","bundle","]","=","{","\"deps\"",":","[","\"jszip\"",",","lib","]","}","# requirements for correct Buttons functionality","libs","[","\"pdfmake\"","]","=","\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/pdfmake\/0.1.36\/pdfmake.min\"","shim","[","\"pdfmake\"","]","=","{","\"deps\"",":","[","\"datatables.net\"","]","}","libs","[","\"vfsfonts\"","]","=","\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/pdfmake\/0.1.36\/vfs_fonts\"","shim","[","\"vfsfonts\"","]","=","{","\"deps\"",":","[","\"datatables.net\"","]","}","if","extensions",".","responsive",":","libs","[","\"datatables.responsive\"","]","=","(","\"https:\/\/cdn.datatables.net\/\"","\"responsive\/2.2.2\/js\/dataTables.responsive.min\"",")","# Responsive","if","extensions",".","scroller",":","libs","[","\"datatables.scroller\"","]","=","(","\"https:\/\/cdn.datatables.net\/\"","\"scroller\/2.0.0\/js\/dataTables.scroller.min\"",")","# Scroll","if","extensions",".","select",":","libs","[","\"datatables.select\"","]","=","(","\"https:\/\/cdn.datatables.net\/\"","\"select\/1.3.0\/js\/dataTables.select.min\"",")","# Select","require",".","config","(","paths","=","libs",",","shim","=","shim",")","# link stylesheets","link_css","(","\"https:\/\/cdn.datatables.net\/v\/dt\/\"","\"dt-1.10.18\/\"","# DataTables","\"af-2.3.3\/\"","# AutoFill","\"b-1.5.6\/\"","# Buttons","\"b-colvis-1.5.6\/\"","# Buttons - Column Visibility","\"b-flash-1.5.6\/\"","# Buttons - Flash","\"b-html5-1.5.6\/\"","# Buttons - HTML5","\"b-print-1.5.6\/\"","# Buttons - Print View","\"cr-1.5.0\/\"","# ColReorder","\"fc-3.2.5\/\"","# FixedColumns","\"fh-3.1.4\/\"","# FixedHeader","\"kt-2.5.0\/\"","# KeyTable","\"r-2.2.2\/\"","# Responsive","\"rg-1.1.0\/\"","# RowGroup","\"rr-1.2.4\/\"","# RowReorder","\"sc-2.0.0\/\"","# Scroll","\"sl-1.3.0\/\"","# Select","\"datatables.min.css\"",",","{","\"id\"",":","\"datatables.min.css\"","}",",",")","# load custom style","load_css","(","Path","(","_HERE",",","\"css\/jupyter-datatables.css\"",")",".","read_text","(","encoding","=","\"utf-8\"",")",",","{","\"id\"",":","\"jupyter-datatables-css\"","}",",",")","load_js","(","Path","(","_HERE",",","\"js\/config.js\"",")",".","read_text","(","encoding","=","\"utf-8\"",")",",","{","\"id\"",":","\"dt-config-js\"","}",")","load_js","(","Path","(","_HERE",",","\"js\/components.js\"",")",".","read_text","(","encoding","=","\"utf-8\"",")",",","{","\"id\"",":","\"dt-components-js\"","}",")","load_js","(","Path","(","_HERE",",","\"js\/graph-objects.js\"",")",".","read_text","(","encoding","=","\"utf-8\"",")",",","{","\"id\"",":","\"dt-graph-objects-js\"","}",")","load_js","(","Path","(","_HERE",",","\"js\/toolbar.js\"",")",".","read_text","(","encoding","=","\"utf-8\"",")",",","{","\"id\"",":","\"dt-toolbar-js\"","}",")","load_js","(","Path","(","_HERE",",","\"js\/tooltips.js\"",")",".","read_text","(","encoding","=","\"utf-8\"",")",",","{","\"id\"",":","\"dt-tooltips-js\"","}",")","load_js","(","Path","(","_HERE",",","\"js\/jupyter-datatables.js\"",")",".","read_text","(","encoding","=","\"utf-8\"",")",",","{","\"id\"",":","\"jupyter-datatables-js\"","}",")","__REPR__","=","partialmethod","(","_repr_datatable_",",","options","=","options",",","classes","=","classes",")","_IS_INITIALIZED","=","True","pd",".","DataFrame",".","_repr_javascript_","=","__REPR__"],"url":"https:\/\/github.com\/CermakM\/jupyter-datatables\/blob\/59b4880f926caaecb862477f3f1c2e7bbebb4939\/jupyter_datatables\/__init__.py#L84-L228"}
{"nwo":"CermakM\/jupyter-datatables","sha":"59b4880f926caaecb862477f3f1c2e7bbebb4939","path":"jupyter_datatables\/__init__.py","language":"python","identifier":"_repr_datatable_","parameters":"(self, options: dict = None, classes: list = None)","argument_list":"","return_statement":"return f\"\"\"\n (function() {{\n const sample_size = Number({sample_size}).toLocaleString();\n const adjusted = Boolean('{adjusted}' == 'True')\n\n const total = Number({len(self)}).toLocaleString();\n\n element.append($('<p>').text(\n `Sample size: ${{sample_size}} out of ${{total}} ${{ adjusted ? \"(adjusted)\" : \"\" }}`));\n }}());\n \"\"\"","docstring":"Return DataTable representation of pandas DataFrame.","docstring_summary":"Return DataTable representation of pandas DataFrame.","docstring_tokens":["Return","DataTable","representation","of","pandas","DataFrame","."],"function":"def _repr_datatable_(self, options: dict = None, classes: list = None):\n \"\"\"Return DataTable representation of pandas DataFrame.\"\"\"\n if options is None:\n options = {}\n options.update({\n k: v for k, v in config.defaults.options.__dict__.items()\n if not k.startswith(\"_\")\n })\n\n # column types\n options.update({'columnDefs': _get_columns_defs(self, options)})\n\n # pop buttons, we need to use them separately\n buttons = options.pop(\"buttons\", [])\n classes = classes if classes is not None else \" \".join(\n config.defaults.classes)\n\n script = \"\"\"\n const settings = await appendDataTable(`$$html`, $$options, $$buttons, element);\n\n console.debug(\"DataTable successfully created.\");\n \"\"\"\n\n df = self\n sample_size = config.defaults.sample_size or len(self)\n\n if sample_size > len(df):\n raise ValueError(\n f\"Sample size cannot be larger than length of the table: {sample_size} > {len(df)}\"\n )\n\n adjusted = False\n\n if config.defaults.limit is not None:\n n = len(self)\n\n # compute the sample size, it will be used for the data preview\n # to speed up computation\n if len(self) > config.defaults.limit:\n sample_size = getattr(config.defaults, 'sample_size', None) or min([\n n, _calculate_sample_size(n)\n ])\n\n idx = []\n # get 5% of extremes from each column to account for outliers in the sample\n # (if applicable)\n fraction = math.ceil(sample_size * 0.05)\n for col in self.columns:\n if self[col].dtype != \"O\":\n # for comfortable preview, take the first 10 samples (if applicable)\n idx.extend(self.index[:min(len(self), 10, sample_size)])\n idx.extend(self.nlargest(fraction, col).index)\n idx.extend(self.nsmallest(fraction, col).index)\n\n idx = set(idx)\n random_index = self.index.difference(idx)\n random_sample = sample_size - \\\n min(len(idx), sample_size) if len(random_index) else 0\n\n sample_index = pd.Index({\n *idx,\n *np.random.choice(random_index, size=random_sample, replace=False)\n })\n adjusted = len(sample_index) != sample_size\n\n df = self.loc[sample_index].sort_index()\n\n sample_size = len(df)\n\n sort = config.defaults.sort\n if sort == True or sort == 'index':\n df.sort_index(inplace=True)\n elif sort:\n df.sort_values(inplace=True)\n\n sha = hashlib.sha256(\n df.to_json().encode()\n )\n digest = sha.hexdigest()\n\n html = df.to_html(classes=classes, table_id=digest)\n\n execute_with_requirements(\n script,\n required=[\n \"base\/js\/events\",\n \"datatables.net\",\n \"d3\",\n \"chartjs\",\n \"dt-config\",\n \"dt-components\",\n \"dt-graph-objects\",\n \"dt-toolbar\",\n \"dt-tooltips\",\n \"jupyter-datatables\"\n ],\n html=html,\n options=json.dumps(options),\n buttons=buttons,\n )\n\n # return script which links the scrollbars event after save\n safe_script = \"\"\"\n setTimeout(() => {\n const table_id = '$$table_id';\n const table = $(`#${table_id}_wrapper`);\n \n let scrollHead = table.find('div.dataTables_scrollHead');\n let scrollBody = table.find('div.dataTables_scrollBody');\n \n $(scrollBody).on(\n 'scroll',\n (e) => {\n scrollHead.scrollLeft(scrollBody.scrollLeft());\n },\n );\n }, 200);\n \"\"\"\n\n safe_execute(safe_script, table_id=digest)\n\n return f\"\"\"\n (function() {{\n const sample_size = Number({sample_size}).toLocaleString();\n const adjusted = Boolean('{adjusted}' == 'True')\n\n const total = Number({len(self)}).toLocaleString();\n\n element.append($('<p>').text(\n `Sample size: ${{sample_size}} out of ${{total}} ${{ adjusted ? \"(adjusted)\" : \"\" }}`));\n }}());\n \"\"\"","function_tokens":["def","_repr_datatable_","(","self",",","options",":","dict","=","None",",","classes",":","list","=","None",")",":","if","options","is","None",":","options","=","{","}","options",".","update","(","{","k",":","v","for","k",",","v","in","config",".","defaults",".","options",".","__dict__",".","items","(",")","if","not","k",".","startswith","(","\"_\"",")","}",")","# column types","options",".","update","(","{","'columnDefs'",":","_get_columns_defs","(","self",",","options",")","}",")","# pop buttons, we need to use them separately","buttons","=","options",".","pop","(","\"buttons\"",",","[","]",")","classes","=","classes","if","classes","is","not","None","else","\" \"",".","join","(","config",".","defaults",".","classes",")","script","=","\"\"\"\n const settings = await appendDataTable(`$$html`, $$options, $$buttons, element);\n\n console.debug(\"DataTable successfully created.\");\n \"\"\"","df","=","self","sample_size","=","config",".","defaults",".","sample_size","or","len","(","self",")","if","sample_size",">","len","(","df",")",":","raise","ValueError","(","f\"Sample size cannot be larger than length of the table: {sample_size} > {len(df)}\"",")","adjusted","=","False","if","config",".","defaults",".","limit","is","not","None",":","n","=","len","(","self",")","# compute the sample size, it will be used for the data preview","# to speed up computation","if","len","(","self",")",">","config",".","defaults",".","limit",":","sample_size","=","getattr","(","config",".","defaults",",","'sample_size'",",","None",")","or","min","(","[","n",",","_calculate_sample_size","(","n",")","]",")","idx","=","[","]","# get 5% of extremes from each column to account for outliers in the sample","# (if applicable)","fraction","=","math",".","ceil","(","sample_size","*","0.05",")","for","col","in","self",".","columns",":","if","self","[","col","]",".","dtype","!=","\"O\"",":","# for comfortable preview, take the first 10 samples (if applicable)","idx",".","extend","(","self",".","index","[",":","min","(","len","(","self",")",",","10",",","sample_size",")","]",")","idx",".","extend","(","self",".","nlargest","(","fraction",",","col",")",".","index",")","idx",".","extend","(","self",".","nsmallest","(","fraction",",","col",")",".","index",")","idx","=","set","(","idx",")","random_index","=","self",".","index",".","difference","(","idx",")","random_sample","=","sample_size","-","min","(","len","(","idx",")",",","sample_size",")","if","len","(","random_index",")","else","0","sample_index","=","pd",".","Index","(","{","*","idx",",","*","np",".","random",".","choice","(","random_index",",","size","=","random_sample",",","replace","=","False",")","}",")","adjusted","=","len","(","sample_index",")","!=","sample_size","df","=","self",".","loc","[","sample_index","]",".","sort_index","(",")","sample_size","=","len","(","df",")","sort","=","config",".","defaults",".","sort","if","sort","==","True","or","sort","==","'index'",":","df",".","sort_index","(","inplace","=","True",")","elif","sort",":","df",".","sort_values","(","inplace","=","True",")","sha","=","hashlib",".","sha256","(","df",".","to_json","(",")",".","encode","(",")",")","digest","=","sha",".","hexdigest","(",")","html","=","df",".","to_html","(","classes","=","classes",",","table_id","=","digest",")","execute_with_requirements","(","script",",","required","=","[","\"base\/js\/events\"",",","\"datatables.net\"",",","\"d3\"",",","\"chartjs\"",",","\"dt-config\"",",","\"dt-components\"",",","\"dt-graph-objects\"",",","\"dt-toolbar\"",",","\"dt-tooltips\"",",","\"jupyter-datatables\"","]",",","html","=","html",",","options","=","json",".","dumps","(","options",")",",","buttons","=","buttons",",",")","# return script which links the scrollbars event after save","safe_script","=","\"\"\"\n setTimeout(() => {\n const table_id = '$$table_id';\n const table = $(`#${table_id}_wrapper`);\n \n let scrollHead = table.find('div.dataTables_scrollHead');\n let scrollBody = table.find('div.dataTables_scrollBody');\n \n $(scrollBody).on(\n 'scroll',\n (e) => {\n scrollHead.scrollLeft(scrollBody.scrollLeft());\n },\n );\n }, 200);\n \"\"\"","safe_execute","(","safe_script",",","table_id","=","digest",")","return","f\"\"\"\n (function() {{\n const sample_size = Number({sample_size}).toLocaleString();\n const adjusted = Boolean('{adjusted}' == 'True')\n\n const total = Number({len(self)}).toLocaleString();\n\n element.append($('<p>').text(\n `Sample size: ${{sample_size}} out of ${{total}} ${{ adjusted ? \"(adjusted)\" : \"\" }}`));\n }}());\n \"\"\""],"url":"https:\/\/github.com\/CermakM\/jupyter-datatables\/blob\/59b4880f926caaecb862477f3f1c2e7bbebb4939\/jupyter_datatables\/__init__.py#L231-L362"}
{"nwo":"CermakM\/jupyter-datatables","sha":"59b4880f926caaecb862477f3f1c2e7bbebb4939","path":"jupyter_datatables\/__init__.py","language":"python","identifier":"_get_columns_defs","parameters":"(df: pd.DataFrame, options: dict = None)","argument_list":"","return_statement":"return col_defs","docstring":"Create column defs to be used by DataTable constructor.","docstring_summary":"Create column defs to be used by DataTable constructor.","docstring_tokens":["Create","column","defs","to","be","used","by","DataTable","constructor","."],"function":"def _get_columns_defs(df: pd.DataFrame, options: dict = None):\n \"\"\"Create column defs to be used by DataTable constructor.\"\"\"\n options = options or {}\n\n idx_cols = df.index.names\n col_defs = list(options.get('columnDefs', []))\n\n for i, dtype in enumerate(map(str, df.dtypes)):\n col_idx = i + len(idx_cols)\n col_def = {}\n\n append = True\n for d in col_defs:\n if d.get('targets', None) == col_idx:\n append = False\n col_def = d\n break\n\n col_def.update(type=dtype, targets=col_idx)\n if append:\n col_defs.append(col_def)\n\n return col_defs","function_tokens":["def","_get_columns_defs","(","df",":","pd",".","DataFrame",",","options",":","dict","=","None",")",":","options","=","options","or","{","}","idx_cols","=","df",".","index",".","names","col_defs","=","list","(","options",".","get","(","'columnDefs'",",","[","]",")",")","for","i",",","dtype","in","enumerate","(","map","(","str",",","df",".","dtypes",")",")",":","col_idx","=","i","+","len","(","idx_cols",")","col_def","=","{","}","append","=","True","for","d","in","col_defs",":","if","d",".","get","(","'targets'",",","None",")","==","col_idx",":","append","=","False","col_def","=","d","break","col_def",".","update","(","type","=","dtype",",","targets","=","col_idx",")","if","append",":","col_defs",".","append","(","col_def",")","return","col_defs"],"url":"https:\/\/github.com\/CermakM\/jupyter-datatables\/blob\/59b4880f926caaecb862477f3f1c2e7bbebb4939\/jupyter_datatables\/__init__.py#L365-L387"}
{"nwo":"CermakM\/jupyter-datatables","sha":"59b4880f926caaecb862477f3f1c2e7bbebb4939","path":"jupyter_datatables\/__init__.py","language":"python","identifier":"_calculate_sample_size","parameters":"(n, ci: float = 0.975, e: float = 0.02, p: float = 0.5)","argument_list":"","return_statement":"return _smart_ceil(u \/ (1 + u * math.pow(n, -1)))","docstring":"Calculate representative sample size.\n\n :param ci: float, confidence interval, default = 0.975\n :param e: float, margin of error, default = 0.02\n :param p: float, population proportion, default = 0.5","docstring_summary":"Calculate representative sample size.","docstring_tokens":["Calculate","representative","sample","size","."],"function":"def _calculate_sample_size(n, ci: float = 0.975, e: float = 0.02, p: float = 0.5) -> int:\n \"\"\"Calculate representative sample size.\n\n :param ci: float, confidence interval, default = 0.975\n :param e: float, margin of error, default = 0.02\n :param p: float, population proportion, default = 0.5\n \"\"\"\n try:\n from scipy import stats as st\n except ImportError:\n return math.sqrt(n)\n\n z = st.norm.ppf(1 - (1 - ci) \/ 2)\n u = z**2 * p * (1 - p) \/ e**2\n\n return _smart_ceil(u \/ (1 + u * math.pow(n, -1)))","function_tokens":["def","_calculate_sample_size","(","n",",","ci",":","float","=","0.975",",","e",":","float","=","0.02",",","p",":","float","=","0.5",")","->","int",":","try",":","from","scipy","import","stats","as","st","except","ImportError",":","return","math",".","sqrt","(","n",")","z","=","st",".","norm",".","ppf","(","1","-","(","1","-","ci",")","\/","2",")","u","=","z","**","2","*","p","*","(","1","-","p",")","\/","e","**","2","return","_smart_ceil","(","u","\/","(","1","+","u","*","math",".","pow","(","n",",","-","1",")",")",")"],"url":"https:\/\/github.com\/CermakM\/jupyter-datatables\/blob\/59b4880f926caaecb862477f3f1c2e7bbebb4939\/jupyter_datatables\/__init__.py#L390-L405"}
{"nwo":"CermakM\/jupyter-datatables","sha":"59b4880f926caaecb862477f3f1c2e7bbebb4939","path":"jupyter_datatables\/__init__.py","language":"python","identifier":"_smart_ceil","parameters":"(x: typing.Union[int, float], order: int = None)","argument_list":"","return_statement":"return int(x if x % mod == 0 else x + mod - x % mod)","docstring":"Smart ceil to the nearest round integer.\n\n The 'nearest' is chosen based on the order of the given number","docstring_summary":"Smart ceil to the nearest round integer.","docstring_tokens":["Smart","ceil","to","the","nearest","round","integer","."],"function":"def _smart_ceil(x: typing.Union[int, float], order: int = None) -> int:\n \"\"\"Smart ceil to the nearest round integer.\n\n The 'nearest' is chosen based on the order of the given number\n \"\"\"\n order = int(order) if order else len(str(math.ceil(x)))\n if order <= 0:\n raise ValueError(\"`order` must be integer >= 0\")\n mod = math.pow(10, min([order, 3]))\n\n return int(x if x % mod == 0 else x + mod - x % mod)","function_tokens":["def","_smart_ceil","(","x",":","typing",".","Union","[","int",",","float","]",",","order",":","int","=","None",")","->","int",":","order","=","int","(","order",")","if","order","else","len","(","str","(","math",".","ceil","(","x",")",")",")","if","order","<=","0",":","raise","ValueError","(","\"`order` must be integer >= 0\"",")","mod","=","math",".","pow","(","10",",","min","(","[","order",",","3","]",")",")","return","int","(","x","if","x","%","mod","==","0","else","x","+","mod","-","x","%","mod",")"],"url":"https:\/\/github.com\/CermakM\/jupyter-datatables\/blob\/59b4880f926caaecb862477f3f1c2e7bbebb4939\/jupyter_datatables\/__init__.py#L408-L418"}
|