<?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>DEV Community: Allyn E Farach</title>
    <description>The latest articles on DEV Community by Allyn E Farach (@e_farach).</description>
    <link>https://dev.to/e_farach</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%2F675944%2F51acbad3-b2ad-456e-ba41-1f4b811c35de.png</url>
      <title>DEV Community: Allyn E Farach</title>
      <link>https://dev.to/e_farach</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/e_farach"/>
    <language>en</language>
    <item>
      <title>Migrating a Super Simple CRUD App from Flask to FastAPI</title>
      <dc:creator>Allyn E Farach</dc:creator>
      <pubDate>Fri, 30 Jul 2021 20:51:57 +0000</pubDate>
      <link>https://dev.to/e_farach/migrating-a-super-simple-crud-app-from-flask-to-fastapi-5ba7</link>
      <guid>https://dev.to/e_farach/migrating-a-super-simple-crud-app-from-flask-to-fastapi-5ba7</guid>
      <description>&lt;h2&gt;
  
  
  What is FastAPI and Why Do I Need It?
&lt;/h2&gt;

&lt;p&gt;Flask has been one of the more popular Python frameworks for many years, but it’s time for an update. Today, we’ll be discussing Flask’s faster, more efficient cousin: FastAPI. Compared to Flask, FastAPI is more performant and has Swagger documentation and other goodies built-in. If you want to run a Python web application in production in 2021, you’ll want to use FastAPI. &lt;/p&gt;

&lt;p&gt;While there are some syntactical differences between how you write Flask and how you write FastAPI, you’ll find that they’re quite similar. Today we’re going to migrate a simple CRUD app from using Flask to Fast. So follow along!&lt;/p&gt;

&lt;p&gt;Here is the original Flask code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask, request

app = Flask(__name__)

