-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2068 from jupyter-naas/2066-python-get-release-da…
…tes-from-pypi feat: python release dates
- Loading branch information
Showing
1 changed file
with
314 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,314 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "72a216be-10dc-4c1d-a3fa-a9ce992d1c27", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "25f38b3f-5cb5-4700-ba73-fb24646dd3ca", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"# PyPI - Get release dates from package\n", | ||
"<a href=\"https://app.naas.ai/user-redirect/naas/downloader?url=https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/PyPI/PyPI_Get_number_of_downloads_any_package.ipynb\" target=\"_parent\"><img src=\"https://naasai-public.s3.eu-west-3.amazonaws.com/Open_in_Naas_Lab.svg\"/></a><br><br><a href=\"https://bit.ly/3JyWIk6\">Give Feedbacks</a> | <a href=\"https://app.naas.ai/user-redirect/naas/downloader?url=https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/Naas/Naas_Start_data_product.ipynb\" target=\"_parent\">Generate Data Product</a>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "4da75a0d-6fba-4115-8667-429956d5ffa0", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Tags:** #pypi #downloads #package #operations #analytics #plotly #html #csv #image #png" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "ab9c2b57-cd13-4086-8a8c-676006cdaebf", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Author:** [Mardiat-Iman](https://www.linkedin.com/in/mardiat-iman-ibrahim-imam-726027262)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "963551f0-c489-4f73-b4d0-7699892623bf", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Last update:** 2023-07-27 (Created: 2023-07-27)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "naas-description", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [ | ||
"description" | ||
] | ||
}, | ||
"source": [ | ||
"**Description:** This notebook get the release dates a package from the Python Package Index (PyPI) and plot a Barchart and Scatter Plot to display the release by month. \n", | ||
"\n", | ||
"NB: We have noticed that sometimes not all versions are accessible via this endpoint in comparison with the website. Please let us know if you manage to find a solution to this issue, we would appreciate." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "922c974a-a4f5-41b1-af0e-83db3798dee4", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Input" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "ff478c09-fc56-440d-9aaf-4b6f018f3395", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Import libraries" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "93b77ec7-1f35-4f2e-9770-2bfef3ee3752", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import requests\n", | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import matplotlib.dates as mdates\n", | ||
"from dateutil.parser import parse" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "50b727c1-81d4-4976-8a14-2ff4cfb418b6", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Setup variables" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "2c16f635-fbb6-4921-9777-4a4013dcfa37", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Allow the user to input the package name\n", | ||
"user_input = input(\"Enter the package name to fetch release dates from PyPI: \")\n", | ||
"\n", | ||
"# Extract the package name (remove version number if present)\n", | ||
"package_name = user_input.split()[0]\n", | ||
"print('Package:', package_name)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "36f9d891-dd69-49e0-9288-ba6cf1566198", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Model" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "2beb4527-eb8c-40c3-8e99-a49c80018e3f", | ||
"metadata": {}, | ||
"source": [ | ||
"### Get releases dates" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "22bcc4b1-bb81-4053-81fe-19675d4c0f19", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"def get_release_dates(package_name):\n", | ||
" # Init \n", | ||
" release_dates = []\n", | ||
" \n", | ||
" # Request\n", | ||
" url = f\"https://pypi.org/pypi/{package_name}/json\"\n", | ||
" response = requests.get(url)\n", | ||
" \n", | ||
" # Response\n", | ||
" if response.status_code == 200:\n", | ||
" data = response.json()\n", | ||
" releases = data.get(\"releases\", {})\n", | ||
" release_dates = [parse(release_info[0][\"upload_time\"]) for release_info in releases.values() if release_info]\n", | ||
" if not releases:\n", | ||
" print(f\"No releases found for package '{package_name}'.\")\n", | ||
" else:\n", | ||
" for version, release_info in releases.items():\n", | ||
" if release_info and isinstance(release_info, list):\n", | ||
" latest_release = max(release_info, key=lambda x: x[\"upload_time\"])\n", | ||
" release_date = latest_release[\"upload_time\"]\n", | ||
" print(f\"Version: {version}, Release Date: {release_date}\")\n", | ||
" else:\n", | ||
" print(f\"Error: Package '{package_name}' not found or error in fetching data.\")\n", | ||
" print(\"Raw JSON Response:\", response.text)\n", | ||
" return release_dates\n", | ||
"\n", | ||
"# Call the function with the package name to get the release dates\n", | ||
"release_dates = get_release_dates(package_name)\n", | ||
"print(\"Release dates found:\", len(release_dates))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "b7077755-36d0-4e95-a581-86c5764cc383", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Output" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "d0b568d5-3904-4dbd-b1e8-476ad4ae9a61", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Plotting a Barchart and Scatter Plot for the release dates" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "70713839-101a-4d57-af31-3340bc13b857", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Check if release dates were obtained successfully\n", | ||
"if release_dates:\n", | ||
" # 1. Sort release dates in ascending order\n", | ||
" release_dates.sort()\n", | ||
"\n", | ||
" # 2. Bar Chart - Releases per Month\n", | ||
" release_months = [date.strftime('%Y-%m') for date in release_dates]\n", | ||
" unique_months, month_counts = np.unique(release_months, return_counts=True)\n", | ||
" unique_months = [parse(month + '-01') for month in unique_months] # Convert back to datetime objects\n", | ||
" fig, ax = plt.subplots(figsize=(30, 15), constrained_layout=True)\n", | ||
" ax.set(title=f\"Bar Chart - Releases per Month for {package_name.capitalize()}\")\n", | ||
" ax.bar(unique_months, month_counts, color=\"tab:red\", width=20)\n", | ||
" ax.xaxis.set_major_locator(mdates.MonthLocator())\n", | ||
" ax.xaxis.set_major_formatter(mdates.DateFormatter(\"%b %Y\"))\n", | ||
" ax.set_xlabel(\"Month\")\n", | ||
" ax.set_ylabel(\"Number of Releases\")\n", | ||
" plt.xticks(rotation=45, ha=\"right\")\n", | ||
" ax.spines[[\"top\", \"right\"]].set_visible(False)\n", | ||
" plt.savefig(\"bar_chart.png\") # Save the figure\n", | ||
" plt.show()\n", | ||
"else:\n", | ||
" print(\"No release dates found for the specified package.\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "d0396b1b-00d3-45c0-af1d-af07daa6f07c", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Save the figure" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "52b4015d-f6ab-4bb8-a0a5-f7ac5d824bbc", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"fig.savefig(\"bar_chart.png\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.6" | ||
}, | ||
"naas": { | ||
"notebook_id": "8da37e2aaf56e0b26e7ee696dc16f492d45a2e204454444b107880aac4b15824", | ||
"notebook_path": "PyPI/PyPI_Get_number_of_downloads_any_package.ipynb" | ||
}, | ||
"papermill": { | ||
"default_parameters": {}, | ||
"environment_variables": {}, | ||
"parameters": {}, | ||
"version": "2.3.4" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |