### OctoPrint Development Environment Setup (Shell) Source: https://docs.octoprint.org/en/main/_sources/plugins/gettingstarted.rst.txt Commands to set up a local development environment for OctoPrint. This includes cloning the repository, creating a virtual environment, and installing necessary dependencies. ```shell $ cd ~/devel $ git clone https://github.com/OctoPrint/OctoPrint $ cd OctoPrint $ virtualenv venv $ source venv/bin/activate (venv) $ pip install -e '.[develop,plugins]' [...] (venv) $ octoprint --help ``` -------------------------------- ### Install OctoPrint Dependencies and Setup on Windows Source: https://docs.octoprint.org/en/main/_sources/development/environment.rst.txt This snippet details the process of installing OctoPrint and its development dependencies on a Windows system using Git Bash and pip. It includes cloning the repository, creating a virtual environment, and installing the project with development and plugin extras. ```none pip install virtualenv cd /c/Devel git clone https://github.com/OctoPrint/OctoPrint.git cd OctoPrint virtualenv --python=C:/Python3/python.exe venv source ./venv/Scripts/activate pip install --upgrade pip python -m pip install -e '.[develop,plugins,docs]' pre-commit install git config blame.ignoreRevsFile .git-blame-ignore-revs ``` -------------------------------- ### Install Plugins from URL or File (JSON Example) Source: https://docs.octoprint.org/en/main/_sources/bundledplugins/pluginmanager.rst.txt This JSON structure defines a list of plugin URLs to be installed. The Plugin Manager processes these URLs to install plugins from remote sources. ```json [ "https://github.com/jneilliii/OctoPrint-BedLevelVisualizer/archive/master.zip", "https://github.com/eyal0/OctoPrint-PrintTimeGenius/archive/master.zip" ] ``` -------------------------------- ### Install OctoPrint Plugin via Pip (Shell) Source: https://docs.octoprint.org/en/main/_sources/plugins/gettingstarted.rst.txt This command demonstrates how to install an OctoPrint plugin directly from a GitHub repository using pip. It assumes the plugin is hosted on GitHub and provides a zip archive URL. This is a common method for distributing and installing OctoPrint plugins. ```bash (venv) $ pip install https://github.com/yourGithubName/OctoPrint-HelloWorld/archive/main.zip ``` -------------------------------- ### OctoPrint Server Startup Log (Log) Source: https://docs.octoprint.org/en/main/plugins/gettingstarted.html Example log output showing OctoPrint starting up and successfully discovering and loading a newly added plugin. ```log (venv) $ octoprint serve 2023-11-20 11:14:35,122 - octoprint.startup - INFO - ****************************************************************************** 2023-11-20 11:14:35,124 - octoprint.startup - INFO - Starting OctoPrint 1.9.3 2023-11-20 11:14:35,124 - octoprint.startup - INFO - ****************************************************************************** [...] 2023-11-20 11:14:35,124 - octoprint.plugin.core - INFO - Loading plugins from /home/gina/.octoprint/plugins, /home/gina/devel/OctoPrint/src/octoprint/plugins and installed plugin packages... [...] 2023-11-20 11:14:36,135 - octoprint.plugin.core - INFO - 19 plugin(s) registered with the system: [...] | Hello World (1.0.0) = /home/gina/.octoprint/plugins/helloworld.py [...] ``` -------------------------------- ### Complete OctoPrint Setup on Linux Source: https://docs.octoprint.org/en/main/_sources/development/environment.rst.txt Performs the full OctoPrint development setup on Linux, including cloning, virtual environment creation, dependency installation, and Git hook configuration. ```bash cd ~/devel git clone https://github.com/OctoPrint/OctoPrint.git cd OctoPrint virtualenv --python=python3 venv source ./venv/bin/activate pip install --upgrade pip pip install -e '.[develop,plugins,docs]' pre-commit install git config blame.ignoreRevsFile .git-blame-ignore-revs ``` -------------------------------- ### Run OctoPrint Server Source: https://docs.octoprint.org/en/main/_sources/development/environment.rst.txt Starts the OctoPrint server from the command line. This command is used to run the application after setup. ```bash octoprint serve ``` -------------------------------- ### OctoPrint Plugin: Saying Hello on Startup (Python) Source: https://docs.octoprint.org/en/main/_sources/plugins/gettingstarted.rst.txt Extends the basic plugin structure to include functionality that executes upon OctoPrint server startup. It utilizes the StartupPlugin mixin and the on_after_startup method to log a 'Hello World!' message. ```python import octoprint.plugin class HelloWorldPlugin(octoprint.plugin.StartupPlugin): def on_after_startup(self): self._logger.info("Hello World!") __plugin_name__ = "Hello World" __plugin_version__ = "1.0.0" __plugin_description__ = "A quick \"Hello World\" example plugin for OctoPrint" __plugin_pythoncompat__ = ">=3.7,<4" __plugin_implementation__ = HelloWorldPlugin() ``` -------------------------------- ### Install Plugin and Start OctoPrint Server for Testing Source: https://docs.octoprint.org/en/main/plugins/python3_migration.html Installs the plugin in editable mode within the activated virtual environment and starts the OctoPrint server with debugging enabled. This allows for real-time testing of the plugin's Python 3 compatibility. ```bash pip install -e path/to/your/plugin octoprint serve --debug ``` -------------------------------- ### Install OctoPrint Dependencies and Setup on macOS Source: https://docs.octoprint.org/en/main/_sources/development/environment.rst.txt This snippet outlines the steps to set up the OctoPrint development environment on macOS. It involves installing Xcode command line tools, Homebrew for Python 3, pip, virtualenv, and then cloning and installing OctoPrint with its development dependencies. ```none xcode-select --install sudo xcodebuild sudo xcode-select -s /Applications/Xcode.app/Contents/Developer ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" brew install python python -m ensurepip --upgrade pip install virtualenv cd ~/devel git clone https://github.com/OctoPrint/OctoPrint.git cd OctoPrint virtualenv venv source venv/bin/activate pip install --upgrade pip pip install -e '.[develop,plugins]' pre-commit install git config blame.ignoreRevsFile .git-blame-ignore-revs ``` -------------------------------- ### OctoPi Virtual Environment Activation (Shell) Source: https://docs.octoprint.org/en/main/_sources/plugins/gettingstarted.rst.txt Instructions for activating the OctoPrint virtual environment on a Raspberry Pi running OctoPi. This is an alternative to a full local development setup. ```shell $ source ~/oprint/bin/activate (oprint) $ octoprint --help ``` -------------------------------- ### Simplify OctoPrint Plugin __init__.py Source: https://docs.octoprint.org/en/main/_sources/plugins/gettingstarted.rst.txt This Python snippet demonstrates simplifying the plugin's initialization file by removing redundant metadata. It retains essential plugin implementation details like the startup plugin class and Python compatibility, while relying on pyproject.toml for name, version, and description. ```python import octoprint.plugin class HelloWorldPlugin(octoprint.plugin.StartupPlugin): def on_after_startup(self): self._logger.info("Hello World!") __plugin_pythoncompat__ = ">=3.7,<4" __plugin_implementation__ = HelloWorldPlugin() ``` -------------------------------- ### Install OctoPrint and Dependencies on Mac OS X Source: https://docs.octoprint.org/en/main/development/environment.html Clones the OctoPrint repository, sets up a Python virtual environment, installs dependencies, and configures pre-commit hooks and git blame ignore revisions. This sequence of commands is essential for starting OctoPrint development on macOS. ```bash cd ~/devel git clone https://github.com/OctoPrint/OctoPrint.git cd OctoPrint virtualenv venv source venv/bin/activate pip install --upgrade pip pip install -e '.[develop,plugins]' pre-commit install git config blame.ignoreRevsFile .git-blame-ignore-revs ``` -------------------------------- ### OctoPrint Plugin Initialization and Startup Logic (Python) Source: https://docs.octoprint.org/en/main/_sources/plugins/gettingstarted.rst.txt This Python code defines a basic OctoPrint plugin named 'Hello World'. It inherits from several plugin mixins to enable startup, template, and settings functionality. The `on_after_startup` method logs a message including a URL retrieved from the plugin's settings. ```python import octoprint.plugin class HelloWorldPlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplatePlugin, octoprint.plugin.SettingsPlugin): def on_after_startup(self): self._logger.info("Hello World! (more: %s)" % self._settings.get(["url"])) def get_settings_defaults(self): return dict(url="https://en.wikipedia.org/wiki/Hello_world") __plugin_name__ = "Hello World" __plugin_pythoncompat__ = ">=3.7,<4" __plugin_implementation__ = HelloWorldPlugin() ``` -------------------------------- ### Example GET Response from Simple API Source: https://docs.octoprint.org/en/main/plugins/mixins.html This illustrates a typical HTTP response from a GET request to a SimpleApiPlugin's resource. It returns a JSON document containing dynamic data provided by the plugin. ```http HTTP/1.1 200 Ok Content-Type: application/json { "foo": "bar" } ``` -------------------------------- ### Implement WizardPlugin: Basic Setup Check (Python) Source: https://docs.octoprint.org/en/main/plugins/mixins.html This example demonstrates a basic implementation of WizardPlugin to check if a required setting is unset. It inherits from SettingsPlugin, TemplatePlugin, and WizardPlugin. The is_wizard_required method returns True if 'some_key' is None, triggering the wizard. ```python class MyPlugin(octoprint.plugin.SettingsPlugin, octoprint.plugin.TemplatePlugin, octoprint.plugin.WizardPlugin): def get_default_settings(self): return dict(some_key=None) def is_wizard_required(self): return self._settings.get(["some_key"]) is None ``` -------------------------------- ### Set Up OctoPrint Development Environment (Bash) Source: https://docs.octoprint.org/en/main/plugins/gettingstarted.html Commands to clone OctoPrint, create a virtual environment, install dependencies, and activate the environment for local development. ```bash cd ~/ devel git clone https://github.com/OctoPrint/OctoPrint [...] cd OctoPrint virtualenv venv [...] source venv/bin/activate (venv) $ pip install -e '.[develop,plugins]' [...] (venv) $ octoprint --help Usage: octoprint [OPTIONS] COMMAND [ARGS]... [...] ``` -------------------------------- ### Install Cookiecutter for Plugin Development Source: https://docs.octoprint.org/en/main/_sources/plugins/gettingstarted.rst.txt This command installs the cookiecutter package, which is a prerequisite for using the octoprint dev plugin:new command to generate a new OctoPrint plugin skeleton. It specifies a version range for compatibility. ```bash (venv) $ pip install "cookiecutter>=2.1.1,<3" ``` -------------------------------- ### Install OctoPrint Plugin using pip in Development Mode Source: https://docs.octoprint.org/en/main/_sources/plugins/gettingstarted.rst.txt This command installs the OctoPrint plugin in editable mode using pip. This is useful during development as it allows changes to be reflected without reinstallation. The command utilizes the Python interpreter associated with the current virtual environment. ```bash (venv) $ octoprint dev plugin:install >> /home/gina/.pyenv/versions/3.11.2/envs/octoprint-py311/bin/python -m pip install -e . Obtaining file:///home/gina/devel/OctoPrint-Helloworld [...] Installing collected packages: OctoPrint-Helloworld [...] Successfully installed OctoPrint-Helloworld-0.1.0 ``` -------------------------------- ### ResettableTimer Example: Basic Usage Source: https://docs.octoprint.org/en/main/modules/util.html Shows a basic example of the ResettableTimer class, which allows resetting the timer's countdown. It starts a timer, waits for half the interval, and then resets it. ```python import time def hello(): print("Ran hello() at {}").format(time.time()) t = ResettableTimer(60.0, hello) t.start() print("Started at {}").format(time.time()) time.sleep(30) t.reset() print("Reset at {}").format(time.time()) ``` -------------------------------- ### Build OctoPrint Documentation Source: https://docs.octoprint.org/en/main/_sources/development/environment.rst.txt Builds the HTML documentation for OctoPrint using Sphinx. The documentation will be generated in the '_build' directory. ```bash sphinx-build -b html . _build ``` -------------------------------- ### OctoPrint Discovery Plugin Configuration Example (YAML) Source: https://docs.octoprint.org/en/main/_sources/bundledplugins/discovery.rst.txt This snippet shows a valid configuration example for the discovery plugin in OctoPrint's config.yaml file. It specifies network interfaces, public port, SSL usage, and custom ZeroConf services with their respective TXT records and model details. ```yaml plugins: discovery: interfaces: - eth0 publicPort: 443 useSsl: true zeroConf: - service: _someservice._tcp port: 1234 txt_record: field1: value1 field2: value2 model: name: Some Model vendor: Some Vendor ``` -------------------------------- ### OctoPrintClient Initialization and Options Source: https://docs.octoprint.org/en/main/jsclientlib/base.html Details on how to instantiate the OctoPrintClient and configure its options. ```APIDOC ## OctoPrintClient Class ### Description Instantiates a new instance of the client library. By default, an instance is globally registered as `OctoPrint`. ### Method Constructor ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Options Object (`options`) An optional object to configure `OctoPrintClient.options`. - **baseurl** (`string`) - The base URL of the OctoPrint API. - **apikey** (`string`) - The API Key for authentication. If unset, session login is required. - **locale** (`string`) - The locale for `X-Locale` headers, useful for localized content. ### Request Example ```javascript const octoPrintClient = new OctoPrintClient({ baseurl: "http://localhost:5000/", apikey: "YOUR_API_KEY" }); ``` ### Response #### Success Response (N/A - Constructor) N/A #### Response Example N/A ``` -------------------------------- ### Add Navigation Bar Link in OctoPrint Plugin (Python & Jinja2) Source: https://docs.octoprint.org/en/main/_sources/plugins/gettingstarted.rst.txt This example demonstrates how to add custom content to OctoPrint's web interface, specifically a link in the navigation bar. It involves inheriting from `TemplatePlugin` in Python and creating a Jinja2 template file (`.jinja2`) to define the HTML structure. The plugin's directory structure needs to include a `templates` subdirectory. ```python import octoprint.plugin class HelloWorldPlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplatePlugin): def on_after_startup(self): self._logger.info("Hello World!") __plugin_name__ = "Hello World" __plugin_pythoncompat__ = ">=3.7,<4" __plugin_implementation__ = HelloWorldPlugin() ``` ```html Hello World! ``` -------------------------------- ### Plugin File Structure Example Source: https://docs.octoprint.org/en/main/plugins/gettingstarted.html An example of a typical OctoPrint plugin's file structure, highlighting the placement of static assets like CSS, JavaScript, and LESS files, as well as template files and the main Python initialization script. ```text octoprint_helloworld/ static/ css/ helloworld.css js/ helloworld.js templates/ helloworld_navbar.jinja2 helloworld_settings.jinja2 helloworld_tab.jinja2 __init__.py .editorconfig .gitignore babel.cfg MANIFEST.in README.md pyproject.toml requirements.txt setup.py Taskfile.yml ``` -------------------------------- ### Retrieve Installed Language Packs (HTTP) Source: https://docs.octoprint.org/en/main/_sources/api/languages.rst.txt This HTTP GET request retrieves a list of all installed language packs on OctoPrint. The response is a JSON object containing a map of component lists, indexed by component identifier. ```http GET /api/languages HTTP/1.1 Host: example.com X-Api-Key: abcdef... ``` ```json { "language_packs": { "_core": { "identifier": "_core", "name": "Core", "languages": [] }, "some_plugin": { "identifier": "some_plugin", "name": "Some Plugin", "languages": [ { "locale": "de", "locale_display": "Deutsch", "locale_english": "German", "last_update": 1474574597, "author": "Gina Häußge" }, { "locale": "it", "locale_display": "Italiano", "locale_english": "Italian", "last_update": 1470859680, "author": "The italian Transifex Team" } ] } } } ``` -------------------------------- ### Example pyproject.toml Configuration Source: https://docs.octoprint.org/en/main/plugins/gettingstarted.html Shows the content of a pyproject.toml file generated by the cookiecutter template. This file defines build system requirements and project metadata for a Python package. ```toml [build-system] requires = ["setuptools>=68", "wheel"] build-backend = "setuptools.build_meta" [project] name = "OctoPrint-Helloworld" version = "1.0.0" description = "A quick \"Hello World\" example plugin for OctoPrint" authors = [ {name = "Your name", email = "you@somewhere.net"} ] readme = {file = "README.md", content-type = "text/markdown"} dynamic = [ "license" ] requires-python = ">=3.7, <4" ``` -------------------------------- ### Startup and After-Startup Log Output Source: https://docs.octoprint.org/en/main/_sources/plugins/mixins.rst.txt This section shows the expected log output from plugins implementing the StartupPlugin. The order of 'starting up' and 'started up' messages reflects the plugin's priority and bundling status, demonstrating how OctoPrint manages the execution order of these methods. ```none Plugin B starting up Plugin C starting up Plugin D starting up Plugin A starting up Plugin D started up Plugin A started up Plugin B started up Plugin C started up ``` -------------------------------- ### Retrieve Installed Language Packs (GET /api/languages) Source: https://docs.octoprint.org/en/main/api/languages.html Retrieves a list of all installed language packs on OctoPrint. The response includes a JSON object detailing each language pack, its identifier, name, and the languages it contains. Requires the SETTINGS permission. ```http GET /api/languages HTTP/1.1 Host: example.com X-Api-Key: abcdef... ``` ```json HTTP/1.1 200 OK Content-Type: application/json { "language_packs": { "_core": { "identifier": "_core", "name": "Core", "languages": [] }, "some_plugin": { "identifier": "some_plugin", "name": "Some Plugin", "languages": [ { "locale": "de", "locale_display": "Deutsch", "locale_english": "German", "last_update": 1474574597, "author": "Gina Häußge" }, { "locale": "it", "locale_display": "Italiano", "locale_english": "Italian", "last_update": 1470859680, "author": "The italian Transifex Team" } ] } } ``` -------------------------------- ### OctoPrint Viewmodel Initialization Example (JavaScript) Source: https://docs.octoprint.org/en/main/_sources/plugins/viewmodels.rst.txt Demonstrates how to define a custom viewmodel and register it with OctoPrint's viewmodel system. This example shows how to implement the `onAllBound` callback to interact with other viewmodels during application startup. ```javascript $(function() { function MyCustomViewModel(parameters) { var self = this; // ... self.onAllBound = function(allViewModels) { // do something with them } // ... } OCTOPRINT_VIEWMODELS.push({ construct: MyCustomViewModel, dependencies: ["loginStateViewModel"], elements: ["#some_div", "#some_other_div"] }); }) ``` -------------------------------- ### GET /api/languages Source: https://docs.octoprint.org/en/main/_sources/api/languages.rst.txt Retrieves a list of all installed language packs on OctoPrint. This includes core language packs and those provided by plugins. ```APIDOC ## GET /api/languages ### Description Retrieves a list of installed language packs. ### Method GET ### Endpoint /api/languages ### Parameters #### Query Parameters None #### Request Body None ### Request Example ```http GET /api/languages HTTP/1.1 Host: example.com X-Api-Key: abcdef... ``` ### Response #### Success Response (200) - **language_packs** (object) - A map of component lists, indexed by the component's identifier. - **identifier** (string) - The plugin's identifier, `_core` for core OctoPrint, the plugin's identifier for plugins. - **name** (string) - Displayable name of the component, `Core` for core OctoPrint, the plugin's name for plugins. - **languages** (array) - Language packs for the component. - **locale** (string) - The locale of the language pack (e.g., 'de', 'it'). - **locale_display** (string) - The display name of the locale (e.g., 'Deutsch', 'Italiano'). - **locale_english** (string) - The English name of the locale (e.g., 'German', 'Italian'). - **last_update** (integer) - Timestamp of the last update. - **author** (string) - The author of the language pack. #### Response Example ```json { "language_packs": { "_core": { "identifier": "_core", "name": "Core", "languages": [] }, "some_plugin": { "identifier": "some_plugin", "name": "Some Plugin", "languages": [ { "locale": "de", "locale_display": "Deutsch", "locale_english": "German", "last_update": 1474574597, "author": "Gina Häußge" }, { "locale": "it", "locale_display": "Italiano", "locale_english": "Italian", "last_update": 1470859680, "author": "The italian Transifex Team" } ] } } } ``` ``` -------------------------------- ### YAML List Example Source: https://docs.octoprint.org/en/main/configuration/yaml.html Demonstrates the creation of a YAML list using the hyphen prefix for list items. All items in a list must have the same indentation level. ```yaml - item 1 - 23.42 - 57 - true ``` -------------------------------- ### SD Printing Operations Source: https://docs.octoprint.org/en/main/_sources/development/virtual_printer.rst.txt Manages SD card printing operations including starting, selecting, and canceling prints. ```APIDOC ## POST /api/sdcard ### Description Performs operations related to SD card printing. ### Method POST ### Endpoint /api/sdcard ### Parameters #### Request Body - **operation** (string) - Required - The SD card operation. Possible values: `start_sd`, `select_sd`, `cancel_sd`. - **file** (string) - Required for `start_sd` and `select_sd` - The name of the file on the SD card. ### Request Example ```json { "operation": "start_sd", "file": "my_print_file.gcode" } ``` ### Response #### Success Response (200) - **message** (string) - A confirmation message about the SD card operation. #### Response Example ```json { "message": "SD print started for file 'my_print_file.gcode'." } ``` -------------------------------- ### Retrieve All Printer Profiles (HTTP GET) Source: https://docs.octoprint.org/en/main/_sources/api/printerprofiles.rst.txt This endpoint retrieves all configured printer profiles. It requires the 'CONNECTION' permission and returns a 200 status code with a profile list object. The example shows a GET request and a sample JSON response. ```http GET /api/printerprofiles HTTP/1.1 Host: example.com X-Api-Key: abcdef... ``` ```json HTTP/1.1 200 OK Content-Type: application/json { "profiles": { "_default": { "id": "_default", "name": "Default", "color": "default", "model": "Generic RepRap Printer", "default": true, "current": true, "resource": "http://example.com/api/printerprofiles/_default", "volume": { "formFactor": "rectangular", "origin": "lowerleft", "width": 200, "depth": 200, "height": 200 }, "heatedBed": true, "heatedChamber": false, "axes": { "x": { "speed": 6000, "inverted": false }, "y": { "speed": 6000, "inverted": false }, "z": { "speed": 200, "inverted": false }, "e": { "speed": 300, "inverted": false } }, "extruder": { "count": 1, "offsets": [ {"x": 0.0, "y": 0.0} ] } }, "my_profile": { "id": "my_profile", "name": "My Profile", "color": "default", "model": "My Custom Printer", "default": false, "current": false, "resource": "http://example.com/api/printerprofiles/my_profile", "volume": { "formFactor": "rectangular", "origin": "lowerleft", "width": 200, "depth": 200, "height": 200 }, "heatedBed": true, "heatedChamber": true, "axes": { "x": { "speed": 6000, "inverted": false }, "y": { "speed": 6000, "inverted": false }, "z": { "speed": 200, "inverted": false }, "e": { "speed": 300, "inverted": false } }, "extruder": { "count": 1, "offsets": [ {"x": 0.0, "y": 0.0} ] } } } } ``` -------------------------------- ### Build OctoPrint Documentation (Shell) Source: https://docs.octoprint.org/en/main/_sources/development/environment.rst.txt Builds the OctoPrint documentation in HTML format using Sphinx. It requires Sphinx to be installed and specifies the source and output directories. ```shell python.interpreterPath -m sphinx-build -b html ./docs ./docs/_build ``` -------------------------------- ### OctoPrintClient Initialization and Options Source: https://docs.octoprint.org/en/main/_sources/jsclientlib/base.rst.txt Information on how to instantiate the OctoPrintClient and the available options for configuration. ```APIDOC ## OctoPrintClient Initialization and Options ### Description Instantiates a new instance of the client library. An instance is globally registered as ``OctoPrint`` by default. ### Parameters #### Constructor Options - **options** (object) - Optional - An object of options to set :js:attr:`OctoPrintClient.options` to. #### Client Options - **baseurl** (string) - The base URL of the OctoPrint API. - **apikey** (string) - The API Key for OctoPrint API requests. If unset, session login is required. - **locale** (string) - The locale for ``X-Locale`` headers on API requests, useful for localized content. ``` -------------------------------- ### YAML List Example Source: https://docs.octoprint.org/en/main/_sources/configuration/yaml.rst.txt Demonstrates how to define a list in YAML using hyphens to prefix each item. All list items must maintain the same indentation level. ```yaml - item 1 - 23.42 - 57 - true ``` -------------------------------- ### RepeatedTimer Example: Basic Usage Source: https://docs.octoprint.org/en/main/modules/util.html Demonstrates the basic usage of the RepeatedTimer class, which runs a function repeatedly at a specified interval. It starts the timer and prints a message every second. ```python def hello(): print("Hello World!") t = RepeatedTimer(1.0, hello) t.start() # prints "Hello World!" every second ``` -------------------------------- ### Basic OctoPrint Plugin Structure (Python) Source: https://docs.octoprint.org/en/main/_sources/plugins/gettingstarted.rst.txt Defines the essential metadata for an OctoPrint plugin, including its name, version, description, and Python compatibility. This code should be saved in a .py file within the OctoPrint plugins directory. ```python __plugin_name__ = "Hello World" __plugin_version__ = "1.0.0" __plugin_description__ = "A quick \"Hello World\" example plugin for OctoPrint" __plugin_pythoncompat__ = ">=3.7,<4" ``` -------------------------------- ### OctoPrint Plugin Configuration (pyproject.toml) Source: https://docs.octoprint.org/en/main/plugins/gettingstarted.html This snippet shows the pyproject.toml configuration for an OctoPrint plugin. It defines package details, entry points, and optional dependencies. The 'octoprint.plugin' entry point is crucial for OctoPrint to discover the plugin. ```toml [tool.setuptools] include-package-data = true [tool.setuptools.packages.find] include = [ "octoprint_helloworld", "octoprint_helloworld.*" ] [project.entry-points."octoprint.plugin"] helloworld = "octoprint_helloworld" [project.urls] Homepage = "https://github.com/yourGithubName/OctoPrint-Helloworld" [project.optional-dependencies] develop = [ "go-task-bin" ] ``` -------------------------------- ### Create and Activate Virtual Environment Source: https://docs.octoprint.org/en/main/_sources/development/environment.rst.txt Creates a virtual environment named 'venv' using Python 3 and then activates it. This isolates project dependencies. ```bash virtualenv --python=python3 venv source venv/bin/activate ``` ```bash virtualenv --python=python3 venv source venv/Scripts/activate ``` -------------------------------- ### Define Custom API Endpoint with BlueprintPlugin Source: https://docs.octoprint.org/en/main/plugins/mixins.html This example demonstrates how to create a simple echo endpoint using BlueprintPlugin. It defines a GET route '/echo' that returns the 'text' parameter from the request. It requires the `octoprint.plugin` and `flask` libraries. ```python import octoprint.plugin import flask class MyBlueprintPlugin(octoprint.plugin.BlueprintPlugin): @octoprint.plugin.BlueprintPlugin.route("/echo", methods=["GET"]) def myEcho(self): if not "text" in flask.request.values: abort(400, description="Expected a text to echo back.") return flask.request.values["text"] __plugin_implementation__ = MyBlueprintPlugin() ``` -------------------------------- ### Python Plugin Initialization (get_assets with LESS) Source: https://docs.octoprint.org/en/main/plugins/gettingstarted.html This Python code snippet demonstrates how to include LESS files in the `get_assets` method of an OctoPrint plugin. By referencing `less/helloworld.less`, the plugin tells OctoPrint to process and include this LESS file as a static asset. ```python import octoprint.plugin class HelloWorldPlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplatePlugin, ``` -------------------------------- ### Example POST Request for Simple API Command Source: https://docs.octoprint.org/en/main/plugins/mixins.html This is an example of a valid HTTP POST request to a SimpleApiPlugin's endpoint. It specifies the command to execute ('command2') and provides the required 'some_parameter' along with an optional parameter. ```http POST /api/plugin/mysimpleapiplugin HTTP/1.1 Host: example.com Content-Type: application/json X-Api-Key: abcdef... { "command": "command2", "some_parameter": "some_value", "some_optional_parameter": 2342 } ``` -------------------------------- ### Configure OctoPrint Plugin Assets with LESS Source: https://docs.octoprint.org/en/main/_sources/plugins/gettingstarted.rst.txt This Python code snippet demonstrates how to configure an OctoPrint plugin to include LESS files in its assets. It specifies JavaScript, CSS, and LESS files that the plugin will use. This is part of the `AssetPlugin` interface in OctoPrint. ```python import octoprint.plugin class HelloWorldPlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplatePlugin, octoprint.plugin.SettingsPlugin, octoprint.plugin.AssetPlugin): def on_after_startup(self): self._logger.info("Hello World! (more: %s)" % self._settings.get(["url"])) def get_settings_defaults(self): return dict(url="https://en.wikipedia.org/wiki/Hello_world") def get_template_configs(self): return [ dict(type="navbar", custom_bindings=False), dict(type="settings", custom_bindings=False) ] def get_assets(self): return dict( js=["js/helloworld.js"], css=["css/helloworld.css"], less=["less/helloworld.less"] ) __plugin_name__ = "Hello World" __plugin_pythoncompat__ = ">=3.7,<4" __plugin_implementation__ = HelloWorldPlugin() ``` -------------------------------- ### Use Global OctoPrint Instance (JavaScript) Source: https://docs.octoprint.org/en/main/_sources/jsclientlib/index.rst.txt Illustrates how to use the globally available `OctoPrint` instance after including the client library. This example shows setting the base URL and API key, and then making a call to list files. ```javascript OctoPrint.options.baseurl = "http://example.com/octoprint/" OctoPrint.options.apikey = "apikey"; OctoPrint.files.list() .done(function(response) { // do something with the response }); ``` -------------------------------- ### Python Plugin Configuration for OctoPrint Source: https://docs.octoprint.org/en/main/_sources/plugins/gettingstarted.rst.txt This Python code defines a basic OctoPrint plugin, 'HelloWorldPlugin', inheriting from necessary base classes. It configures settings defaults, disables custom bindings for navbar and settings templates, and logs a message on startup. ```python import octoprint.plugin class HelloWorldPlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplatePlugin, octoprint.plugin.SettingsPlugin): def on_after_startup(self): self._logger.info("Hello World! (more: %s)" % self._settings.get(["url"])) def get_settings_defaults(self): return dict(url="https://en.wikipedia.org/wiki/Hello_world") def get_template_configs(self): return [ dict(type="navbar", custom_bindings=False), dict(type="settings", custom_bindings=False) ] __plugin_name__ = "Hello World" __plugin_pythoncompat__ = ">=3.7,<4" __plugin_implementation__ = HelloWorldPlugin() ``` -------------------------------- ### Python Plugin Class with StartupMixin Source: https://docs.octoprint.org/en/main/_sources/plugins/gettingstarted.rst.txt This Python code defines a custom plugin class that subclasses OctoPrint's StartupPlugin. It overrides the on_after_startup method to add custom logging during server startup. The plugin utilizes the injected self._logger for logging purposes. ```python from octoprint.plugin import StartupPlugin class HelloWorldPlugin(StartupPlugin): def on_after_startup(self): self._logger.info("Hello World plugin started!") ``` -------------------------------- ### Install OctoPrint and Dependencies Source: https://docs.octoprint.org/en/main/_sources/development/environment.rst.txt Upgrades pip and then installs OctoPrint in editable mode, including development and plugin dependencies. This command ensures all necessary packages are installed for development. ```bash pip install --upgrade pip pip install -e '.[develop,plugins,docs]' ```