@app.route('/basic_api/entities/&amp;lt;int:entity_id&amp;gt;', methods=['GET', 'PUT', 'DELETE'])
def entity(entity_id):
    if request.method == "GET":
        return {
            'id': entity_id,
            'message': 'This endpoint should return the entity {} details'.format(entity_id),
            'method': request.method
        }
    if request.method == "PUT":
        return {
            'id': entity_id,
            'message': 'This endpoint should update the entity {}'.format(entity_id),
            'method': request.method,
            'body': request.json
        }
    if request.method == "DELETE":
        return {
            'id': entity_id,
            'message': 'This endpoint should delete the entity {}'.format(entity_id),
            'method': request.method
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we’re going to break down each piece of this Flask code, and show you the equivalent code in FastAPI. First up, importing the libraries and instantiating the application object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask, request

app = Flask(__name__)


from typing import Optional
from fastapi import FastAPI

app = FastAPI()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next up, let’s rewrite the &lt;code&gt;/basic_api/entities/&amp;lt;int:entity_id&amp;gt;&lt;/code&gt; endpoint in FastAPI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@app.route('/basic_api/entities/&amp;lt;int:entity_id&amp;gt;', methods=['GET', 'PUT', 'DELETE'])
def entity(entity_id):
   if request.method == "GET":
       return {
           'id': entity_id,
           'message': 'This endpoint should return the entity {} details'.format(entity_id),
           'method': request.method
       }
   if request.method == "PUT":
       return {
           'id': entity_id,
           'message': 'This endpoint should update the entity {}'.format(entity_id),
           'method': request.method,
           'body': request.json
       }
   if request.method == "DELETE":
       return {
           'id': entity_id,
           'message': 'This endpoint should delete the entity {}'.format(entity_id),
           'method': request.method
       }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that in FastAPI, the request methods are defined as methods on the FastAPI object, for instance &lt;code&gt;@app.get, @app.put, @app.post&lt;/code&gt;, etc rather than as a parameter. Also note that instead of stating the type of the url parameter &lt;code&gt;entity_id&lt;/code&gt;, within the route, it’s instead typed as a parameter in &lt;code&gt;entity()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@app.get('/basic_api/entities/{entity_id}')
def entity(entity_id: int):
   return {
       'id': entity_id,
       'message': 'This endpoint should return the entity {} details'.format(entity_id),
   }


@app.put('/basic_api/entities/{entity_id}')
def entity(entity_id: int, body: Entity):
   return {
       'id': entity_id,
       'message': 'This endpoint should update the entity {}'.format(entity_id),
       'body name': body.name
   }


@app.delete('/basic_api/entities/{entity_id}')
def entity(entity_id: int):
   return {
       'id': entity_id,
       'message': 'This endpoint should delete the entity {}'.format(entity_id),
   }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also note that in the put request route, we are passing along, as &lt;code&gt;body&lt;/code&gt;, an &lt;code&gt;Entity&lt;/code&gt; object. To define this object, we create a new class that inherits from &lt;code&gt;BaseModel&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;All together, the FastAPI application looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from pydantic import BaseModel


class Entity(BaseModel):
   name: str
   description: Optional[str] = None


from pydantic import BaseModel
from typing import Optional
from fastapi import FastAPI

app = FastAPI()


class Entity(BaseModel):
   name: str
   description: Optional[str] = None

@app.get('/basic_api/entities/{entity_id}')
def entity(entity_id: int):
   return {
       'id': entity_id,
       'message': 'This endpoint should return the entity {} details'.format(entity_id),
   }


@app.put('/basic_api/entities/{entity_id}')
def entity(entity_id: int, body: Entity):
   return {
       'id': entity_id,
       'message': 'This endpoint should update the entity {}'.format(entity_id),
       'body name': body.name
   }


@app.delete('/basic_api/entities/{entity_id}')
def entity(entity_id: int):
   return {
       'id': entity_id,
       'message': 'This endpoint should delete the entity {}'.format(entity_id),
   }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that’s it! You can save the &lt;code&gt;main.py&lt;/code&gt; file and run the server with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uvicorn main:app --reload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will bring the server up on &lt;code&gt;http://localhost:8000&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;One of the nice features of FastAPI is that it comes with Swagger already integrated. You can access it at &lt;code&gt;http://localhost:8000/docs&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;From there, you can test each of your endpoints to see that they work!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxji57bhff4ku3f9kx5hs.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxji57bhff4ku3f9kx5hs.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>flask</category>
      <category>python</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Setting up a Python Virtual Environment on Windows For Absolute Beginners</title>
      <dc:creator>Allyn E Farach</dc:creator>
      <pubDate>Wed, 28 Jul 2021 21:03:59 +0000</pubDate>
      <link>https://dev.to/e_farach/setting-up-a-python-virtual-environment-on-windows-for-absolute-beginners-il6</link>
      <guid>https://dev.to/e_farach/setting-up-a-python-virtual-environment-on-windows-for-absolute-beginners-il6</guid>
      <description>&lt;p&gt;&lt;strong&gt;Technical documentation online tends to focus on Mac or Linux users, with Windows users put on the back burner. As Windows users, we wanted to create a guide for a simple workflow for Python programmers that would work with Windows interfaces.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What's a Python Virtual Environment and why do you need it?
&lt;/h2&gt;

&lt;p&gt;Python applications often require additional packages and libraries as dependencies. Different applications might require different versions of these dependencies, creating a conundrum if we just install one version globally. A Python virtual environment is a solution to this problem -- it’s a siloed directory containing a particular version of Python along with particular versions of associated packages. Different applications can then use different virtual environments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Download and Install Windows Terminal
&lt;/h2&gt;

&lt;p&gt;In order to create and run our virtual environments, we need to use Windows Terminal. It's a command-line frontend that sits on top of a number of Windows command-line applications like Command Prompt and Powershell.&lt;/p&gt;

&lt;p&gt;Quick side note: you will likely already have either Command Prompt or Powershell applications installed, but you should go ahead and install Windows Terminal anyways. It’s a modern multi-tabbed frontend with lots of nice extras. We will be using it with Powershell, which has syntax that’s closer to the Linux/Mac Terminals. Note that Command Prompt uses different command syntax than Powershell does. If you find yourself in a situation where you're getting error messages despite following the instructions, double check and make sure that you're using Windows Terminal with Powershell.&lt;/p&gt;

&lt;p&gt;You'll start by installing Windows Terminal. This is where you'll be typing in the text commands to interact with your file system and run your code. To install, go to the Microsoft Store and type &lt;strong&gt;terminal&lt;/strong&gt; in the search bar. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F83hglac6511rabqitt1d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F83hglac6511rabqitt1d.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It should take you to a page with a bunch of different apps, one of them being Windows Terminal. Click on that app and download it. &lt;/p&gt;

&lt;h2&gt;
  
  
  Download and Install Python
&lt;/h2&gt;

&lt;p&gt;While Terminal is downloading, you can download Python as well.&lt;/p&gt;

&lt;p&gt;To download Python, you'll first go to &lt;a href="https://www.python.org/" rel="noopener noreferrer"&gt;the Python Website&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you're at the website, you'll click &lt;strong&gt;Downloads&lt;/strong&gt;, which is at the top center of the screen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx5ih8i4bglqfmpi9e567.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx5ih8i4bglqfmpi9e567.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
The page should now look like this. you'll click on the &lt;strong&gt;Download Python 3.9.6&lt;/strong&gt; button, and you should see the download bar pop up at the bottom of the screen, showing the status of our download.&lt;/p&gt;

&lt;p&gt;Once you see that the download is complete, you'll double click on the download itself. This should initiate the installation process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create and Activate Your Python Virtual Environment
&lt;/h2&gt;

&lt;p&gt;Open up Windows Terminal in Administrator Mode and allow Windows to Run Scripts&lt;/p&gt;

&lt;p&gt;The next few steps will be completed within Windows Terminal. Go to your Windows search bar and type in &lt;strong&gt;terminal&lt;/strong&gt;. You should see a screen pop up from the search bar, and &lt;strong&gt;Windows Terminal&lt;/strong&gt; underneath the Apps section. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fct08ceilvuuckfioo7wh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fct08ceilvuuckfioo7wh.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the arrow right next to Windows Terminal. You’ll see a bunch of different programs, including &lt;strong&gt;Command Prompt&lt;/strong&gt;, &lt;strong&gt;Azure Cloud Shell&lt;/strong&gt;, and &lt;strong&gt;PowerShell&lt;/strong&gt;. For today’s exercise, you’ll be using &lt;strong&gt;PowerShell&lt;/strong&gt;, which is the most modern version. Right click PowerShell and choose &lt;strong&gt;Run as Administrator&lt;/strong&gt;. This is because Windows will often prevent its terminals from running scripts. When you run as administrator, we can change that setting. You should now see a ready-to-go Terminal window like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbyxouzi8rfdgwxwlbpd4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbyxouzi8rfdgwxwlbpd4.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On Windows 10, the Execution Policy is set to restricted by default. This means that Powershell cannot execute any script, so the first thing you want to do is to change that. You can type&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

set-executionpolicy remotesigned


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;into the terminal. From there, Windows will ask us a long sentence that culminates in, &lt;code&gt;Do you want to change the execution policy?&lt;/code&gt;. Type &lt;br&gt;
&lt;code&gt;Y&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and hit enter. Windows will now allow you to run scripts without any trouble. &lt;/p&gt;

&lt;p&gt;A quick note: I found that after doing this once, Windows did not prevent me from running scripts again. I continued running Powershell in Administrator mode just so I could bypass any other issues that came up, but you probably shouldn't have issues with running scripts outside of Administrator mode, as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a directory for your Python application
&lt;/h3&gt;

&lt;p&gt;You usually want to create one virtual environment per new Python application, so let's create a directory for our application, say &lt;code&gt;flask_demo&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Do that by typing in:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

mkdir flask_demo


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let's go into that directory by inputting:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

cd flask_demo


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Create your Python virtual environment
&lt;/h3&gt;

&lt;p&gt;Create a virtual environment called &lt;code&gt;env&lt;/code&gt; by writing: &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

python3 -m venv env


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And hitting the &lt;strong&gt;Enter&lt;/strong&gt; button.&lt;/p&gt;

&lt;h3&gt;
  
  
  Activate the virtual environment
&lt;/h3&gt;

&lt;p&gt;You can do that by writing&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

.env\Scripts\activate


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And hitting the &lt;strong&gt;Enter&lt;/strong&gt; button again.&lt;/p&gt;

&lt;p&gt;Great, you have now created and activated your own Python virtual environment.&lt;/p&gt;

&lt;p&gt;From here, you can install Python packages in such a way that it won't interfere with your other applications that might need conflicting versions!&lt;/p&gt;

&lt;p&gt;Leave me a message and tell me what you think of my tutorial!&lt;/p&gt;

</description>
      <category>windows</category>
      <category>python</category>
      <category>virtualenv</category>
      <category>terminal</category>
    </item>
  </channel>
</rss>
