title: How can I add custom filter FastApi Jinja2 template?
body: <p>How can I use custom filter Jinja2 template in FastApi?</p>
  <p>I could find <a href="https://ttl255.com/jinja2-tutorial-part-4-template-filters/" rel="nofollow noreferrer">adding custom filter in Jinja2</a> but not specified in FastApi
  ,</p>
  <p>and other answer from stackoverflow and tried that answer but I got &quot;app doesn't have template_filter property&quot; error.</p>
  <p>If there is workaround for changing time format in template , that would be helpful, too.</p>
  <h2>main.py</h2>
  <pre><code>
  import alpaca_trade_api as tradeapi
  from polygon import RESTClient
  from fastapi import FastAPI, Request
  from fastapi.responses import HTMLResponse
  from fastapi.staticfiles import StaticFiles
  from fastapi.templating import Jinja2Templates  
  import sqlite3
  
  import config
  import helpers
  
  # getting ready alpaca api
  api = tradeapi.REST(config.ALPACA_API_KEY_ID, config.ALPACA_API_SECRET, base_url=config.ALPACA_API_URL)
  
  
  # getting ready web app 
  app = FastAPI()
  app.mount(&quot;/static&quot;, StaticFiles(directory=&quot;static&quot;), name=&quot;static&quot;)
  templates = Jinja2Templates(directory=&quot;templates&quot;)
  
  
  @app.get(&quot;/stock/120days_bars/{symbol}/&quot;)
  def home(request: Request, symbol):
  
      # RESTClient can be used as a context manager to facilitate closing the underlying http session
      # https://requests.readthedocs.io/en/master/user/advanced/#session-objects
      with RESTClient(config.POLIGON_API_KEY_ID) as client:
          
          
          from_ = helpers.get_Y_m_d_str_days_before(120)
          to = helpers.get_now_Y_m_d_str()
  
  
          # ref) https://polygon.io/docs/get_v2_aggs_ticker__stocksTicker__range__multiplier___timespan___from___to__anchor
          resp = client.stocks_equities_aggregates(symbol, 1, &quot;day&quot;, from_, to, unadjusted=False, sort='desc')
  
          print(f&quot;Minute aggregates for {resp.ticker} between {from_} and {to}.&quot;)
  
  
          context = {&quot;symbol&quot;:symbol}
          bars = []
  
          if resp.queryCount &gt; 0 :
              # there is query result
  
              bars = resp.results
              context[&quot;bars&quot;] =bars
  
  
      return templates.TemplateResponse(&quot;120days_bars.html&quot;, {&quot;request&quot;: request, &quot;context&quot;:context}) 
  </code></pre>
  <h2>custom filter</h2> 
  <pre><code>def ts_to_datetime(ts) -&gt; str:
      return datetime.datetime.fromtimestamp(ts / 1000.0).strftime('%Y-%m-%d %H:%M')
  </code></pre>
  <h2>120days_bars.html</h2>
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Stocks&lt;/title&gt;
  
          &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;{{ url_for('static', path='/semantic.min.css') }}&quot;&gt;
          &lt;script
            src=&quot;https://code.jquery.com/jquery-3.1.1.min.js&quot;
            integrity=&quot;sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=&quot;
            crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
          &lt;script src=&quot;{{ url_for('static', path='/semantic.min.js') }}&quot;&gt;&lt;/script&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
          &lt;div class=&quot;ui container&quot;&gt;
            &lt;table class=&quot;ui inverted teal table&quot;&gt;
                &lt;thead&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Time&lt;/th&gt;
                      &lt;th&gt;Volume&lt;/th&gt;
                      &lt;th&gt;VWAP&lt;/th&gt;
                      &lt;th&gt;Open&lt;/th&gt;
                      &lt;th&gt;Close&lt;/th&gt;
                      &lt;th&gt;High&lt;/th&gt;
                      &lt;th&gt;Low&lt;/th&gt;
                      &lt;th&gt;Name&lt;/th&gt;
                      &lt;th&gt;Timestamp&lt;/th&gt;
                      &lt;th&gt;Num transaction&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/thead&gt;
                  &lt;tbody&gt;
  
                      {% for bar in context.bars %}
                      &lt;tr&gt;
                          &lt;td&gt;{{bar.t | ts_to_date_str}}&lt;/td&gt;   &lt;!-- i want to use custom filter here to change time format --&gt;
                        &lt;td&gt;{{bar.v}}&lt;/td&gt;
                        &lt;td&gt;{{bar.vw}}&lt;/td&gt;
                        &lt;td&gt;{{bar.o}}&lt;/td&gt;
                        &lt;td&gt;{{bar.c}}&lt;/td&gt;
                        &lt;td&gt;{{bar.h}}&lt;/td&gt;
                        &lt;td&gt;{{bar.l}}&lt;/td&gt;
                        &lt;td&gt;{{bar.t}}&lt;/td&gt;
                        &lt;td&gt;{{bar.n}}&lt;/td&gt;
                      &lt;/tr&gt;
                      {% endfor %}
                  &lt;/tbody&gt;
              &lt;/table&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>