@logilab/sparqlexplorer-views
v1.7.1
Published
Generic views for SparqlExplorer to display OWL, DC, SKOS
Downloads
18
Readme
Sparql generic views
This repo contains views to display data from several vocabularies in a web browser.
Supported vocabularies are:
- SKOS Concept
- SKOS Concept Scheme
- SchemaPerson
- FOAF
- OWL Class
- DublinCore
Usage
As a dependency
This library is published on NPM and exports the required components.
First add it as a dependency for your project:
npm install @logilab/sparqlexporer-views
If you want to use a specific view (eg: DublinCore), import the right component and pass the required props:
import {DublinCore} from "@logilab/sparqlexporer-views"
export function MyComponent() {
return (
<DublinCore
uri={/* uri */}
endpoint={/* sparql endpoint */}
generateUrl={/* how to build urls for navigation to other URIs */}
/>
)
}
For each view, a helper method is available to check if it can be applied to the URI.
The pattern is isXXXApplicable
where XXX
is the view name.
import {isDublinCoreApplicable} from "@logilab/sparqlexporer-views"
export function MyComponent() {
const [applicable, setApplicable] = useState<boolean | undefined>();
useEffect(() => {
const checkApplicable = async () => {
setApplicable(await isDublinCoreApplicable(/* uri */, /* endpoint */))
}
checkApplicable();
}, [])
if (applicable === undefined) {
return <p>Loading</p>
}
if (applicable) {
return <p>Applicable</p>
}
return <p>Not applicable</p>
}
Alternatively, you can loop through all available views and check the applicability for each to render the appropriate view for your URI.
import {SparqlViewConfig, SPARQL_VIEWS} from "@logilab/sparqlexporer-views"
export function MyComponent() {
const [view, setView] = useState<SparqlViewConfig | undefined | null>();
useEffect(() => {
const checkApplicable = async () => {
const result = await Promise.all(
SPARQL_VIEWS.map((view) => view.isApplicable(/* uri */, /* endpoint */))
)
const validViews = SPARQL_VIEWS.filter((v, i) => result[i])
if (validViews.length > 0) {
setView(validViews[0]);
} else {
setView(null)
}
}
checkApplicable();
}, [])
if (view === undefined) {
return <p>Loading</p>
}
if (view === null) {
return <p>No view available</p>
}
const Component = view.component;
return (
<Component
uri={/* uri */}
endpoint={/* sparql endpoint */}
generateUrl={/* how to build urls for navigation to other URIs */}
/>
)
}
Served from a server
⚠️ Please note this approach is insecure as it allows loading arbitrary JavaScript at run-time.
The views are also available as JS bundles on a static URL. Navigate to https://open-source.pages.logilab.fr/SemWeb/generic-views/index.vd.json to see the list of available views. For each item in the list, the url
key points to the js bundle. You can use @logilab/libview
library to simplify this process.
Here is an example fetching all views exposed in index.vd.json
ad selecting the first one valid:
import {ViewDescription, ViewWrapper} from '@logilab/libview';
const viewServer = "https://open-source.pages.logilab.fr/SemWeb/generic-views/index.vd.json"
const response = await fetch(viewServer)
if (response.status === 200) {
const json = await response.json()
const viewlist = json.map(
(viewDescription: ViewDescription) => new ViewWrapper(viewDescription)
)
const viewsApplicablities = await Promise.all(
viewList.map((view) => view.isApplicable(/* uri */, /* endpoint */))
);
const validViews = viewlist.filter((v, i) => viewsApplicablities[i])
const firstView = validViews[0]
const viewEntryPoint = firstView.getViewEntrypoint()
viewEntryPoint.renderSparql(/* domElement */, /* uri */, /* endpoint */);
}
Developing
Clone with Mercurial and and install the npm project.
hg clone https://forge.extranet.logilab.fr/open-source/SemWeb/generic-views
npm install
This project does not contain a demo/examples. You will have to install your local version of the library into your project to test the changes.
As explained above, there are 2 ways to serve the library, from a webserver or as a dependency.
As a dependency
The recommended method is to use npm link
to install the local library version into your project.
To prevent errors such as this one, the first step is to link the react version from your project into this library. Then you can link this library into your project.
If any of the commands below give you premission issues, do not run the commands as sudo
. Instead change the npm globals folder to be in your home directory.
The following example links the library in the sparqlexplorer project clone next to this library.
# From this library root
npm link ../sparqlexplorer/node_modules/react
cd ../sparqlexplorer
npm link ../generic-views
Once the linking process is done, you can start making your changes. To see them in your target project, you will need to compile the project using the build command.
# For a one-off build
npm run build:lib
# To build after each change
npm run build:lib -- --watch
Served from a server
If your project reads the custom views from a dev server, the build commands are slightly different as the project needs to be bundled with webpack.
# First build the project
npm run build
# or rebuild after any change
npm run build:watch
# Then serve the files in a local server
npm run serve
This will deploy a server on http://localhost:8080/
. You can then give the http://localhost:8080/index.vd.json
url to your app to load the local views.
Contributing
SparlqlExplorer is licensed under AGPLv3, all contributions are welcome!
As the project is on the Logilab code hosting platform, you will need an account to create pull requests and open issues.
Please reach us on our public matrix channel or via email at [email protected] if you want to contribute.