<?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: Spencer Adler</title>
    <description>The latest articles on DEV Community by Spencer Adler (@spencer_adler_880da14d230).</description>
    <link>https://dev.to/spencer_adler_880da14d230</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%2F1525651%2Faad88d14-eb57-4837-b991-18536d219728.png</url>
      <title>DEV Community: Spencer Adler</title>
      <link>https://dev.to/spencer_adler_880da14d230</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/spencer_adler_880da14d230"/>
    <language>en</language>
    <item>
      <title>Restful Routing - A Flask API Example</title>
      <dc:creator>Spencer Adler</dc:creator>
      <pubDate>Thu, 22 Aug 2024 16:54:45 +0000</pubDate>
      <link>https://dev.to/spencer_adler_880da14d230/restful-routing-a-flask-api-example-5cpb</link>
      <guid>https://dev.to/spencer_adler_880da14d230/restful-routing-a-flask-api-example-5cpb</guid>
      <description>&lt;p&gt;Restful routing is the push to make routing consistent though all different applications. REST is Representational State Transfer. It uses HTTP in a consistent, human-readable, machine-readable way. &lt;/p&gt;

&lt;p&gt;The standards are GET, POST, PATCH, PUT, and DELETE. &lt;/p&gt;

&lt;p&gt;Below will give an example of a couple of the restful routes in a flask API database to get/give the information from/to the front and perform the required action. &lt;/p&gt;

&lt;p&gt;An example of a GET for Users in the server side using flask is using the below code. &lt;/p&gt;

&lt;p&gt;First you will also need to import these items. The db import will be used later for the DELETE example. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import request (*Used for POST and PATCH*)
from config import db, api, app 

from flask_restful import Resource
from models.user import User
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Your config file should be set up like below for the imports to work. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; db = SQLAlchemy(app=app, metadata=metadata)
 api = Api(app=app)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The GET code in the User route:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; class UsersResource(Resource):
     def get(self):
        users = [user.to_dict() for user in User.query.all()]
        return users, 200

 api.add_resource(UsersResource, "/api/users", endpoint="users")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To break down this code.&lt;/p&gt;

&lt;p&gt;You will need to create a class for the Users Resource and put in the Resource as an argument. &lt;/p&gt;

&lt;p&gt;Next create a function for the get. The function will query the User table then create a list of all the users converting them to dictionaries for transfer so that they can be visible on the webpage as JSON. Then you return the list as well as a status code of 200 that the request was successful. &lt;/p&gt;

&lt;p&gt;Lastly you will need to create the resource. Name the resource you are using as well as the path and endpoint. /api is added in front of the path so that the hosting website can discern from the front and backend route. &lt;/p&gt;

&lt;p&gt;For DELETE you will have to create another resource for a single user delete. See the code below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; class UserResource(Resource):
     def delete(self, id):
         user= User.query.get(id)
         db.session.delete(user)
         db.session.commit()
         return {}, 204

 api.add_resource(UserResource, "/api/user/&amp;lt;int:id&amp;gt;", 
 endpoint="user")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To further explain what is happening in the delete that is different from the get is a few things. First it is just deleting one user so you will need to create a new resource. It is set up similarly but not plural. &lt;/p&gt;

&lt;p&gt;Then you create the delete function. It will need two arguments as you need to pass in the id that was sent from the front end to determine which user to delete. Then you will use db session delete and commit to update the database. After that you return an empty response as there is nothing to send back and the status for a delete (204). &lt;/p&gt;

&lt;p&gt;Lastly you need to create the resource. This time using the UserResource. The path will be different to be singular and have the id that was passed into the front end and the endpoint is also singular. &lt;/p&gt;

&lt;p&gt;RESTful routing makes it so when switching between different applications there is a standard that everyone can follow for backend routing and know the paths that are standardized. Additionally it makes the paths cleaner and easier to read.&lt;/p&gt;

</description>
      <category>flask</category>
      <category>restful</category>
      <category>routing</category>
      <category>python</category>
    </item>
    <item>
      <title>Creating relationships using SQLAlchemy in Python</title>
      <dc:creator>Spencer Adler</dc:creator>
      <pubDate>Thu, 01 Aug 2024 01:56:38 +0000</pubDate>
      <link>https://dev.to/spencer_adler_880da14d230/creating-relationships-using-sqlalchemy-in-python-572f</link>
      <guid>https://dev.to/spencer_adler_880da14d230/creating-relationships-using-sqlalchemy-in-python-572f</guid>
      <description>&lt;p&gt;SQLAlchemy can help with many tasks required in Python when trying to create SQL tables one of those tasks is creating relationships. &lt;/p&gt;

