<?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: Luna Daniels</title>
    <description>The latest articles on Forem by Luna Daniels (@alelda).</description>
    <link>https://forem.com/alelda</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%2F1049123%2Fad07b1f9-850e-45f3-bdc5-56e28ba33edd.png</url>
      <title>Forem: Luna Daniels</title>
      <link>https://forem.com/alelda</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/alelda"/>
    <language>en</language>
    <item>
      <title>Flask-SQLAlchemy &amp; React js</title>
      <dc:creator>Luna Daniels</dc:creator>
      <pubDate>Fri, 09 Jun 2023 22:28:19 +0000</pubDate>
      <link>https://forem.com/alelda/flask-sqlalchemy-react-js-4ll4</link>
      <guid>https://forem.com/alelda/flask-sqlalchemy-react-js-4ll4</guid>
      <description>&lt;p&gt;Hey there, tech enthusiasts! Today, I'm going to dive into the wonderful world of web development and guide you through the process of connecting a Flask-SQLAlchemy backend to a React.js frontend.&lt;/p&gt;

&lt;p&gt;First things first, let's talk about Flask-SQLAlchemy. Flask-SQLAlchemy is a powerful combination of Flask, a lightweight Python web framework, and SQLAlchemy, a flexible and intuitive SQL toolkit. Together, they make a solid team that allows us to easily interact with databases in our Flask applications.&lt;/p&gt;

&lt;p&gt;Now, onto React.js – a JavaScript library for building user interfaces. React.js has gained immense popularity due to its component-based architecture, reusability, and efficiency. It's the perfect fit for creating stunning frontends that seamlessly interact with the backend.&lt;/p&gt;

&lt;p&gt;So, how do we connect these two powerhouses? Well, let me break it down for you. The key lies in building a solid API that acts as a bridge between the Flask backend and the React frontend. This API will handle all the requests and responses, ensuring smooth communication between the two.&lt;/p&gt;

&lt;p&gt;First, let's set up our Flask-SQLAlchemy backend. We'll define our models, create the necessary routes, and handle the CRUD operations. SQLAlchemy makes it incredibly easy to work with databases by abstracting the SQL queries, so you can focus on the logic of your application.&lt;/p&gt;

&lt;p&gt;Once we have our backend up and running, it's time to shift our focus to the React.js frontend. We'll start by setting up our project, installing the necessary dependencies, and organizing our components. Then, we'll create the UI elements, such as forms and buttons, to interact with the backend.&lt;/p&gt;

&lt;p&gt;To connect our frontend to the backend API, we'll use JavaScript's built-in fetch function or popular libraries like Axios to make HTTP requests. We'll send requests to the appropriate endpoints on the Flask server, handle the responses, and update the UI accordingly. Remember, communication is the key to a successful relationship between the frontend and the backend!&lt;/p&gt;

&lt;p&gt;We can take this connection to the next level by implementing real-time updates using technologies like WebSockets or server-sent events. This way, any changes made on the backend can be instantly reflected on the frontend, providing a seamless user experience.&lt;/p&gt;

&lt;p&gt;As you embark on this journey of connecting Flask-SQLAlchemy to React.js, keep in mind that patience and persistence are your best friends. It might feel overwhelming at times, but trust me, the feeling of accomplishment when everything falls into place is absolutely worth it.&lt;/p&gt;

&lt;p&gt;So, fellow developers, go forth and conquer! Connect your Flask-SQLAlchemy backend to your React.js frontend, unleash your creativity, and build amazing web applications that leave a lasting impression. Happy coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Models in Python</title>
      <dc:creator>Luna Daniels</dc:creator>
      <pubDate>Thu, 18 May 2023 16:07:21 +0000</pubDate>
      <link>https://forem.com/alelda/models-in-python-4imd</link>
      <guid>https://forem.com/alelda/models-in-python-4imd</guid>
      <description>&lt;p&gt;Models are an essential part of many Python applications, allowing us to represent and work with data in a structured way. In this tutorial, we'll cover the basics of creating and using models in Python. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Model?
&lt;/h2&gt;

&lt;p&gt;A model is a representation or blueprint that describes the structure and behavior of a real-world entity or concept. In Python, models are typically created using classes, which define the attributes and methods that represent the model's properties and actions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use Models?
&lt;/h2&gt;

&lt;p&gt;Models help organize and manipulate data efficiently. They provide a way to encapsulate related data and operations, making code more modular, reusable, and maintainable. Models also allow us to apply complex algorithms and techniques to analyze and process data effectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining a Model Class
&lt;/h2&gt;

&lt;p&gt;In Python, models are often defined as classes, which act as blueprints for creating instances (objects) of the model. To create a model class, define it using the class keyword followed by the class name. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car:
    # class definition
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Adding Attributes
&lt;/h2&gt;

&lt;p&gt;Attributes represent the characteristics or properties of a model. To add attributes to a model class, define them within the class body. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Implementing Methods
&lt;/h2&gt;

&lt;p&gt;Methods define the actions or behaviors associated with a model. You can define methods inside the model class, just like regular functions. For example, let's add a method to our Car class that allows us to start the engine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

    def start_engine(self):
        print("Engine started!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating an Instance
&lt;/h2&gt;

&lt;p&gt;To work with a model, we need to create an instance (object) of that model. To create an instance, call the class name as if it were a function, passing any required arguments defined in the class's &lt;strong&gt;init&lt;/strong&gt; method. For example, let's create an instance of the Car class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_car = Car("Tesla", "Model S", 2023)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Accessing Attributes
&lt;/h2&gt;

&lt;p&gt;Once we have an instance, we can access its attributes using the dot notation (instance.attribute). For example, to access the make of my_car, we can use my_car.make. Let's print the car's make, model, and year:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(my_car.make)   # Output: Tesla
print(my_car.model)  # Output: Model S
print(my_car.year)   # Output: 2023
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Calling Methods
&lt;/h2&gt;

&lt;p&gt;Similarly, we can call the methods defined in the model class using the dot notation (instance.method()). For example, let's start the engine of my_car:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_car.start_engine()   # Output: Engine started!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulations! You have now learned the basics of creating and using models in Python. Models provide a powerful way to structure and manipulate data in your applications. Remember to experiment and explore further to deepen your understanding. Happy coding!&lt;/p&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>Lets talk about React Hooks</title>
      <dc:creator>Luna Daniels</dc:creator>
      <pubDate>Mon, 01 May 2023 15:59:03 +0000</pubDate>
      <link>https://forem.com/alelda/lets-talk-about-react-hooks-8h9</link>
      <guid>https://forem.com/alelda/lets-talk-about-react-hooks-8h9</guid>
      <description>&lt;p&gt;React Hooks is a super cool feature that was introduced in React 16.8, and it's the hot new thing that all the cool kids are using these days. It basically lets you use all the cool stuff that you could only do in class components, but now you can do it in functional components too! That means you can write your components in a more concise and organized way, which is pretty awesome.&lt;/p&gt;

&lt;p&gt;Hooks are like little functions that give you superpowers to hook into the React rendering and lifecycle process. They each have their own special powers and can be used to manage different aspects of component logic. useState is a tool belt that lets you manage state in a functional component, while useEffect is like a magic wand that lets you add lifecycle methods.&lt;/p&gt;

&lt;p&gt;The cool thing about using hooks is that they make it easier to write reusable code, which is super important when you're trying to build complex apps. They also make your code more straightforward and easier to read, which is like a breath of fresh air in a world of spaghetti code.&lt;/p&gt;

&lt;p&gt;And, let's not forget about performance. Hooks can help optimize your app by reducing unnecessary re-renders and improving state updates. It's like having a speed boost for your app, and who doesn't love that?&lt;/p&gt;

&lt;p&gt;So, in short, React Hooks is a really cool feature that you should definitely be using. It makes your code more organized, easier to read, and more performant. And, if you're not using it, then you're totally missing out on the fun!&lt;/p&gt;

&lt;p&gt;Here's a couple of the most used hooks in React:&lt;/p&gt;

&lt;h2&gt;
  
  
  useState
&lt;/h2&gt;

&lt;p&gt;The useState hook is one of the most commonly used hooks in React. It lets you add state to a functional component, which means you can manage and update the state of the component without needing to use class components.&lt;/p&gt;

&lt;p&gt;To use useState, you first need to import it from the React library. Then, you can call the useState function and pass in an initial state value. The function returns an array with two values: the current state value, and a function that you can call to update the state.&lt;/p&gt;

&lt;p&gt;Here's an example of how you might use useState in a functional component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  function handleClick() {
    setCount(count + 1);
  }

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;You've clicked the button {count} times&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={handleClick}&amp;gt;Click me!&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're using useState to manage the count state value. We start with an initial value of 0, and we pass that in as the argument to useState. The count value is the current state value, and the setCount function is what we use to update the state.&lt;/p&gt;

&lt;p&gt;In the handleClick function, we call setCount and pass in count + 1. This updates the count state value and triggers a re-render of the component, which means the UI will update to reflect the new value of count.&lt;/p&gt;

&lt;p&gt;Overall, useState is a really powerful tool that makes it easy to manage state in functional components. By using it, you can write more concise and organized code, and you don't need to use class components just to manage state.&lt;/p&gt;

&lt;h2&gt;
  
  
  useEffect
&lt;/h2&gt;

&lt;p&gt;The useEffect hook is another widely used hook in React, and it lets you add side effects to functional components.&lt;/p&gt;

&lt;p&gt;In React, a side effect is any code that affects something outside of the component, such as fetching data from an API, setting up event listeners, or updating the document title. In class components, you would use lifecycle methods like componentDidMount and componentDidUpdate to add side effects. However, in functional components, you can use useEffect to achieve the same functionality.&lt;/p&gt;

&lt;p&gt;To use useEffect, you call it inside your functional component and pass in two arguments: a function that represents the side effect you want to add, and an array of dependencies that specify when the side effect should be triggered.&lt;/p&gt;

&lt;p&gt;Here's an example of how you might use useEffect to fetch data from an API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';

function MyComponent() {
  const [data, setData] = useState(null);

  useEffect(() =&amp;gt; {
    fetch('https://api.example.com/data')
      .then(response =&amp;gt; response.json())
      .then(data =&amp;gt; setData(data));
  }, []);

  if (!data) {
    return &amp;lt;p&amp;gt;Loading...&amp;lt;/p&amp;gt;;
  }

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;{data.title}&amp;lt;/h1&amp;gt;
      &amp;lt;p&amp;gt;{data.description}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're using useEffect to fetch data from an API and update the component's state with that data. We start with an initial state of null for data, and we use useState to manage the state.&lt;/p&gt;

&lt;p&gt;In the useEffect call, we pass in a function that uses the fetch API to get data from an external API. We then update the component's state with the response data using setData. The empty array ([]) passed as the second argument tells React that the side effect should only be triggered once, when the component mounts.&lt;/p&gt;

&lt;p&gt;In the component's render method, we check whether data is still null. If it is, we display a loading message. Otherwise, we display the data we fetched from the API.&lt;/p&gt;

&lt;p&gt;Overall, useEffect is a really powerful tool that lets you add side effects to functional components. By using it, you can easily fetch data from APIs, set up event listeners, and more, all without needing to use class components or lifecycle methods.&lt;/p&gt;

&lt;p&gt;React hooks are a powerful and versatile feature of the React library that allow developers to add state and side effects to functional components. They were introduced in React version 16.8, and have since become a staple of modern React development.&lt;/p&gt;

&lt;p&gt;There are many different hooks available, each with their own specific use cases. The useState hook lets you add state to a functional component, while the useEffect hook lets you add side effects. Other hooks, like useContext, useRef, and useReducer, provide additional functionality that can help you build more complex and feature-rich applications.&lt;/p&gt;

&lt;p&gt;One of the key benefits of using React hooks is that they make it easier to write more concise and reusable code. By breaking down your components into smaller, more modular pieces, you can create more maintainable and scalable codebases.&lt;/p&gt;

&lt;p&gt;Overall, React hooks are a powerful and flexible tool that can help you build modern, efficient, and robust web applications. Whether you're building a simple app or a complex enterprise solution, React hooks are definitely worth learning and mastering.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Fetch() Requests</title>
      <dc:creator>Luna Daniels</dc:creator>
      <pubDate>Tue, 04 Apr 2023 21:23:27 +0000</pubDate>
      <link>https://forem.com/alelda/fetch-requests-2obe</link>
      <guid>https://forem.com/alelda/fetch-requests-2obe</guid>
      <description>&lt;p&gt;So, fetch() requests. What are they? A fetch() request is a way to interact with a server. It returns a Promise that resolves to the Response object representing the response to the request. &lt;/p&gt;

&lt;p&gt;"How does one do this?" you might ask. Well, for starters, we need an API (Application Programming Interface), a server or a mock server to be able to run the request. To do this, there are many different ways, however for this example we will be using a JSON server. &lt;/p&gt;

&lt;p&gt;JSON (JavaScript Object Notation) is a data interchange format. JSON sends data between frontends and backends. A JSON server is a node package that can turn a JSON file on your computer into mock data storage.&lt;/p&gt;

&lt;p&gt;To set up the JSON server, install JSON globally by running the command below in your terminal:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ npm install -g json-server&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After installing, create a file that will act as storage data:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ touch db.json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To start the JSON server, run the following code in the same directory as the db.json file:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ json-server --watch db.json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And you're good to go! You should now have a JSON server up and running that you can communicate with. Now for the communication part.&lt;/p&gt;

&lt;p&gt;There's a few different things you can do with fetch(). The main four are GET, POST, PATCH &amp;amp; DELETE. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GET: Allows you to pull data.&lt;/li&gt;
&lt;li&gt;POST: Allows you to send data&lt;/li&gt;
&lt;li&gt;PATCH: Allows you to update data&lt;/li&gt;
&lt;li&gt;DELETE: A bit self explanatory, but it allows you to remove data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A simple GET request could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch('https://example.com/data.json')
  .then(response =&amp;gt; response.json())
  .then(data =&amp;gt; console.log(data))
  .catch(error =&amp;gt; console.error(error));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're making a GET request to '&lt;a href="https://example.com/data.json"&gt;https://example.com/data.json&lt;/a&gt;'. We're then calling the json() method on the response object to parse the response as JSON. We're using the second then() method to log the data to the console. Finally, we're using the catch() method to log any errors that occur during the fetch request.&lt;/p&gt;

&lt;p&gt;A POST request could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch('https://example.com/api/endpoint', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ name: 'John Doe', email: 'john.doe@example.com' })
})
  .then(response =&amp;gt; response.json())
  .then(data =&amp;gt; console.log(data))
  .catch(error =&amp;gt; console.error(error));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're making a POST request to '&lt;a href="https://example.com/api/endpoint"&gt;https://example.com/api/endpoint&lt;/a&gt;', passing a JSON object as the request body, and setting the 'Content-Type' header to 'application/json'. We're then parsing the response as JSON and logging the data to the console.&lt;/p&gt;

&lt;p&gt;A PATCH request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch('https://example.com/api/endpoint/123', {
  method: 'PATCH',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ name: 'John Doe' })
})
  .then(response =&amp;gt; response.json())
  .then(data =&amp;gt; console.log(data))
  .catch(error =&amp;gt; console.error(error));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're doing pretty much the same thing as POST however, note that the PATCH method is used to update an existing resource, and you need to specify the resource identifier in the URL, in this case, 123.&lt;/p&gt;

&lt;p&gt;A DELETE request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch('https://example.com/api/endpoint/123', {
  method: 'DELETE',
})
  .then(response =&amp;gt; response.json())
  .then(data =&amp;gt; console.log(data))
  .catch(error =&amp;gt; console.error(error));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're making a DELETE request to '&lt;a href="https://example.com/api/endpoint/123"&gt;https://example.com/api/endpoint/123&lt;/a&gt;', which will delete the resource identified by the 123 parameter in the URL. We're not passing any request body, as DELETE requests do not typically have a request body.&lt;/p&gt;

&lt;p&gt;As with the other HTTP methods, you can customize the DELETE request by passing additional options to the fetch() function, such as headers, body, and so on.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
