VS Code

Python Extension

VS Code is a multi-purpose code editor. To execute Python scripts we need to install the Python extension from the VS Code Marketplace.

Click the extensions icon in the activity bar on the left side of the editor and search the extension name in the search box. Once found, select it and click Install.

Although not strictly necessary, we also recommend the following additional extensions.

  • Pylance

  • File Utils

  • Anaconda Extension Pack

  • EditorConfig

The Command Palette

The Command Palette provides access to all VS Code commands. Top open the Command Palette, select it from the View menu (View > Command Palette), or use the keyboard shortcut.

  • Windows: Shift+Ctrl+P

  • macOS: Shift+Cmd+P

Commands are grouped into categories. To list only the commands of a particular category, simply type the name of the category at the Command Pallete prompt (>). For example to list all Python commands, type Python

Select a Python Interpreter

Open the Command Palette.

  1. Select Python: Select Interpreter

  2. Select Python x.x.x ('ita21':conda) from the listed environments.

Select a Python Linter

Open the Command Palette.

  1. Select Python: Select Linter

  2. Select flake8.

Select the Default Terminal Profile

This is for Windows users.

Open the Command Palette.

  1. Select Terminal: Select Default Profile

  2. Select Command Prompt

Don't use the PowerShell since this will create all sorts of problems! Note that this change will only have an effect after all current terminals are shut down.

Create a new Python file

Use keyboard shortcut: Shift+Ctrl+N for Windows, or Shift+Cmd+N for Mac to create a file. Save it as test.py.

Note that when you create a new file, the default file format is .txt. So, if you want to write a Python code, make sure to specify the format .py.

First Python script in VS Code

Type the following code in the text editor. Make sure that you have saved the file as .py.

import compas
print(compas.__version__)

Right click the text editor and then select Run Python File in Terminal in the context menu. You should be able to see the print results 1.8.1 in the Terminal menu. Hooray! It means that we have installed compas correctly.

Plotter

Copy the following code in your text editor and run the file. You will see a pop-up window similar to the picture below.

from compas.geometry import Pointcloud
from compas.utilities import pairwise, i_to_red
from compas_plotters import Plotter

# create a plotter instance
plotter = Plotter()

# create a point cloud
pointcloud = Pointcloud.from_bounds(8, 5, 0, 10)

# point an arrow from every point in the cloud to the next
for index, (a, b) in enumerate(pairwise(pointcloud)):
    vector = b - a
    vector.unitize()
    plotter.add(vector, point=a, draw_point=True, color=i_to_red(max(index / 10, 0.1), normalize=True))

# zoom the extents of all objects that were added
plotter.zoom_extents()

# show the plot
plotter.show()

Last updated