{ "cells": [ { "cell_type": "markdown", "id": "d745c9ae-31f8-47e5-83df-42c0a39e700e", "metadata": {}, "source": [ "# Setting up FastAPI with Uvicorn and Openrc to serve Jupyter Notebook generated html files." ] }, { "cell_type": "markdown", "id": "5dec85d7-1ac2-4bdd-a5c4-40dc8ec80c1f", "metadata": {}, "source": [ "## Prerequisites:\n", "- system with openrc\n", "- fastapi\n", "- uvicorn\n", "- jupyter notebook (optional)\n", "- nginx" ] }, { "cell_type": "markdown", "id": "3d5a9177-1fcd-4bc6-97dd-f85c712e8a9e", "metadata": {}, "source": [ "## Creating FastAPI application" ] }, { "cell_type": "markdown", "id": "6f2751d9-cdfe-417e-8a30-8768e2fd828e", "metadata": {}, "source": [ "Create a directory for project and enter it:" ] }, { "cell_type": "code", "execution_count": null, "id": "58fe2a08-afb0-400b-a8a2-063da88a2559", "metadata": {}, "outputs": [], "source": [ "$ mkdir -p ~/Fastapi_blog/static && cd ~/Fastapi_blog" ] }, { "cell_type": "markdown", "id": "2ad52653-7f16-489f-8a4b-e2fd08f90d05", "metadata": {}, "source": [ "Create a file containing code and html template folder and template for the main page:" ] }, { "cell_type": "code", "execution_count": null, "id": "27e461b5-279f-4143-ac69-5ef61b75e6af", "metadata": {}, "outputs": [], "source": [ "$ touch main.py\n", "$ mkdir templates\n", "$ touch templates/main_page.html" ] }, { "cell_type": "markdown", "id": "9c3d9c08-81ea-43b0-bea8-8486adb06d85", "metadata": {}, "source": [ "main_page.html can be really minimal:" ] }, { "cell_type": "code", "execution_count": null, "id": "e6a61c2c-708e-462d-b487-0ddd23b5e08a", "metadata": {}, "outputs": [], "source": [ "\n", "
\n", "Bob's your uncle!
\n", "\n", "\n", " " ] }, { "cell_type": "markdown", "id": "fd3b37dc-e3d3-48a0-a496-7f0060bc69c0", "metadata": {}, "source": [ "Visit http://localhost:8000/\n", "You should see the main page with test article and you should be able to navigate to the article clicking the link." ] }, { "cell_type": "markdown", "id": "cba92c27-c741-4959-88c5-b9790de7eddf", "metadata": {}, "source": [ "## Jupyter side note" ] }, { "cell_type": "markdown", "id": "f7fefa06-9d13-4793-8bb6-3f96f2171f25", "metadata": {}, "source": [ "To write stuff with Jupyter Notebook start it up:" ] }, { "cell_type": "code", "execution_count": null, "id": "f7eadb7f-e811-4bee-a8d7-68c68ec9f9ca", "metadata": {}, "outputs": [], "source": [ "$ jupyter-notebook" ] }, { "cell_type": "markdown", "id": "d0ce0f2c-e633-4974-8ed4-1f4b3a021d6a", "metadata": {}, "source": [ "In browser a new tab with jupyter notebook should have started. Write your fun stuff. Save it. If you don't want to execute cell content convert to html with:" ] }, { "cell_type": "code", "execution_count": null, "id": "7a4da315-d47b-4bec-8b17-f2cbf494f4d4", "metadata": {}, "outputs": [], "source": [ "$ jupyter nbconvert --to html