Tutorial 2: Initializing Your Project

Tutorial 2: Initializing Your Project

Creating a Project Folder

In this tutorial, we’ll begin by creating a project folder for our packages for OpenFlow Agents project and setting up its initial structure.

Step 1 : Creating an Empty Project Folder

  1. Open your preferred file explorer or terminal.
  2. Navigate to the directory where you want to create your project.
  3. Create a new folder for your project. You can name it something like “example_code_tutorial_02”.

Congratulations! You’ve successfully created your project folder. Now, let’s move on to initializing the project.

Opening the Project folder inside VSCode

Now that we have our project folder set up, let’s open the project folder within VSCode.

Step 2 : Opening Visual Studio Code, our integrated development environment (IDE) of choice, and opening prepared folder.

We’ll open folder that we have created in Step 1 in order to initialize our project.

  1. Open Visual Studio Code.
  2. Click ’Open Folder…’ button.
  3. Navigate to the folder that we have created in Step 1.
  4. Click ’Folders’ menu icon inside the side menu view. Here you will see empty folder for now.

Initializing the Project

Now that we have our project folder set up, let’s connect it to OpenFlow instance and initialize it with the necessary files and configurations.

Step 3 : Add OpenIAP flow instance

! Before connecting to OpenFlow instance, ensure that you have an OpenFlow instance set up and running.
Lets connect to OpenIAP OpenFlow instance.

  • At the top search bar type: ’>Add openiap flow instance’.
  • It will going to ask us for protocol, choose: ’ws’ and click enter.
  • Then it will ask us to enter API domain, type: ’localhost.openiap.io’ and click enter. (This is using locally hosted OpenFlow)
  • Now let’s write your username of OpenFlow user that you have installed: myUsername (replace it with you Username) (!! It’s recommended to just press ‘Enter’ and not type the Username)
  • Now let’s write your password of OpenFlow user that you have installed: myPassword (replace it with you Password) (!! It’s recommended to just press ‘Enter’ and not type the Password)

Step 4 : Initializing OpenIAP flow instance

Now let’s initialize and get the folder structure for our agents packages:

  • At the top search bar type: ’Initialize project, ensure package.json and launch.json exists for openiap instance’ and click enter.
  • It will going to create the project files and folders needed for creating agents packages. (If you run this command in an empty workspace, it will automatically detect which languages you have installed and add one example file for each language.)

Explanation of Project Structure

Before we start creating our project folder, let’s briefly discuss its structure:

  • ./ : This is the folder structure which will hold all the code files for our agents.
  • .vscode/: Contains configuration file launch.json.
  • launch.json : This will contain the launch setting for each of the example files added. The settings will include 2 environment variables, apurl and jwt.
  • .node_modules/ : Contains modules for Node.js.
  • main.js : If NodeJS is detected this file will be created. This is file where you are writing you code that will going to be executed. The package.json file will also have selected main.js as your main file, but you can update this if needed.
  • main.py : If Python is detected this file will be created. This is file where you are writing you code that will going to be executed.
  • package-lock.json : This is how the VS Code extension and OpenFlow agents recognize your project dependencies for Nodejs and how to run it.
  • package.json : This is how the VS Code extension and OpenFlow agents recognize your project dependencies and how to run it. The package.json is used as a normal package.json for NodeJS/TypeScript packages, and as a placeholder for the settings in any other type of project.
  • conda.yaml : For now you will need to create the conda.yaml file if you are going to write python code. This is where you add any Python packages that are needed in order to run your code. Below you will see what code do you need to write inside the conda.yaml file.

Step 5 : Configuring .vscode Folder and Files

We are going to write the code only in python, so we will going to update the following files and folders:

  1. launch.json : Remove everything inside {} and brackets that are starting with „type“: „node“. (You can also delete whole file)
 {
            "type": "node",
            "request": "launch",
            "name": "NodeJS: Run main.js",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}/main.js",
            "console": "integratedTerminal",
            "env": {
                "apiurl": "ws://myUsername:myPassword@localhost.openiap.io/ws/v2",
                "jwt": ""
            }
        },
  1. .node_modules/ : Delete folder with all it’s content.
  2. main.js : Delete file.
  3. package-lock.json : Delete file.
  4. package.json : From this file first delete following:
,
    "scripts": {
        "updateapi": "npm uninstall @openiap/nodeapi && npm i @openiap/nodeapi"
    },
    "dependencies": {
        "@openiap/nodeapi": "^0.0.37"
    }
  1. package.json : Now update following:
"name": "Your_package_name",
    "description": "Example description of your agents package.",
    "main": "main.py",
    "openiap": {
        "daemon": false,
        "chromium": false,
        "language": "python",
        "typescript": false
    }
  1. Create the file conda.yaml in the same directory as main.py and write the code inside:
channels:
  # Define conda channels here.
  - conda-forge

dependencies:
  # Define conda-forge packages here -> https://anaconda.org/search
  # When available, prefer the conda-forge packages over pip as installations are more efficient.
  - python=3.9.13               # https://pyreadiness.org/3.9/ 
  - pip=22.3                    # https://pip.pypa.io/en/stable/news/
  #- truststore=0.7.0              # https://github.com/sethmlarson/truststore/blob/main/CHANGELOG.md
  - pip:
      # Define pip packages here -> https://pypi.org/
      - openiap

You can refer to the documentation of OpenIAP Documentation for more details on configuring these files according to your preferences.
Congratulations! You’ve successfully initialized your project with the necessary files and folders. In the next tutorial, we’ll learn how to run and debug our code in Visual Studio Code.

I would always recommend you press enter and do NOT login with username and password, but using federation.
Using username/password is generally insecure.

1 Like

Thanks for the comment!
It’s updated with that info in the right place! :blush: