Want to share some of your code with a friend so they can use it in their project? Or maybe you've got a collection of functions in one project that you want to use in another project. Libraries are how we easily share functions between projects.
Exporting a Library
In order share code that you've written with someone else or one of your other projects, you'll need to export a library containing your code.
As an example, I've written a set of functions to draw a garden scene. One of those functions is called butterfly, which I can use to draw a butterfly:
In order to share my code, I do the following:
- I click the "Share" button on the top of the screen, then "Show advanced options".
- Within the advanced options, we select "Share as library" tab, then click the "Share as library" button:
- Under the "Export Functions as a Library" header, we select:
- A title and description for our library. Note that the title will be the first part of each function's name when imported elsewhere, so use something short and descriptive.
- Note that the comments that are place directly above the function in our code get exported with our library -- more on how those are used in the "Importing a Library" section.
- Which functions we want to export with our library. Remember, if one of your functions gets used by another one of your functions, you need to include both of the functions in your exported library.
Click "Publish" to create your library! You'll then see an ID for your library, which you can copy and share with other people, or use in other projects of your own.
Updating exported libraries
If you make an update to your library's code, it will not be automatically included in your published library. If you want that update to be shared with other people, you'll need to follow the same steps as described in "Exporting a Library" above -- the only difference is that the final button to publish your updates will say "Update" instead of "Publish".
Importing a Library
If you want to use code that you or someone else has written in a different project, you'll need to import a library containing that code. To do this:
- Click the gear icon at the top right of your Toolbox. Select "Manage Libraries".
- Use "Import library from ID": We'll copy in an ID that was generated when we generated a library (see "Exporting a Library" above), or an ID that has been shared with us by someone else. Click "Add" to import the library.Alternatively, if someone else in your class has published a library, you should see their library listed in the Import library from my class section.
- Once you've imported a library, the functions become available in the "Functions" section of your Toolbox. All of the names of these new functions begin with the name of the library you imported.The comments you included when exporting your library will appear when you hover over the newly imported functions in your Toolbox.
- We can now drag these new functions into our Workspace and use them in our project!
Managing imported libraries
Once you've imported one (or many) libraries, you may want to be able to make changes to them. You can manage existing libraries in the same place that you import them, by clicking the gear icon at the top of your Toolbox:
- Delete libraries you no longer need.
- Update libraries that have been changed: if a library you've imported has been changed by the project that created the library, you will see the option to "Update" the library.