{ "cells": [ { "cell_type": "raw", "metadata": { "tags": [ "skip-execution" ] }, "source": [ "---\n", "sidebar_label: \"Examples\"\n", "sidebar_position: 20\n", "---" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "# Examples\n", "\n", "Bacalhau comes pre-loaded with exciting examples to showcase its abilities and help get you started.\n", "\n", ":::tip\n", "\n", "Use the navigation bar on the left to browse through the examples. You can also view the raw codebase by visiting our [examples repository](https://github.com/bacalhau-project/examples).\n", "\n", ":::\n", "\n", "## Organisation\n", "\n", "Examples are organised by task. Each task has a number of examples that showcase different ways to solve the same problem.\n", "\n", ":::tip\n", "\n", "We're adding examples all the time. Check back soon for more!\n", "\n", ":::\n", "\n", "## Usage\n", "\n", "Each example walks you a specific task and aims at being as self-contained as possible.\n", "For starters, we recommended to start from examples whose prerequistes include only the Bacalhau client (e.g. [Python Hello World](./workload-onboarding/Prolog-Hello-World/index.md)).\n", "First read through an example to grasp its objectives and syntax, once you feel confident with those you may want to run it *interactively*.\n", "\n", "### Structure\n", "\n", "Right at the top you'll see colorful badges reading \"Open in Colab\" or \"launch binder\". These are shortcuts to execute an example in interactive mode (more on this below).\n", "\n", "After a brief introduction, each example lists a number of prerequistes. These can span from a simple Bacalhau client installation to installing a Docker daemon or NVIDIA drivers.\n", "Clearly, there's a limit as in what you can install on a machine to just try out a Bacalhau example. \n", "For instance, you cannot install NVIDIA driver on a Macbook Pro simply because they don't ship with NVIDIA GPUs.\n", "Luckily, that is no problem because for the vast majority of the examples are provided with pre-packaged cloud runtime environments (more on this in the interactive mode section).\n", "\n", "Data is typically stored externally in [Google Cloud Storae (GCS)](https://en.wikipedia.org/wiki/Google_Cloud_Storage) (for remote data examples) or [IPFS](https://docs.ipfs.tech/) (for local data examples).\n", "Sometimes examples ship with datasets stored locally and you may find references of the likes of `./data/train.csv`.\n", "\n", "\n", "Typically each example ends by downloading your Bacalhau job's outputs locally.\n", "This may feel repetitive but it's helpful to display the actual results of your job!\n", "\n", "### Syntax primer\n", "\n", "Spread across each example you'll find **blocks** like the one below.\n", "As you go through an example, you'll need to understand the nature these blocks and how to interpret them.\n", "\n", "```\n", "Hello reader, I'm a block!\n", "```\n", "\n", "\n", "Our examples are written in [Jupyter notebooks](https://jupyterlab.readthedocs.io/en/stable/index.html), a rich format that pulls together descriptive text, various **blocks** and the possibility to run bash commands from within a notebook.\n", "This gives you the power to interactively run our examples (more on this below - last teaser about this, promised :smile:)!\n", "An advantage of notebooks is that once you \"run it\", it'll store the bash commands' output in a dedicated block.\n", "Thanks to that, the static webpages you find in https://docs.bacalhau.org/examples/ are effectively \"snapshots\" of previous runs. \n", "This way, you don't necessarily need to run an example to see what it outputs!\n", "\n", ":::info\n", "\n", "To achieve the above, *some* blocks are annotated with the following [cell magics](https://ipython.readthedocs.io/en/stable/interactive/magics.html#cell-magics): `%%bash` and `%%writefile`.\n", "These tell Jupyter how to run the commands within a block.\n", "If you're just reading through the static webpage, these annotations shall inform you that block aims at either execute bash commands, or write the content of that block to a file on disk.\n", "\n", ":::\n", "\n", "Thus, blocks can be:\n", "\n", "* simple text snippets: these are used to display generic text in a dedicated block.\n", "* bash commands (annotated with `%%bash` in the first line): these can be run in the interactive mode! If you wish to use your own terminal to launch these commands, just ignore the `%%bash` line.\n", "* write to file (annotated with `%%writefile ` in the first line): these inform you the remainder of the example will expect `` to be stored on disk. We use these blocks to:\n", " * Firstly, show you what's inside a file - for instance, this may display the content of a python script.\n", " * Secondly, when running an example in interactive mode, Jupyter will effectively write out the content of the block to disk, in ``.\n", "\n", "Since our examples are runnable, we render the effective run in static web pages to give you a glimpse of what to expect if you run it yourself.\n", "This means we're able to show you what the output of a command is, right below the command block.\n", "For instance, when you run into two consecutive blocks (see below), the former represents a command block and the latter depicts its output.\n", "\n", "```python\n", "%%bash\n", "date\n", "```\n", "\n", " Wed Feb 15 13:21:35 CET 2029\n", "\n", "\n", "### Interactive mode\n", "\n", "If you're trying to run an example by yourself, this section contains the instructions you're looking for.\n", "As stated previously, our examples are written in Jupyter notebook and this gives you the possibility to run its steps one by one, edit them, or simply run them all in a sequence from top to bottom.\n", "This is convenient because while the accompanying text guides you through the example (and hopefully provides enough context), you have to possibility to edit and re-run each step.\n", "\n", ":::tip\n", "\n", "Are you not familiar with Jupyter notebooks but wish to run our examples interactively?\n", "Please stop right here and take a moment to learn more about it in its [official docs](https://jupyterlab.readthedocs.io/en/stable/index.html).\n", "This [video on YouTube (7min)](https://www.youtube.com/watch?v=jZ952vChhuI) is a perfect quick introduction to Jupyter Notebook.\n", "See also [the difference between Jupyter Notebook and JupyterLab](https://stackoverflow.com/questions/50982686/what-is-the-difference-between-jupyter-notebook-and-jupyterlab/).\n", "\n", ":::\n", "\n", "Finally, how to run our examples interactively?\n", "You have two options:\n", "\n", "#### Run examples locally\n", "\n", "1. Pick an example you'd like to run\n", "1. Find the corresponding `.ipynb` file in https://github.com/bacalhau-project/examples\n", "1. Pull the example repo locally: `git clone https://github.com/bacalhau-project/examples.git`\n", "1. Install Jupyter Notebook or preferably [install JupyterLab](https://jupyterlab.readthedocs.io/en/stable/getting_started/installation.html) locally\n", "1. Launch JupyterLab, run `jupyter lab` in a terminal (see the [official docs for more details](https://jupyterlab.readthedocs.io/en/stable/getting_started/starting.html))\n", "1. Use the [Jupyter interface](https://jupyterlab.readthedocs.io/en/stable/user/interface.html) to interact with the example of your choice\n", "\n", "Can you run notebooks in VS Code? Install [the related extension](https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter), then clone the repo as in the steps above.\n", "\n", ":::tip\n", "\n", "Spare yourself the hustle of installing all the above and use a hosted Jupyter service instead (see section below).\n", "\n", ":::\n", "#### Use a hosted Jupyter service (recommended!)\n", "\n", "Most examples come with badges on the top of the page \"Google \"mybinder.org.\n", "Those badges are clicckable and they'll open a Colab/Binder workspace (in the cloud) with the notebook and related files ready-to-go.\n", "They typically work out-of-the-box and perfectly support installing tools like the Bacalhau client or a python library.\n", "However, it must be noted they may complain when trying to install advanced pre-requisites that require system-level components (e.g. Docker). When you run into that case, you'll have to resort to running the notebook and installing all of its dependencies locally (see section above).\n", "\n", "## Developer Information\n", "\n", "All of the examples are open source and available on GitHub. The examples exist in a separate repository located at https://github.com/bacalhau-project/examples/. Please see the [README.md for more instructions on how to contribute](https://github.com/bacalhau-project/examples/README.md).\n", "\n", "Note that the code for the rest of the documentation (this website) [is located in a separate repository](https://github.com/bacalhau-project/docs.bacalhau.org/)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" }, "vscode": { "interpreter": { "hash": "21fd917facdca5c02b7d24e32528f1b4e6711465b0262edbfffba943391e1222" } } }, "nbformat": 4, "nbformat_minor": 4 }