{"@attributes":{"version":"2.0"},"channel":{"title":"DEV Community: Anurag Singh","description":"The latest articles on DEV Community by Anurag Singh (@ashleymavericks).","link":"https:\/\/dev.to\/ashleymavericks","image":{"url":"https:\/\/media2.dev.to\/dynamic\/image\/width=90,height=90,fit=cover,gravity=auto,format=auto\/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F127046%2Fd04ebb53-9597-47be-86f9-f9629bc3be2d.jpg","title":"DEV Community: Anurag Singh","link":"https:\/\/dev.to\/ashleymavericks"},"language":"en","item":{"title":"Supercharge Your FastAPI Development with Browser Hot Reloading Using Arel","pubDate":"Sun, 30 Jun 2024 17:51:49 +0000","link":"https:\/\/dev.to\/ashleymavericks\/browser-hot-reloading-for-python-asgi-web-apps-using-arel-1l19","guid":"https:\/\/dev.to\/ashleymavericks\/browser-hot-reloading-for-python-asgi-web-apps-using-arel-1l19","description":"<p>Ever found yourself muttering, \u201cWhy can\u2019t my FastAPI app just refresh the browser automatically when I make a change?\u201d Trust me, you\u2019re not alone. As a web developer working with Python ASGI frameworks like FastAPI for a full stack application, those constant manual reloads can be a real buzzkill. Enter Arel \u2013 your new best friend for adding seamless hot-reloading to your FastAPI projects. Let\u2019s dive into how you can integrate Arel into your workflow and transform your development experience!<\/p>\n\n<h2>\n  \n  \n  FastAPI and the Quest for Hot Reloading\n<\/h2>\n\n<p>FastAPI is a fantastic, high-performance framework for building APIs with Python. It\u2019s built on top of ASGI (Asynchronous Server Gateway Interface) and leverages the Uvicorn web server to deliver a sleek and efficient development experience. However, out of the box, FastAPI doesn\u2019t come with built-in browser hot-reloading. That\u2019s where Arel steps in.<\/p>\n\n<h2>\n  \n  \n  Introducing Arel\n<\/h2>\n\n<p>Arel is a lightweight library designed to implement development-only hot-reloading for non-Python files that are not dynamically read from disk on each request. This includes:<\/p>\n\n<ul>\n<li>HTML templates<\/li>\n<li>GraphQL schemas<\/li>\n<li>Cached rendered Markdown content<\/li>\n<li>Static assets<\/li>\n<\/ul>\n\n<h3>\n  \n  \n  Key Features\n<\/h3>\n\n<ul>\n<li>Real-time file change detection<\/li>\n<li>WebSocket-based browser notifications<\/li>\n<li>Customizable reload hooks<\/li>\n<li>Minimal performance overhead<\/li>\n<\/ul>\n\n<h3>\n  \n  \n  How Does Arel Work?\n<\/h3>\n\n<p>Arel keeps an eye on the files you specify. When it detects a change, it sends a notification to the browser via WebSocket. An injected client script then triggers a page reload, ensuring your latest changes are visible immediately. You can also register custom reload hooks for any additional server-side operations, such as refreshing cached content or re-initializing other resources.<\/p>\n\n<h3>\n  \n  \n  Installing Arel\n<\/h3>\n\n<p>First things first, let\u2019s get Arel installed. Open your terminal and run:<\/p>\n\n<p><code>pip install arel<\/code><\/p>\n\n<h3>\n  \n  \n  Package Requirements\n<\/h3>\n\n<p>To ensure everything runs smoothly, make sure you have the following packages installed:<\/p>\n\n<ul>\n<li>arel<\/li>\n<li>fastapi<\/li>\n<li>jinja2<\/li>\n<li>uvicorn[standard] or websockets<\/li>\n<\/ul>\n\n<p>You can install them all at once like this:<\/p>\n\n<p><code>pip install arel fastapi jinja2 uvicorn[standard]<\/code><\/p>\n\n<h2>\n  \n  \n  Setting Up Arel with FastAPI\n<\/h2>\n\n<p>Let\u2019s walk through setting up Arel in your FastAPI project. I\u2019ve also shared a GitHub repository with a complete example you can reference.<\/p>\n\n<h3>\n  \n  \n  Project Structure\n<\/h3>\n\n<p>Here\u2019s a quick glance at the structure of the project:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>\u251c\u2500\u2500 main.py\n\u251c\u2500\u2500 templates\n\u2502   \u251c\u2500\u2500 base.html\n\u2502   \u2514\u2500\u2500 index.html\n\u2514\u2500\u2500 requirements.txt\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<h3>\n  \n  \n  Templates Configuration\n<\/h3>\n\n<p><code>base.html<\/code>: Your base template sets up the structure for your HTML pages and includes the hot-reload script when in debug mode.<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;title&gt;FastAPI with Arel&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    {% block content %}{% endblock %}\n\n    &lt;!-- Hot reload script --&gt;\n    {% if DEBUG %}\n        {{ hot_reload.script(url_for('hot-reload')) | safe }}\n    {% endif %}\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p><code>index.html<\/code>: This is a simple template that extends base.html and contains some content to demonstrate hot reloading.<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>{% extends \"base.html\" %}\n\n{% block content %}\n    &lt;h1&gt;Testing Hot Reloading&lt;\/h1&gt;\n    &lt;p&gt;Look! Auto hot-reloading in action. It sure makes development a breeze!&lt;\/p&gt;\n{% endblock %}\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<h3>\n  \n  \n  Main Application Setup\n<\/h3>\n\n<p>Here\u2019s how you can set up your main.py to integrate Arel with FastAPI:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>import os\nimport arel\nfrom fastapi import FastAPI, Request\nfrom fastapi.templating import Jinja2Templates\n\napp = FastAPI()\ntemplates = Jinja2Templates(\"templates\")\n\nif _debug := os.getenv(\"DEBUG\"):\n    hot_reload = arel.HotReload(paths=[arel.Path(\".\")])\n    app.add_websocket_route(\"\/hot-reload\", route=hot_reload, name=\"hot-reload\")\n    app.add_event_handler(\"startup\", hot_reload.startup)\n    app.add_event_handler(\"shutdown\", hot_reload.shutdown)\n    templates.env.globals[\"DEBUG\"] = _debug\n    templates.env.globals[\"hot_reload\"] = hot_reload\n\n@app.get(\"\/\")\ndef index(request: Request):\n    return templates.TemplateResponse(\"index.html\", context={\"request\": request})\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<h3>\n  \n  \n  Enabling Hot Reloading\n<\/h3>\n\n<p>To activate hot reloading, you need to set the DEBUG flag to True. You can do this directly in your FastAPI application or by using an environment variable.<\/p>\n\n<p>Setting Debug in Code:<\/p>\n\n<p><code>app = FastAPI(debug=True)<\/code><\/p>\n\n<p>Set the DEBUG environment variable before starting your FastAPI server:<\/p>\n\n<p><code>DEBUG=true uvicorn main:app --reload<\/code><\/p>\n\n<p>This command tells Uvicorn to start your FastAPI app with hot-reloading enabled.<\/p>\n\n<h2>\n  \n  \n  Wrapping Up\n<\/h2>\n\n<p>Integrating Arel into your FastAPI project is a straightforward way to boost your development workflow with browser hot-reloading. No more tedious manual refreshes\u2014just quick, automatic updates that let you focus on building awesome features.<\/p>\n\n<p>Ready to give it a try? Check out the <a href=\"https:\/\/github.com\/ashleymavericks\/browser-hot-reloading\" rel=\"noopener noreferrer\">GitHub repository<\/a> for a complete implementation and start enhancing your FastAPI development experience today!<\/p>\n\n<p>Happy coding! \ud83d\ude80<\/p>\n\n","category":["python","fastapi","tutorial","productivity"]}}}