<p dir="auto">Is there any equivalent approach for accessing fastapi application from IPython shell context?<br>
Like we use python manage.py shell in flask?</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from fastapi import Depends, FastAPI, HTTPException, Request, Response
from typing import Optional
from jinjasql import JinjaSql


app = FastAPI()

@app.get(&quot;/items/&quot;)
async def read_item(request: Request, user_id: Optional[int] = 0, project_id: Optional[int] = 0):
    request_query_params = {item[0]: item[1] for item in request.query_params.multi_items() }
    print(request_query_params)

    template = &quot;&quot;&quot;
    select username, sum(spend)
    from transactions
    where 1 = 1
    {% if user_id %}
    and user_id = {{ user_id }}
    {% endif %}
    {% if project_id %}
    and project_id = {{ project_id }}
    {% endif %}
    &quot;&quot;&quot;
    j = JinjaSql()
    query, bind_params = j.prepare_query(template, request_query_params)

    return query"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">fastapi</span> <span class="pl-k">import</span> <span class="pl-v">Depends</span>, <span class="pl-v">FastAPI</span>, <span class="pl-v">HTTPException</span>, <span class="pl-v">Request</span>, <span class="pl-v">Response</span>
<span class="pl-k">from</span> <span class="pl-s1">typing</span> <span class="pl-k">import</span> <span class="pl-v">Optional</span>
<span class="pl-k">from</span> <span class="pl-s1">jinjasql</span> <span class="pl-k">import</span> <span class="pl-v">JinjaSql</span>


<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-v">FastAPI</span>()

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">get</span>(<span class="pl-s">"/items/"</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">read_item</span>(<span class="pl-s1">request</span>: <span class="pl-v">Request</span>, <span class="pl-s1">user_id</span>: <span class="pl-v">Optional</span>[<span class="pl-s1">int</span>] <span class="pl-c1">=</span> <span class="pl-c1">0</span>, <span class="pl-s1">project_id</span>: <span class="pl-v">Optional</span>[<span class="pl-s1">int</span>] <span class="pl-c1">=</span> <span class="pl-c1">0</span>):
    <span class="pl-s1">request_query_params</span> <span class="pl-c1">=</span> {<span class="pl-s1">item</span>[<span class="pl-c1">0</span>]: <span class="pl-s1">item</span>[<span class="pl-c1">1</span>] <span class="pl-k">for</span> <span class="pl-s1">item</span> <span class="pl-c1">in</span> <span class="pl-s1">request</span>.<span class="pl-s1">query_params</span>.<span class="pl-en">multi_items</span>() }
    <span class="pl-en">print</span>(<span class="pl-s1">request_query_params</span>)

    <span class="pl-s1">template</span> <span class="pl-c1">=</span> <span class="pl-s">"""</span>
<span class="pl-s">    select username, sum(spend)</span>
<span class="pl-s">    from transactions</span>
<span class="pl-s">    where 1 = 1</span>
<span class="pl-s">    {% if user_id %}</span>
<span class="pl-s">    and user_id = {{ user_id }}</span>
<span class="pl-s">    {% endif %}</span>
<span class="pl-s">    {% if project_id %}</span>
<span class="pl-s">    and project_id = {{ project_id }}</span>
<span class="pl-s">    {% endif %}</span>
<span class="pl-s">    """</span>
    <span class="pl-s1">j</span> <span class="pl-c1">=</span> <span class="pl-v">JinjaSql</span>()
    <span class="pl-s1">query</span>, <span class="pl-s1">bind_params</span> <span class="pl-c1">=</span> <span class="pl-s1">j</span>.<span class="pl-en">prepare_query</span>(<span class="pl-s1">template</span>, <span class="pl-s1">request_query_params</span>)

    <span class="pl-k">return</span> <span class="pl-s1">query</span></pre></div>
<h3 dir="auto">Description</h3>
<ul dir="auto">
<li>Open the browser and call the endpoint <code class="notranslate">http://127.0.0.1:8000/items/?project_id=2</code>.</li>
<li>It returns <code class="notranslate">"\n    select username, sum(spend)\n    from transactions\n    where 1 = 1\n    \n    \n    and project_id = %s\n    \n    "</code></li>
<li>query parameter <code class="notranslate">user_id </code> that has a default value 0 is missing.</li>
</ul>
<p dir="auto">I wish to have <code class="notranslate">request_query_params</code> include the default value too. It can save some typing when there is a lot of query parameters.</p>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>OS: Windows</li>
<li>FastAPI Version 0.61.1:</li>
<li>Python version: 3.8.2</li>
</ul>