&lt;p&gt;Creating relationships with SQLAlchemy is made much easier than using just SQL. It streamlines the process with easier to follow syntax and less steps. &lt;/p&gt;

&lt;p&gt;SQLAlchemy is imported into Python and all of the short cut syntax can be used. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; from flask_sqlalchemy import SQLAlchemy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To create a relationship you must start by creating a table. SQLAlchemy makes this easy as well by shorting the syntax to: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Owner(db.Model, SerializerMixin):
    __tablename__ = "owners"
    id = db.Column(db.Integer, primary_key=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will create an owners table with an id that is a primary key.  Other code can be used to create the column attributes for owner. These will help with connecting the relationship. Once the columns are created you can work on the connecting the Dog table that we will create later. The code for the relationship is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dogs = db.relationship('Dog', back_populates='owner')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This connects dog to owner where an owner can have many dogs but a dog has one owner as seen by the back populates being singular. &lt;/p&gt;

&lt;p&gt;Then you can create the dog class and table. It can have a number of column attributes. The important one for the relationship is it should have an owner id with a foreign key. This is written in SQLAlchemy as: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;owner_id = db.Column(db.Integer, db.ForeignKey('owners.id')) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then like owner it will have a relationship. This one will be the opposite and connect dog to owner with the below code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;owner = db.relationship('Owner', back_populates= 'dogs')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here instead of back populates being dog it is dogs as an owner can have many dogs while the previous code was owner because a dog has one owner. &lt;/p&gt;

&lt;p&gt;Now the two tables will have a relationship. As stated before this juristically decreases the amount of code required and makes the syntax simple then having to write it in SQL. &lt;/p&gt;

&lt;p&gt;More code can be added to add an association proxy and create serialization rules which will help an infinite loop but the above will create the initial relationship a lot faster than without SQLAlchemy. &lt;/p&gt;

</description>
      <category>python</category>
      <category>sqlalchemy</category>
      <category>database</category>
      <category>flask</category>
    </item>
    <item>
      <title>Properties and attributes in Python</title>
      <dc:creator>Spencer Adler</dc:creator>
      <pubDate>Thu, 11 Jul 2024 19:17:30 +0000</pubDate>
      <link>https://dev.to/spencer_adler_880da14d230/properties-and-attributes-in-python-39aj</link>
      <guid>https://dev.to/spencer_adler_880da14d230/properties-and-attributes-in-python-39aj</guid>
      <description>&lt;p&gt;When writing code in Python there are many different functions you can write. In these functions you can create attributes and property.&lt;/p&gt;

&lt;p&gt;The definition of attributes are variables that belong to an object. The definition of properties are attributes that are controlled by methods.&lt;/p&gt;

&lt;p&gt;An example of attributes and properties are below. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Attributes:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Traveler:
    some attribute= "All members of this class will have this attribute."
    def __init__(self, name):
         self.name = name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;name is an attribute of the traveler class. Since it is inside the function it is instance attribute. &lt;/p&gt;

&lt;p&gt;Some attribute will be same for all the travelers while the name can change for each traveler. &lt;/p&gt;

&lt;p&gt;The traveler class can have many attributes like age, height etc... These attributes provide more information about the class. Similar to props in React. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Properties:&lt;/strong&gt;&lt;br&gt;
In adding to the code above you can get and set the name using some parameters. Then you would have a property for the name. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def get_name(self):
    return self._name

def set_name(self, name):
    if type(name)==str and len(name) &amp;gt; 0:
         self._name = name
    else:
         print("Name needs to be a string and longer than 0 characters.")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;name = property(get_name,set_name)&lt;/p&gt;

&lt;p&gt;get_name gets the name and then set name sets the name with the parameters in the code. When the name is input not following those parameters the console prints out an error message on what the requirements are. Then the property calls get_name and set_name when the property is called. See below for a way to call the property for name. &lt;/p&gt;

&lt;p&gt;some_traveler = Traveler(name="Spencer")&lt;/p&gt;

&lt;p&gt;name equaling Spencer is passed into the Traveler class and the property name is called. It gets the name and then sets it. Since it is a string and greater than 0 characters it is able to set it without an error message. Now when some_traveler.name is called it will be Spencer. &lt;/p&gt;

</description>
      <category>python</category>
      <category>properties</category>
      <category>attributes</category>
    </item>
    <item>
      <title>State in React</title>
      <dc:creator>Spencer Adler</dc:creator>
      <pubDate>Thu, 20 Jun 2024 20:25:29 +0000</pubDate>
      <link>https://dev.to/spencer_adler_880da14d230/state-in-react-4pmg</link>
      <guid>https://dev.to/spencer_adler_880da14d230/state-in-react-4pmg</guid>
      <description>&lt;p&gt;React has many great features to it that help create javascript. One way to impact the DOM is through state. React makes what is required in regular Javascript to update DOM much easier with fewer steps and simpler syntax. The unique feature to state is that every time it is updated the page re-renders. &lt;/p&gt;

&lt;p&gt;State is data that is dynamic in a component. It can be updated over time as the application is updated and changed by the user. All these updates will be reflected in the webpage as they are changed. &lt;/p&gt;

&lt;p&gt;When it is required to update an item that needs to be shown on the page instead of updating a variable you are to update state as you can have the issue of the variable updating but the page would not re-render and the updated variable would not be displayed. &lt;/p&gt;

&lt;p&gt;The way to use state in react is a multi step process. First you need to use the react hook:  import { useState } from "react"&lt;/p&gt;

&lt;p&gt;This lets you use react's backend information and all that is required to update state. &lt;/p&gt;

&lt;p&gt;Then you need to create a state variable. An example of this is: &lt;br&gt;
const [count, setCount] = useState(0)&lt;/p&gt;

&lt;p&gt;Above you are setting the state array for the variable count and setting the initial value of count to 0 as that is what is put in the parentheses of useState. &lt;/p&gt;

&lt;p&gt;You then can create a function to update state and every time this occurs the count variable will be updated and the page will be re-rendered to show the updated count. See the full example of this below. The starred area is the last piece of the puzzle as discussed above. &lt;/p&gt;

&lt;p&gt;import { useState } from "react"&lt;/p&gt;

&lt;p&gt;function Counter() {&lt;br&gt;
  const [count, setCount] = useState(0);&lt;/p&gt;

&lt;p&gt;**function increment() {&lt;br&gt;
    setCount(count + 1);&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;return {count};&lt;br&gt;
}**&lt;/p&gt;

</description>
      <category>react</category>
      <category>state</category>
    </item>
    <item>
      <title>Event Listeners in Javascript</title>
      <dc:creator>Spencer Adler</dc:creator>
      <pubDate>Wed, 29 May 2024 22:35:11 +0000</pubDate>
      <link>https://dev.to/spencer_adler_880da14d230/event-listeners-in-javascript-489f</link>
      <guid>https://dev.to/spencer_adler_880da14d230/event-listeners-in-javascript-489f</guid>
      <description>&lt;p&gt;Javascript is a great tool to make it so that the webpage can come alive. One way to do this in Javascript is using event listeners. Event listeners allow the user to interact with the webpage in real time. &lt;/p&gt;

&lt;p&gt;The event listener listens for an event to happen and then runs a callback function to do an actions. The event listener is added on an element in which an event will happen. &lt;/p&gt;

&lt;p&gt;The event listener is set up in a way that even if the function is anonymous it will still be run. The event listener knows to run the function in the 2nd argument when the event takes place. &lt;/p&gt;

&lt;p&gt;Typically the parameter of the function in an event listener is named event or e. Then an array of attributes is produced when the event happens and those items can be gathered by creating a variable that takes the target of the event and stores it.  &lt;/p&gt;

&lt;p&gt;Some frequently used event listeners are clicks, submits, DOMcontent loaded, and buttons. So more unique and less frequently used event listeners are ones related to the mouse's location. &lt;/p&gt;

&lt;p&gt;An example of an event listener in Javascript can be seen below:&lt;/p&gt;

&lt;p&gt;(In the HTML there would be a button element that had the ID of button.) &lt;/p&gt;

&lt;p&gt;const button= document.getElementById('button');&lt;br&gt;
button.addEventListener('click', function() {&lt;br&gt;
  alert('This button was clicked!');&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;What this code does is when the button is click a message appears on the top of the page that says. This page says: This button was clicked! To get out of that alert the user presses the ok button. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>event</category>
      <category>listeners</category>
    </item>
  </channel>
</rss>
