The OctoPrint tutorial for writing OctoPrint plugins is geared toward Linux and in particular, the Raspberry PI. However what happens with you want to print directly to your favorite 3D printer directly from your mac using a USB cable?
Let's look at how to write a simple OctoPrint plugin for macOS!
To get started, this is my simple script to start OctoPrint on macOS:
macos$ cat /usr/local/bin/octostart
#!/bin/bash
cd /Applications/OctoPrint
virtualenv venv
source venv/bin/activate
octoprint serve
This script permits me to start OctoPrint in the recommended virtual environment easily and consistently.
The first thing I recommend all beginner OctoPrint plugin writers to do is to find the directories where OctoPrint searches for plugins when it starts up. You can do this by starting OctoPrint and looking at the terminal output. Approximately 15 lines (give-or-take) after OctoPrint starts, you will fine a line similar to this:
2021-09-25 17:32:42,578 - octoprint.plugin.core - INFO - Loading plugins from /Applications/OctoPrint/venv/lib/python3.7/site-packages/octoprint/plugins, /Users/Tim/Library/Application Support/OctoPrint/plugins and installed plugin packages...
In my macOS, OctoPrint tells us that it searches these two directories for plugins:
- /Applications/OctoPrint/venv/lib/python3.7/site-packages/octoprint/plugins
- /Users/Tim/Library/Application Support/OctoPrint/plugins
To make matters easy when writing a plugin, I created a symbolic link between to the second directory on my desktop. This symlink is not required but it made my life a bit easier learning to write my first OctoPrint plugin without an documentation for macOS (that I could find):
ln -sf /Users/Tim/Library/Application Support/OctoPrint/plugins /Users/Tim/Desktop/plugins
The most important thing is to simply know where OctoPrint searches, by default, for plugins for your chosen computer setup. The location documented in the OctoPrint plugin tutorial is different for Linux-based distributions and not the mac; and this took me a bit of extra time to figure out.
Next, you can put a "Hello World" file in that location and it now be an OctoPrint plugin. I recommend you test your first plugin with this Python code from the OctoPrint tutorial:
helloworld.py
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import octoprint.plugin
class HelloWorldPlugin(octoprint.plugin.StartupPlugin):
def on_after_startup(self):
self._logger.info("Hi Neo, Hello World!")
__plugin_name__ = "Hello World"
__plugin_version__ = "1.0.0"
__plugin_description__ = "A quick \"Hello World\" example plugin for OctoPrint"
__plugin_pythoncompat__ = ">=2.7,<4"
__plugin_implementation__ = HelloWorldPlugin()
Restart OctoPrint and you should see your hello world log entry in the terminal:
2021-09-25 18:34:49,383 - octoprint.plugins.helloworld.py INFO - Hi Neo, Hello World!
This works; but the problem is that by using this method, you cannot add HTML templates and Javascript because of how Python plugins for OctoPrint are sourced. Next is how I got the plugin to work more completely. The steps are pretty simple after you figure it out, which took me a few hours to get right.
-
Create a file in the plugin directory called
setup.py
which describes the meta data for the plugin, including the name of the plugin which will be used as the plugin code subdirectory.. -
Create the subdirectory using the correct naming convention.
-
Rename your very basic
helloworld.py
plugin code to__init__.py
and move that code to the subdirectory created in generic step two above.
In my next post in this topic, I will describe how to do the steps above and add functionality to our "Hello World" plugin to display two links in the OctoPrint navbar and the results will look like this, with one link in red and one link in blue or any other color you desire with any text or links you want.
Please stand by for my next post in this topic where I will describe how to add functionality this OctoPrint plugin for macOS and make it a bit useful. Also, I will tell you an important "secret" on how to get your CSS changes to actually work! This will save you a lot of time, as it took me hours to figure this out!
Will be back soon with more details on creating our first OctoPrint plugin on macOS ......