Skip to main content

A general-purpose library to add Webmentions support to your website

Project description

Webmentions

A general-purpose library to add Webmentions support to your website.

This library implements support for Webmentions, a W3C recommendation for federated mentions, comments and reactions on the Web.

The underlying mechanism of Webmentions is relatively simple:

  • Alice publishes an article A1 on her blog A

  • Bob reads A1 and mentions it in an article B1 on his blog B

  • If both A and B support Webmentions, then:

    • A exposes an HTTP endpoint to receive mention notifications, and that endpoint is advertised on A1 as a rel="webmention" link in the page or a Link header in the HTTP response.

    • When Bob saves B1, its blog will scan for links in the page, discover which services expose a Webmention endpoint, and send a Webmention to A to notify them of the mention.

    • A will receive the notification and, if it accepts comments from B, the comment or the reaction will be rendered as a response on A1.

Installation

For the base installation:

pip install webmentions

Usage

This library provides the bindings to send and process Webmentions. It is agnostic about how the Webmentions are stored and rendered, but it provides a few helpers for common frameworks.

Some examples are provided under the examples directory.

SQLAlchemy + FastAPI

pip install "webmentions[db,fastapi]"
from fastapi import FastAPI
from webmentions import WebmentionsHandler
from webmentions.storage.adapters.db import init_db_storage
from webmentions.server.adapters.fastapi import bind_webmentions_endpoint

app = FastAPI()

# Replace this with your own database URL, or an existing engine.
# Extra arguments passed to init_db_storage will be passed to create_engine
storage = init_db_storage(engine="sqlite:////tmp/webmentions.db")

# Your Webmentions handler
handler = WebmentionsHandler(
    storage=storage,
    # This should match the public base URL of your site
    base_url=f"https://example.com",
)

# ...Initialize your Web app here...

# Bind a POST /webmention to your FastAPI app
bind_webmentions_endpoint(app, handler)

SQLAlchemy + Flask

pip install "webmentions[db,flask]"
from flask import Flask
from webmentions import WebmentionsHandler
from webmentions.storage.adapters.db import init_db_storage
from webmentions.server.adapters.flask import bind_webmentions_endpoint

app = Flask(__name__)

# Replace this with your own database URL, or an existing engine.
# Extra arguments passed to init_db_storage will be passed to create_engine
storage = init_db_storage(engine="sqlite:////tmp/webmentions.db")

# Your Webmentions handler
handler = WebmentionsHandler(
    storage=storage,
    # This should match the public base URL of your site
    base_url=f"https://example.com",
)

# ...Initialize your Web app here...

# Bind a POST /webmention to your Flask app
bind_webmentions_endpoint(app, handler)

Tests

pip install -e ".[tests]"
pytest tests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

webmentions-0.1.1.tar.gz (26.3 kB view details)

Uploaded Source

File details

Details for the file webmentions-0.1.1.tar.gz.

File metadata

  • Download URL: webmentions-0.1.1.tar.gz
  • Upload date:
  • Size: 26.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for webmentions-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4eda37f5ba8218f24fcd864e3d1c326756201ce4c59cb84c1b9aaaf8dbf2bbb4
MD5 d888aeb554a065ae48356455da8c6f95
BLAKE2b-256 adca78698f2f31ecf3c1fbb9122caed418b32095abd1d0f3d7847bef5a12da7d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page