Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion .github/workflows/deploy-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ on:
types:
- published

permissions:
# Give the GITHUB_TOKEN write permission to open a PR with the changes to the switcher.json file.
contents: write
pull-requests: write

jobs:
run:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: main
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Conengmo and @merschformann do you need this? That means any change in the code won't be tested in the docs until it is on main.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is here to make sure that docs are only ever released from the main branch, thus, reflecting docs in the state the main branch is in. A push to main is also a trigger of this workflow.
It does not feel fully necessary, if the events would change to only be pushes to main and release (this one could technically result in none-main being released if release is defined on branch). I am not sure what pull_request is for. It is just explicit about limiting docs releases to be coming from main.
(just my two cents)

Maybe the triggers of the workflow and the checkout should be revised? 🤔

cc @Conengmo


- name: Setup Micromamba env
uses: mamba-org/setup-micromamba@v1
Expand All @@ -38,13 +45,29 @@ jobs:
make clean html linkcheck
popd

- name: Update switcher and latest version
if: ${{ github.event_name == 'release' }}
run: |
python docs/update_switcher.py --version ${{ github.ref_name }}

- name: Create PR
if: ${{ github.event_name == 'release' }}
uses: peter-evans/create-pull-request@v3
with:
commit-message: "docs: Update switcher.json for ${{ github.ref_name }}"
title: "docs: Update switcher.json for ${{ github.ref_name }}"
body: "This PR updates the switcher.json file."
branch: "docs/update-switcher-${{ github.ref_name }}"
base: "main"
labels: "documentation"

- name: Publish to Github Pages on main
if: ${{ github.ref == 'refs/heads/main' }}
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: docs/_build/html/
destination_dir: latest
destination_dir: dev
keep_files: false

- name: Publish to Github Pages on release
Expand Down
6 changes: 5 additions & 1 deletion docs/_static/switcher.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[
{
"version": "dev",
"url": "https://python-visualization.github.io/folium/dev/"
},
{
"version": "latest",
"url": "https://python-visualization.github.io/folium/latest/"
"url": "https://python-visualization.github.io/folium/v0.16.0/"
},
{
"version": "v0.16.0",
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
# documentation.
html_theme_options = {
"switcher": {
"json_url": "https://python-visualization.github.io/folium/latest/_static/switcher.json",
"version_match": "latest" if ".dev" in version else version,
"json_url": "https://python-visualization.github.io/folium/dev/_static/switcher.json",
"version_match": "dev" if ".dev" in version else version,
},
"navbar_start": ["navbar-logo", "version-switcher"],
"footer_start": ["version", "copyright", "sphinx-version", "theme-version"],
Expand Down
56 changes: 56 additions & 0 deletions docs/update_switcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
This script is used to update switcher.json on docs releases. It adds the new version to
the list of versions and sets the latest version to the new version.
"""

import argparse
import json
import os


def main():
# Define CLI arguments
parser = argparse.ArgumentParser(description="Update switcher.json")
parser.add_argument(
"--version", "-v", required=True, type=str, help="The new version to add"
)
args = parser.parse_args()

# Setup path to switcher.json (relative to this script) and load it
switcher_path = os.path.join(os.path.dirname(__file__), "_static", "switcher.json")
with open(switcher_path) as f:
switcher = json.load(f)

# Find index of 'latest' entry
latest_index = None
for i, version in enumerate(switcher):
if version["version"] == "latest":
latest_index = i
break
if latest_index is None:
raise ValueError("'latest' version not found in switcher.json")

# Add the new version to the list of versions (we always insert it after latest)
new_version = {
"version": args.version,
"url": f"https://python-visualization.github.io/folium/{args.version}/",
}

# Update the latest version
switcher[latest_index]["url"] = new_version["url"]

# Make sure version is unique
if any(version["version"] == args.version for version in switcher):
print(
f"Version {args.version} already exists in switcher.json. Not adding it again."
)
else:
switcher.insert(latest_index + 1, new_version)

# Write the updated switcher.json
with open(switcher_path, "w") as f:
json.dump(switcher, f, indent=2)


if __name__ == "__main__":
main()