Skip to content

🐍 Use from Python

PyPI PyPI - Python Version

You can use this toolkit to sign and publish Nanopublications from Python with the nanopub-sign pip package.

Build a Nanopublication

This package takes an already prepared Nanopublication RDF string as input. If you want to build a Nanopublication programmatically, use the nanopub pip package. You can then feed the serialized RDF of the built Nanopub to this package functions.

πŸ“₯️ Install

Install the package with pip or pipx:

pip install nanopub-sign

✍️ Sign Nanopubs

This process involves signing a Nanopublication RDF string using a specified RSA private key passed through the profile. The signing operation ensures that the Nanopub is authentically created by the holder of the private key.

Get a private key

You can easily create and register a new private key on the playground page after login with your ORCID.

sign.py
from nanopub_sign import Nanopub, NpProfile

# Change the RDF and private key as you wish
rdf_str = """@prefix : <http://purl.org/nanopub/temp/mynanopub#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix dc: <http://purl.org/dc/terms/> .
@prefix pav: <http://purl.org/pav/> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix np: <http://www.nanopub.org/nschema#> .
@prefix npx: <http://purl.org/nanopub/x/> .
@prefix ex: <http://example.org/> .

:Head {
    : np:hasAssertion :assertion ;
        np:hasProvenance :provenance ;
        np:hasPublicationInfo :pubinfo ;
        a np:Nanopublication .
}

:assertion {
    ex:mosquito ex:transmits ex:malaria .
}

:provenance {
    :assertion prov:hadPrimarySource <http://dx.doi.org/10.3233/ISU-2010-0613> .
}

:pubinfo {
    : a npx:ExampleNanopub .
}"""
private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCjY1gsFxmak6SOCouJPuEzHNForkqFhgfHE3aAIAx+Y5q6UDEDM9Q0EksheNffJB4iPqsAfiFpY0ARQY92K5r8P4+a78eu9reYrb2WxZb1qPJmvR7XZ6sN1oHD7dd/EyQoJmQsmOKdrqaLRbzR7tZrf52yvKkwNWXcIVhW8uxe7iUgxiojZpW9srKoK/qFRpaUZSKn7Z/zgtDH9FJkYbBsGPDMqp78Kzt+sJb+U2W+wCSSy34jIUxx6QRbzvn6u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7nkntwewmf3i7V8lT8ZTwVRsmObWU60ySJ8qKuwoBQodki2VX12NpMN1wgWe3qUUlr6gLJU4xAoGAet6nD3QKwk6TTmcGVfSWOzvpaDEzGkXjCLaxLKh9GreM/OE+h5aN2gUoFeQapG5rUwI/7Qq0xiLbRXw+OmfAoV2XKv7iI8DjdIh0F06mlEAwQ/B0CpbqkuuxphIbchtdcz/5ra233r3BMNIqBl3VDDVoJlgHPg9msOTRy13lFqc="

# Instantiate a nanopub profile (ORCID and name are optional)
profile = NpProfile(
    private_key=private_key,
    orcid_id="https://orcid.org/0000-0000-0000-0000",
    name="Your Name",
)

# Sign a nanopub
np = Nanopub(rdf_str)
np = np.sign(profile=profile)
print("Signed info dict:", np.info())

Run the script:

python sign.py

πŸ“¬ Publish Nanopubs

Signed Nanopubs can be published to a Nanopub server. This makes the Nanopub accessible to others in the network.

Use the publish function on a Nanopub, the 2 arguments are optional:

  • πŸ”‘ profile is required if you want to also sign the nanopub, it is not required if you provide a signed nanopub
  • 🧫 If the server_url is null it will be published to the test server
from nanopub_sign import Nanopub, NpProfile, get_np_server

np = Nanopub(rdf_str).publish(profile=profile, server_url=None)
print("Published info dict:", np.info())
print(np.rdf())

Provide the nanopub signed or unsigned

  • If signed nanopub and profile not provided, we publish the signed nanopub as it is
  • If signed nanopub and profile provided, we re-sign the nanopub (signature triples are updated)
  • If unsigned nanopub and profile provided, we sign the nanopub
  • If unsigned nanopub and profile not provided, we throw an error

πŸ§ͺ Test and production servers

If the provided server_url is empty, the nanopub will be published to the test server. In this case the nanopub will not be available at https://w3id.org/np/, but at https://np.test.knowledgepixels.com/, e.g. https://np.test.knowledgepixels.com/RAKObyGXmbgTYWj2iN0XGgJv0yWNDQd_DTmAWUouGfIsM

To publish to a production server use get_np_server(true). With true for a random server in the network, and false for the main nanopub server, defaults to true.

from nanopub_sign import Nanopub, NpProfile, get_np_server

np = Nanopub.publish(
    rdf=rdf_str,
    profile=profile,
    server_url=get_np_server(),
)

β˜‘οΈ Verify Nanopubs

This operation involves checking the integrity of Nanopubs. It ensures that a Nanopub is valid, regardless of whether it is signed or unsigned.

from nanopub_sign import Nanopub

np = Nanopub(rdf_str).check()
print("Checked info dict:", np.info())

πŸ“‘ Fetch Nanopubs

This function allows you to retrieve Nanopubs from the network using their URI. It’s useful for accessing and using Nanopubs created by others.

from nanopub_sign import Nanopub

np = Nanopub.fetch("https://w3id.org/np/RAltRkGOtHoj5LcBJZ62AMVOAVc0hnxt45LMaCXgxJ4fw")
print(np.info())

πŸ”‘ Generate private key and publish introduction

You can generate a new private/public key pair, and publish a nanopub introduction to register this key under your ORCID in the Nanopublications network:

from nanopub_sign import Nanopub, NpProfile, KeyPair, get_np_server

# Randomly generate a new private/public key pair
keypair = KeyPair()

# Create a profile with this new private key
new_profile = NpProfile(
    private_key=keypair.private,
    orcid_id="https://orcid.org/0000-0000-0000-0000",
    name="Your Name",
)

# Publish a nanopub introduction for this profile
np = Nanopub.publish_intro(new_profile, get_np_server())
print(np.info())

# Publish to the production network:
# np = Nanopub.publish_intro(new_profile, get_np_server())