Skip to content

dssCode (Platform for Coding)

Access via dsslab-net-bench

If you want to import a graph, which has been coded in dssCode, go to your profile settings in dssCode and copy the API-Token from there. This is your key to access the platform using python.

Warning

Never commit this token to repositories!

We never want to write the token into our script, even if we just pass it as a parameter. Instead we need to pass it another way. This can be done with a so-called environment variable. An environment variable is made up of a key and a value, similar to an item in a dictionary in python.

The plan is: 1. We create an environment variable in a way, that prevents it from being made public. 2. We feed the environment variables into running python code with a python variable. 3. This python variable is passed to the method that needs the token.

Umgebungsvariablen über IDE

Viele IDEs ermöglichen eine Festlegung von Umgebungsvariablen, wenn Python ausgeführt wird. Falls du eine solche verwendest, findest du dazu online häufig eine Erklärung. Hier einige Erklärungsseiten für das Setzen von Umgebungsvariablen von verbreiteten IDEs:

  • Pycharm
  • VS Code
  • Spyder unterstützt anscheinend derzeit nicht das Setzen von Umgebungsvariablen.

.env

Neben den Möglichkeiten der IDE gibt es noch eine weitere Möglichkeit, die unabhängig funktioniert: Das Erstellen einer .env-Datei. Diese Datei besteht aus einer Liste von Umgebungsvariablen, die untereinander geschrieben werden.

Hinweis

.env kann auch für das TextSearch-Modul genutzt werden (siehe unten).

Der Inhalt der .env sieht wie folgt aus:

DSSCODE_TOKEN=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

Weitere verwendete Variablen können darunter geschrieben werden. Variablen aus .env werden gegenüber allgemeinen Umgebungsvariablen priorisiert.

Achtung

In deinem Skript muss explizit load_dotenv() aufgerufen werden:

from dotenv import load_dotenv
load_dotenv()

Dann geht es wie im nächsten Abschnitt beschrieben weiter.

Verwenden von Umgebungsvariablen

Um den Wert einer Umgebungsvariable in eine Python-Variable zu übertragen, können wir die Bordmittel von Python nutzen:

import os

# Hängt davon ab, wie dein Token bezeichnet ist
dsscode_token=os.getenv("DSSCODE_TOKEN")

Damit wäre deine Umgebungsvariable DSSCODE_TOKEN in die Python-Variable dsscode_token übertragen und du kannst sie jetzt an die entsprechenden Methoden übergeben.

There are two ways to achieve 1.:

Project Slug

You need the so-called project slug from dssCode. That is a string that is readable for humans, without spaces or special characters. If you are in the project overvie, you can find the slug in the adress bar of your browser:

In this example this would be sustainable_investment.

Snapshot

The point of the snapshots is, to get a specific state from dssCode. Creating a snapshot works like this in dssCode: Projektübersicht -> Snapshots -> Snapshot anlegen. After that, a new entry appears in the table below. This ID needs to be copied and is our value for snapshot in python.

Note

This snapshot-ID can be commited. That can make sense so that other people in your team can get the same state for their network.

Usage

A simple call could look like this:

G = dnb.import_from_dsscode(
    "sustainable_investment",
    "hhhhhhhhhhhhhhhhhh" # (1)
)

Depending on the size of the graph, this can take a bit of time. Per default, the call is cached, which means that the API doesn't need to be requested for every call. This reduces the load on the server. You don't need to do anything more to achieve that.