<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Anurag Singh</title>
    <description>The latest articles on Forem by Anurag Singh (@ashleymavericks).</description>
    <link>https://forem.com/ashleymavericks</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F127046%2Fd04ebb53-9597-47be-86f9-f9629bc3be2d.jpg</url>
      <title>Forem: Anurag Singh</title>
      <link>https://forem.com/ashleymavericks</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ashleymavericks"/>
    <language>en</language>
    <item>
      <title>Supercharge Your FastAPI Development with Browser Hot Reloading Using Arel</title>
      <dc:creator>Anurag Singh</dc:creator>
      <pubDate>Sun, 30 Jun 2024 17:51:49 +0000</pubDate>
      <link>https://forem.com/ashleymavericks/browser-hot-reloading-for-python-asgi-web-apps-using-arel-1l19</link>
      <guid>https://forem.com/ashleymavericks/browser-hot-reloading-for-python-asgi-web-apps-using-arel-1l19</guid>
      <description>&lt;p&gt;Ever found yourself muttering, “Why can’t my FastAPI app just refresh the browser automatically when I make a change?” Trust me, you’re 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 – your new best friend for adding seamless hot-reloading to your FastAPI projects. Let’s dive into how you can integrate Arel into your workflow and transform your development experience!&lt;/p&gt;

&lt;h2&gt;
  
  
  FastAPI and the Quest for Hot Reloading
&lt;/h2&gt;

&lt;p&gt;FastAPI is a fantastic, high-performance framework for building APIs with Python. It’s 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’t come with built-in browser hot-reloading. That’s where Arel steps in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing Arel
&lt;/h2&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTML templates&lt;/li&gt;
&lt;li&gt;GraphQL schemas&lt;/li&gt;
&lt;li&gt;Cached rendered Markdown content&lt;/li&gt;
&lt;li&gt;Static assets&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Key Features
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Real-time file change detection&lt;/li&gt;
&lt;li&gt;WebSocket-based browser notifications&lt;/li&gt;
&lt;li&gt;Customizable reload hooks&lt;/li&gt;
&lt;li&gt;Minimal performance overhead&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How Does Arel Work?
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing Arel
&lt;/h3&gt;

&lt;p&gt;First things first, let’s get Arel installed. Open your terminal and run:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install arel&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Package Requirements
&lt;/h3&gt;

&lt;p&gt;To ensure everything runs smoothly, make sure you have the following packages installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;arel&lt;/li&gt;
&lt;li&gt;fastapi&lt;/li&gt;
&lt;li&gt;jinja2&lt;/li&gt;
&lt;li&gt;uvicorn[standard] or websockets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can install them all at once like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install arel fastapi jinja2 uvicorn[standard]&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up Arel with FastAPI
&lt;/h2&gt;

&lt;p&gt;Let’s walk through setting up Arel in your FastAPI project. I’ve also shared a GitHub repository with a complete example you can reference.&lt;/p&gt;

&lt;h3&gt;
  
  
  Project Structure
&lt;/h3&gt;

&lt;p&gt;Here’s a quick glance at the structure of the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;├── main.py
├── templates
│   ├── base.html
│   └── index.html
└── requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Templates Configuration
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;base.html&lt;/code&gt;: Your base template sets up the structure for your HTML pages and includes the hot-reload script when in debug mode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;title&amp;gt;FastAPI with Arel&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    {% block content %}{% endblock %}

    &amp;lt;!-- Hot reload script --&amp;gt;
    {% if DEBUG %}
        {{ hot_reload.script(url_for('hot-reload')) | safe }}
    {% endif %}
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;index.html&lt;/code&gt;: This is a simple template that extends base.html and contains some content to demonstrate hot reloading.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{% extends "base.html" %}

{% block content %}
    &amp;lt;h1&amp;gt;Testing Hot Reloading&amp;lt;/h1&amp;gt;
    &amp;lt;p&amp;gt;Look! Auto hot-reloading in action. It sure makes development a breeze!&amp;lt;/p&amp;gt;
{% endblock %}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Main Application Setup
&lt;/h3&gt;

&lt;p&gt;Here’s how you can set up your main.py to integrate Arel with FastAPI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
import arel
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates

app = FastAPI()
templates = Jinja2Templates("templates")

if _debug := os.getenv("DEBUG"):
    hot_reload = arel.HotReload(paths=[arel.Path(".")])
    app.add_websocket_route("/hot-reload", route=hot_reload, name="hot-reload")
    app.add_event_handler("startup", hot_reload.startup)
    app.add_event_handler("shutdown", hot_reload.shutdown)
    templates.env.globals["DEBUG"] = _debug
    templates.env.globals["hot_reload"] = hot_reload

@app.get("/")
def index(request: Request):
    return templates.TemplateResponse("index.html", context={"request": request})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Enabling Hot Reloading
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Setting Debug in Code:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app = FastAPI(debug=True)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Set the DEBUG environment variable before starting your FastAPI server:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;DEBUG=true uvicorn main:app --reload&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command tells Uvicorn to start your FastAPI app with hot-reloading enabled.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Integrating Arel into your FastAPI project is a straightforward way to boost your development workflow with browser hot-reloading. No more tedious manual refreshes—just quick, automatic updates that let you focus on building awesome features.&lt;/p&gt;

&lt;p&gt;Ready to give it a try? Check out the &lt;a href="https://github.com/ashleymavericks/browser-hot-reloading" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt; for a complete implementation and start enhancing your FastAPI development experience today!&lt;/p&gt;

&lt;p&gt;Happy coding! 🚀&lt;/p&gt;

</description>
      <category>python</category>
      <category>fastapi</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
