|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": { |
| 7 | + "ExecuteTime": { |
| 8 | + "end_time": "2025-02-08T19:31:35.646100Z", |
| 9 | + "start_time": "2025-02-08T19:31:34.663998Z" |
| 10 | + }, |
| 11 | + "vscode": { |
| 12 | + "languageId": "python" |
| 13 | + } |
| 14 | + }, |
| 15 | + "outputs": [ |
| 16 | + { |
| 17 | + "data": { |
| 18 | + "text/html": [ |
| 19 | + "<div>\n", |
| 20 | + "<style scoped>\n", |
| 21 | + " .dataframe tbody tr th:only-of-type {\n", |
| 22 | + " vertical-align: middle;\n", |
| 23 | + " }\n", |
| 24 | + "\n", |
| 25 | + " .dataframe tbody tr th {\n", |
| 26 | + " vertical-align: top;\n", |
| 27 | + " }\n", |
| 28 | + "\n", |
| 29 | + " .dataframe thead th {\n", |
| 30 | + " text-align: right;\n", |
| 31 | + " }\n", |
| 32 | + "</style>\n", |
| 33 | + "<table border=\"1\" class=\"dataframe\">\n", |
| 34 | + " <thead>\n", |
| 35 | + " <tr style=\"text-align: right;\">\n", |
| 36 | + " <th></th>\n", |
| 37 | + " <th>ecosystem</th>\n", |
| 38 | + " <th>type</th>\n", |
| 39 | + " <th>total_affected</th>\n", |
| 40 | + " <th>most_affected_package</th>\n", |
| 41 | + " <th>peak_attack_year</th>\n", |
| 42 | + " <th>trend_data</th>\n", |
| 43 | + " </tr>\n", |
| 44 | + " </thead>\n", |
| 45 | + " <tbody>\n", |
| 46 | + " <tr>\n", |
| 47 | + " <th>0</th>\n", |
| 48 | + " <td>CRAN</td>\n", |
| 49 | + " <td>Vulnerability</td>\n", |
| 50 | + " <td>10</td>\n", |
| 51 | + " <td>readxl</td>\n", |
| 52 | + " <td>2023</td>\n", |
| 53 | + " <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0]</td>\n", |
| 54 | + " </tr>\n", |
| 55 | + " </tbody>\n", |
| 56 | + "</table>\n", |
| 57 | + "</div>" |
| 58 | + ], |
| 59 | + "text/plain": [ |
| 60 | + " ecosystem type total_affected most_affected_package \\\n", |
| 61 | + "0 CRAN Vulnerability 10 readxl \n", |
| 62 | + "\n", |
| 63 | + " peak_attack_year trend_data \n", |
| 64 | + "0 2023 [0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0] " |
| 65 | + ] |
| 66 | + }, |
| 67 | + "execution_count": 1, |
| 68 | + "metadata": {}, |
| 69 | + "output_type": "execute_result" |
| 70 | + } |
| 71 | + ], |
| 72 | + "source": [ |
| 73 | + "import pandas as pd\n", |
| 74 | + "\n", |
| 75 | + "df = pd.read_csv(\"../../data/osv/processed/osv_ecosystem_summary.csv\")\n", |
| 76 | + "df.head(1)" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": null, |
| 82 | + "metadata": { |
| 83 | + "vscode": { |
| 84 | + "languageId": "python" |
| 85 | + } |
| 86 | + }, |
| 87 | + "outputs": [], |
| 88 | + "source": [ |
| 89 | + "from great_tables import GT, md, html, nanoplot_options\n", |
| 90 | + "import pandas as pd\n", |
| 91 | + "import numpy as np\n", |
| 92 | + "import ast\n", |
| 93 | + "\n", |
| 94 | + "\n", |
| 95 | + "# Ensure `trend_data` is properly formatted as a comma-separated string\n", |
| 96 | + "df[\"trend_data\"] = df[\"trend_data\"].apply(\n", |
| 97 | + " lambda x: \", \".join(map(str, np.array(ast.literal_eval(x))))\n", |
| 98 | + " if isinstance(x, str) and x.startswith(\"[\")\n", |
| 99 | + " else \", \".join(map(str, x))\n", |
| 100 | + " if isinstance(x, (list, np.ndarray))\n", |
| 101 | + " else str(x)\n", |
| 102 | + ")\n", |
| 103 | + "\n", |
| 104 | + "df[\"icon\"] = df[\"type\"].replace({\"Vulnerability\": \"bug\", \"Malicious Code\": \"skull\"})\n", |
| 105 | + "\n", |
| 106 | + "color_map = {\"bug\": \"purple\", \"skull\": \"red\"}\n", |
| 107 | + "\n", |
| 108 | + "# Create a sorted table DataFrame with the desired columns\n", |
| 109 | + "table_df = df[\n", |
| 110 | + " [\"ecosystem\", \"icon\", \"total_affected\", \"peak_attack_year\", \"trend_data\"]\n", |
| 111 | + "].sort_values([\"total_affected\", \"ecosystem\"], ascending=[False, True])\n", |
| 112 | + "\n", |
| 113 | + "# Generate the Great Table\n", |
| 114 | + "gt_table = (\n", |
| 115 | + " GT(table_df)\n", |
| 116 | + " .tab_header(\n", |
| 117 | + " title=md(\"**OSV Security Trends**\"),\n", |
| 118 | + " subtitle=md(\n", |
| 119 | + " \"_Malicious Code & Vulnerability Insights Across Software Supply Chains_\"\n", |
| 120 | + " ),\n", |
| 121 | + " )\n", |
| 122 | + " .tab_stub(rowname_col=\"ecosystem\")\n", |
| 123 | + " .tab_stubhead(label=\"Ecosystem\")\n", |
| 124 | + " .tab_source_note(source_note=md(\"*Year Trends from 2014-2024*\"))\n", |
| 125 | + " .tab_source_note(\n", |
| 126 | + " source_note=md(\n", |
| 127 | + " \"Data sourced from [OSV.dev](https://osv.dev) (Open Source Vulnerability) and analyzed for vulnerability & malicious code trends. Covers PyPI, npm, Maven, Go, RubyGems, NuGet, Packagist, Pub, CRAN, Hackage, Hex, and crates.io. Last updated: February 2025.\"\n", |
| 128 | + " )\n", |
| 129 | + " )\n", |
| 130 | + " .tab_source_note(\n", |
| 131 | + " source_note=md(\"**Legend:** Bug = Vulnerability | Skull = Malicious Code\")\n", |
| 132 | + " )\n", |
| 133 | + " .tab_stubhead(label=\"Ecosystem\")\n", |
| 134 | + " .cols_label(\n", |
| 135 | + " ecosystem=\"Ecosystem\",\n", |
| 136 | + " icon=\"Type\",\n", |
| 137 | + " total_affected=\"Total\",\n", |
| 138 | + " peak_attack_year=\"Peak\",\n", |
| 139 | + " trend_data=\"Year Trend\",\n", |
| 140 | + " )\n", |
| 141 | + " .fmt_nanoplot(\n", |
| 142 | + " \"trend_data\",\n", |
| 143 | + " plot_type=\"bar\",\n", |
| 144 | + " reference_line=\"mean\",\n", |
| 145 | + " options=nanoplot_options(\n", |
| 146 | + " data_bar_stroke_color=\"black\",\n", |
| 147 | + " data_bar_stroke_width=2,\n", |
| 148 | + " data_bar_fill_color=\"darkgray\",\n", |
| 149 | + " reference_line_color=\"pink\",\n", |
| 150 | + " ),\n", |
| 151 | + " )\n", |
| 152 | + " .fmt_number(columns=\"total_affected\", sep_mark=\",\", decimals=0)\n", |
| 153 | + " .cols_align(align=\"left\", columns=[\"ecosystem\"])\n", |
| 154 | + " .cols_align(\n", |
| 155 | + " align=\"center\",\n", |
| 156 | + " columns=[\"icon\", \"total_affected\", \"peak_attack_year\", \"trend_data\"],\n", |
| 157 | + " )\n", |
| 158 | + " .fmt_icon(columns=\"icon\", fill_color=color_map)\n", |
| 159 | + ")\n", |
| 160 | + "\n", |
| 161 | + "\n", |
| 162 | + "# Generate the raw HTML from the table\n", |
| 163 | + "html_output = gt_table.as_raw_html()\n", |
| 164 | + "\n", |
| 165 | + "# Save it to an HTML file\n", |
| 166 | + "with open(\n", |
| 167 | + " \"../../data/osv/processed/osv_security_trends.html\", \"w\", encoding=\"utf-8\"\n", |
| 168 | + ") as f:\n", |
| 169 | + " f.write(html_output)\n", |
| 170 | + "\n", |
| 171 | + "# Display the table\n", |
| 172 | + "gt_table" |
| 173 | + ] |
| 174 | + } |
| 175 | + ], |
| 176 | + "metadata": { |
| 177 | + "kernelspec": { |
| 178 | + "display_name": "Python 3 (ipykernel)", |
| 179 | + "language": "python", |
| 180 | + "name": "python3" |
| 181 | + }, |
| 182 | + "language_info": { |
| 183 | + "name": "plaintext" |
| 184 | + } |
| 185 | + }, |
| 186 | + "nbformat": 4, |
| 187 | + "nbformat_minor": 2 |
| 188 | +} |
0 commit comments