Создайте анимацию загрузки в React с помощью реагирующих спиннеров

Введение

При создании приложений React, которые извлекают контент из внешних источников, загрузка которых занимает некоторое время, всегда полезно обеспечить приятное взаимодействие с пользователем, привлекая пользователей и удерживая их внимание с помощью загрузчика, поскольку это помогает пользователям лучше понять, что происходит. чем оставить их спекулировать.

В этом руководстве мы узнаем, как отображать анимацию загрузчика при загрузке приложения и извлечении контента из внешних источников с помощью библиотеки react-spinners.

С этой целью мы создадим небольшое приложение, которое извлекает котировки, с экраном загрузки во время извлечения котировки:

пример загрузки-реакции-анимации

Если вы хотите узнать больше о создании счетчиков с нуля, прочитайте нашу статью «Как создать анимацию загрузки в React с нуля» !

Создание примера приложения React

Давайте начнем с рассмотрения нашей разметки React. По сути, у нас есть два <div>элемента в родительском <div> элементе (для простоты) — один , loader-containerа второй — main-content:

import React from 'react';

const App = () => {
  return (
    <div className="container">
      <div className="loader-container">
        <div className="spinner"></div>
      </div>

      <div className="main-content">
        <h1>Hello World!</h1>
        <p>
          This is a demo Project to show how to add animated loading with React.
        </p>
        <div className="buttons">
          <button className="btn">
            <a href="#">Read Article</a>
          </button>
          <button className="btn get-quote">
            Generate Quote
          </button>
        </div>
        <div className="quote-section">
          <blockquote className="quote">
            If you do not express your own original ideas, if you do not listen
            to your own being, you will have betrayed yourself.
          </blockquote>
          - <span className="author">Rollo May</span>
        </div>
      </div>
    </div>
  );
};

export default App;

Пока что мы создали только файл <div>для нашего загрузчика. Теперь давайте посмотрим, как мы можем добавить его и активировать при загрузке некоторого контента.

Примечание. Вы можете проверить этот репозиторий и при необходимости перепроверить код, читая это руководство.

Использование React Spinners в нашем приложении

react-spinnerпредставляет собой набор множества счетчиков, которые мы можем использовать в наших приложениях React. Чтобы использовать счетчик React, мы должны сначала установить библиотеку в каталог нашего проекта, выполнив любую из следующих команд:

$ npm install --save react-spinners
// Or
$ yarn add react-spinners

Как только это будет сделано, мы теперь можем импортировать конкретный загрузчик, который мы хотим использовать (в данном случае a ClipLoader), и установить некоторые стили, такие как размер и цвет:

import React, { useState, useEffect } from 'react';
import ClipLoader from 'react-spinners/ClipLoader';

const App = () => {
  const [loadingInProgress, setLoading] = useState(false);
  
  <!-- ... -->
  
  return (
    <div className="container">
      {loadingInProgress ? (
        <div className="loader-container">
          <ClipLoader color={'#fff'} size={150} />
        </div>
      ) : (
        <div className="main-content">
          <h1>Hello World!</h1>
          <p>
            This is a demo Project to show how to add animated loading with
            React.
          </p>
          <div className="buttons">
            <button className="btn">
              <a href="#">Read Article</a>
            </button>
            <button className="btn get-quote" onClick={getRandomQuote}>
              Generate Quote
            </button>
          </div>
          <div className="quote-section">
            <blockquote className="quote">{quote.content}</blockquote>-{' '}
            <span className="author">{quote.author}</span>
          </div>
        </div>
      )}
    </div>
  );
};

export default App;

В react-spinnerбиблиотеке есть много полезных функций, например, мы можем использовать ее для обработки загрузки без использования тернарных операторов:

<ClipLoader color={'#fff'} loading={loadingInProgress} size={150} />

Вместо использования тернарного оператора для отображения содержимого на основе значения переменной loadingInProgressмы просто использовали loading={loadingInProgress}вместо этого.

Мы также можем изменить CSS, используемый для управления, spinner-iconиспользуя overrideатрибут:

import { useState } from "react";
import { css } from "@emotion/react";
import ClipLoader from "react-spinners/ClipLoader";

const override = css`
  display: block;
  margin: 0 auto;
  border-color: red;
`;

function App() {
  let [loadingInProgress, setLoading] = useState(true);

  return (
    <div className="container">
      <ClipLoader color={'#fff'} loading={loadingInProgress} css={override} size={150} />

    // ...
    </div>
  );
}

export default App;

Примечание: мы можем прочитать больше react-spinnerв документации , где мы также можем увидеть список доступных грешников.

Заключение

В этом кратком руководстве мы рассмотрели, как можно react-spinnersдобавить счетчик загрузки к элементу в React.

Оригинальный источник статьи по адресу: https://stackabuse.com/

#react #loading #animation 

Создайте анимацию загрузки в React с помощью реагирующих спиннеров
津田  淳

津田 淳

1679733300

使用 react-spinners 在 React 中创建加载动画

介绍

当创建从需要一些时间加载的外部源获取内容的 React 应用程序时,通过吸引用户并将他们的注意力保持在加载器上来提供愉快的用户体验始终是一个好主意,因为这有助于用户了解正在发生的事情,而不是而不是让他们去猜测。

在本指南中,我们将学习如何利用库在加载应用程序和从外部源检索内容时显示加载器动画react-spinners

为此 - 我们将构建一个获取报价的小型应用程序,并在获取报价时显示加载屏幕:

反应加载动画示例

如果您想了解有关从头开始创建微调器的更多信息,请阅读我们的“如何从头开始在 React 中创建加载动画”

创建示例 React 应用程序

让我们先看看我们的 React 标记。基本上,我们在父级<div>中有两个元素(为了简单起见)——一个是 the ,第二个是: <div>loader-containermain-content

import React from 'react';

const App = () => {
  return (
    <div className="container">
      <div className="loader-container">
        <div className="spinner"></div>
      </div>

      <div className="main-content">
        <h1>Hello World!</h1>
        <p>
          This is a demo Project to show how to add animated loading with React.
        </p>
        <div className="buttons">
          <button className="btn">
            <a href="#">Read Article</a>
          </button>
          <button className="btn get-quote">
            Generate Quote
          </button>
        </div>
        <div className="quote-section">
          <blockquote className="quote">
            If you do not express your own original ideas, if you do not listen
            to your own being, you will have betrayed yourself.
          </blockquote>
          - <span className="author">Rollo May</span>
        </div>
      </div>
    </div>
  );
};

export default App;

到目前为止,我们只<div>为我们的装载机创建了一个。现在,让我们看看如何添加它并在加载某些内容时触发它。

注意:您可以查看此存储库并在阅读本指南时根据需要交叉检查代码。

在我们的应用程序中使用 React Spinner

react-spinner是我们可以在 React 应用程序中使用的许多微调器的集合。要使用 React 微调器,我们必须首先通过运行以下任一命令在项目目录中安装该库:

$ npm install --save react-spinners
// Or
$ yarn add react-spinners

完成后,我们现在可以导入我们想要使用的特定加载器(在本例中为 a ClipLoader)并设置一些样式,如大小和颜色:

import React, { useState, useEffect } from 'react';
import ClipLoader from 'react-spinners/ClipLoader';

const App = () => {
  const [loadingInProgress, setLoading] = useState(false);
  
  <!-- ... -->
  
  return (
    <div className="container">
      {loadingInProgress ? (
        <div className="loader-container">
          <ClipLoader color={'#fff'} size={150} />
        </div>
      ) : (
        <div className="main-content">
          <h1>Hello World!</h1>
          <p>
            This is a demo Project to show how to add animated loading with
            React.
          </p>
          <div className="buttons">
            <button className="btn">
              <a href="#">Read Article</a>
            </button>
            <button className="btn get-quote" onClick={getRandomQuote}>
              Generate Quote
            </button>
          </div>
          <div className="quote-section">
            <blockquote className="quote">{quote.content}</blockquote>-{' '}
            <span className="author">{quote.author}</span>
          </div>
        </div>
      )}
    </div>
  );
};

export default App;

react-spinner库有很多有用的特性,例如,我们可以使用它来处理加载而无需使用三元运算符:

<ClipLoader color={'#fff'} loading={loadingInProgress} size={150} />

我们没有使用三元运算符来显示基于变量值的内容loadingInProgress,而是简单地使用了loading={loadingInProgress}

spinner-icon我们还可以通过使用属性来更改用于控制的 CSS override

import { useState } from "react";
import { css } from "@emotion/react";
import ClipLoader from "react-spinners/ClipLoader";

const override = css`
  display: block;
  margin: 0 auto;
  border-color: red;
`;

function App() {
  let [loadingInProgress, setLoading] = useState(true);

  return (
    <div className="container">
      <ClipLoader color={'#fff'} loading={loadingInProgress} css={override} size={150} />

    // ...
    </div>
  );
}

export default App;

注意:react-spinner我们可以在文档中阅读更多相关信息,在那里我们也可以看到可用罪人的列表。

结论

在这个简短的指南中,我们了解了如何react-spinners在 React 中使用向元素添加加载微调器。

原始文章来源:https: //stackabuse.com/

#react #loading #animation 

使用 react-spinners 在 React 中创建加载动画

Create a Loading Animation in React with react-spinners

Introduction

When creating React applications that fetch content from external sources that take some time to load, it is always a good idea to provide a pleasant user experience by engaging users and keeping their attention with a loader, as this helps users understand what is going on rather than leaving them to speculate.

In this guide, we will learn how to display loader animation when loading an application and retrieving content from external sources, by utilizing the react-spinners library.

To that end - we'll build a small application that fetches quotes, with a loading screen while a quote is being fetched:

react-loading-animation-example

If you'd like to learn more about creating spinners from scratch, read our "How to Create a Loading Animation in React from Scratch"!

Creating a Sample React App

Let's begin by looking at our React markup. Basically, we have two <div> elements in the parent <div> (for the sake of simplicity) - one is the loader-container and the second is the main-content:

import React from 'react';

const App = () => {
  return (
    <div className="container">
      <div className="loader-container">
        <div className="spinner"></div>
      </div>

      <div className="main-content">
        <h1>Hello World!</h1>
        <p>
          This is a demo Project to show how to add animated loading with React.
        </p>
        <div className="buttons">
          <button className="btn">
            <a href="#">Read Article</a>
          </button>
          <button className="btn get-quote">
            Generate Quote
          </button>
        </div>
        <div className="quote-section">
          <blockquote className="quote">
            If you do not express your own original ideas, if you do not listen
            to your own being, you will have betrayed yourself.
          </blockquote>
          - <span className="author">Rollo May</span>
        </div>
      </div>
    </div>
  );
};

export default App;

So far, we've only created a <div> for our loader. Now, let's see how we can add it and trigger it when some content loads.

Note: You can check out this repository and cross-check the code if need be while reading this guide.

Using React Spinners in Our Application

react-spinner is a collection of many spinners that we can use in our React applications. To use React spinner, we must first install the library in our project's directory by running any of the following command:

$ npm install --save react-spinners
// Or
$ yarn add react-spinners

Once that is done, we can now import the particular loader we want to use (in this case, a ClipLoader) and set some styling like size and color:

import React, { useState, useEffect } from 'react';
import ClipLoader from 'react-spinners/ClipLoader';

const App = () => {
  const [loadingInProgress, setLoading] = useState(false);
  
  <!-- ... -->
  
  return (
    <div className="container">
      {loadingInProgress ? (
        <div className="loader-container">
          <ClipLoader color={'#fff'} size={150} />
        </div>
      ) : (
        <div className="main-content">
          <h1>Hello World!</h1>
          <p>
            This is a demo Project to show how to add animated loading with
            React.
          </p>
          <div className="buttons">
            <button className="btn">
              <a href="#">Read Article</a>
            </button>
            <button className="btn get-quote" onClick={getRandomQuote}>
              Generate Quote
            </button>
          </div>
          <div className="quote-section">
            <blockquote className="quote">{quote.content}</blockquote>-{' '}
            <span className="author">{quote.author}</span>
          </div>
        </div>
      )}
    </div>
  );
};

export default App;

The react-spinner library has a lot of useful features, for example, we can use it to handle loading without using ternary operators:

<ClipLoader color={'#fff'} loading={loadingInProgress} size={150} />

Instead of using ternary operator to display the content based on the value of the loadingInProgress variable, we've simply used loading={loadingInProgress} instead.

We can also alter the CSS used to control the spinner-icon by using an override attribute:

import { useState } from "react";
import { css } from "@emotion/react";
import ClipLoader from "react-spinners/ClipLoader";

const override = css`
  display: block;
  margin: 0 auto;
  border-color: red;
`;

function App() {
  let [loadingInProgress, setLoading] = useState(true);

  return (
    <div className="container">
      <ClipLoader color={'#fff'} loading={loadingInProgress} css={override} size={150} />

    // ...
    </div>
  );
}

export default App;

Note: We can read more about react-spinner in the documentation, where we can see a list of available sinners as well.

Conclusion

In this short guide, we've taken a look at how you can use react-spinners to add a loading spinner to an element in React.

Original article source at: https://stackabuse.com/

#react #loading #animation 

Create a Loading Animation in React with react-spinners
Gordon  Matlala

Gordon Matlala

1679389560

React-wait: Complex Loader Management Hook for React Applications

React-wait

Complex Loader Management Hook for React.

use-wait.gif 


react-wait is a React Hook helps to manage multiple loading states on the page without any conflict. It's based on a very simple idea that manages an Array of multiple loading states. The built-in loader component listens its registered loader and immediately become loading state.

Why not React.Suspense?:

React has its own Suspense feature to manage all the async works. For now it only supports code-splitting (not data-fetching).

useWait allows you to manage waiting experiences much more explicitly and not only for Promised/async patterns but also complete loading management.

Overview

Here's a quick overview that what's useWait for:

import { useWait, Waiter } from "react-wait";

function A() {
  const { isWaiting } = useWait();
  return (
    <div>
      {isWaiting("creating user") ? "Creating User..." : "Nothing happens"}
    </div>
  );
}

function B() {
  const { anyWaiting } = useWait();
  return (
    <div>
      {anyWaiting() ? "Something happening on app..." : "Nothing happens"}
    </div>
  );
}

function C() {
  const { startWaiting, endWaiting, isWaiting } = useWait();

  function createUser() {
    startWaiting("creating user");
    // Faking the async work:
    setTimeout(() => {
      endWaiting("creating user");
    }, 1000);
  }

  return (
    <button disabled={isWaiting("creating user")} onClick={createUser}>
      <Wait on="creating user" fallback={<Spinner />}>
        Create User
      </Wait>
    </button>
  );
}

ReactDOM.render(
  <Waiter>
    <C />
  </Waiter>,
  document.getElementById("root")
);

Quick Start

If you are a try and learn developer, you can start trying the react-wait now using codesandbox.io.

Quick start on CodeSandbox

1. Install:

yarn add react-wait

2. Require:

import { Waiter, useWait } from "react-wait";

function UserCreateButton() {
  const { startWaiting, endWaiting, isWaiting, Wait } = useWait();

  return (
    <button
      onClick={() => startWaiting("creating user")}
      disabled={isWaiting("creating user")}
    >
      <Wait on="creating user" fallback={<div>Creating user!</div>}>
        Create User
      </Wait>
    </button>
  );
}

3. Wrap with the Waiter Context Provider

And you should wrap your App with Waiter component. It's actually a Context.Provider that provides a loading context to the component tree.

const rootElement = document.getElementById("root");
ReactDOM.render(
  <Waiter>
    <App />
  </Waiter>,
  rootElement
);

Installation

$ yarn add react-wait
# or if you using npm
$ npm install react-wait

The API

react-wait provides some helpers to you to use in your templates.

anyWaiting()

Returns boolean value if any loader exists in context.

const { anyWaiting } = useWait();

return <button disabled={anyWaiting()}>Disabled while waiting</button>;

isWaiting(waiter String)

Returns boolean value if given loader exists in context.

const { isWaiting } = useWait();

return (
  <button disabled={isWaiting("creating user")}>
    Disabled while creating user
  </button>
);

startWaiting(waiter String)

Starts the given waiter.

const { startWaiting } = useWait();

return <button onClick={() => startWaiting("message")}>Start</button>;

endWaiting(waiter String)

Stops the given waiter.

const { end } = useWait();

return <button onClick={() => endWaiting("message")}>Stop</button>;

Using Wait Component

function Component() {
  const { Wait } = useWait();
  return (
    <Wait on="the waiting message" fallback={<div>Waiting...</div>}>
      The content after waiting done
    </Wait>
  );
}

Better example for a button with loading state:

<button disabled={isWaiting("creating user")}>
  <Wait on="creating user" fallback={<div>Creating User...</div>}>
    Create User
  </Wait>
</button>

Making Reusable Loader Components

With reusable loader components, you will be able to use custom loader components as example below. This will allow you to create better user loading experience.

function Spinner() {
  return <img src="spinner.gif" />;
}

Now you can use your spinner everywhere using waiting attribute:

<button disabled={isWaiting("creating user")}>
  <Wait on="creating user" fallback={<Spinner />}>
    Create User
  </Wait>
</button>

Creating Waiting Contexts using createWaitingContext(context String)

To keep your code DRY you can create a Waiting Context using createWaitingContext.

function CreateUserButton() {
  const { createWaitingContext } = useWait();

  // All methods will be curried with "creating user" on.
  const { startWaiting, endWaiting, isWaiting, Wait } = createWaitingContext(
    "creating user"
  );

  function createUser() {
    startWaiting();
    setTimeout(endWaiting, 1000);
  }

  return (
    <Button disabled={isWaiting()} onClick={createUser}>
      <Wait fallback="Creating User...">Create User</Wait>
    </Button>
  );
}

Read the Medium post "Managing Complex Waiting Experiences on Web UIs".


Contributors

  • Fatih Kadir Akın, (creator)

Other Implementations

Since react-wait based on a very simple idea, it can be implemented on other frameworks.

  • vue-wait: Multiple Process Loader Management for Vue.
  • dom-wait: Multiple Process Loader Management for vanilla JavaScript.

Download Details:

Author: F
Source Code: https://github.com/f/react-wait 
License: MIT license

#react #hooks #loading 

React-wait: Complex Loader Management Hook for React Applications

Create a Loading Animation in React with react-spinners

Create a Loading Animation in React with react-spinners

Introduction

When creating React applications that fetch content from external sources that take some time to load, it is always a good idea to provide a pleasant user experience by engaging users and keeping their attention with a loader, as this helps users understand what is going on rather than leaving them to speculate.

In this guide, we will learn how to display loader animation when loading an application and retrieving content from external sources, by utilizing the react-spinners library.

To that end - we'll build a small application that fetches quotes, with a loading screen while a quote is being fetched:

react-loading-animation-example

If you'd like to learn more about creating spinners from scratch, read our "How to Create a Loading Animation in React from Scratch"!

Creating a Sample React App

Let's begin by looking at our React markup. Basically, we have two <div> elements in the parent <div> (for the sake of simplicity) - one is the loader-container and the second is the main-content:

import React from 'react';

const App = () => {
  return (
    <div className="container">
      <div className="loader-container">
        <div className="spinner"></div>
      </div>

      <div className="main-content">
        <h1>Hello World!</h1>
        <p>
          This is a demo Project to show how to add animated loading with React.
        </p>
        <div className="buttons">
          <button className="btn">
            <a href="#">Read Article</a>
          </button>
          <button className="btn get-quote">
            Generate Quote
          </button>
        </div>
        <div className="quote-section">
          <blockquote className="quote">
            If you do not express your own original ideas, if you do not listen
            to your own being, you will have betrayed yourself.
          </blockquote>
          - <span className="author">Rollo May</span>
        </div>
      </div>
    </div>
  );
};

export default App;

So far, we've only created a <div> for our loader. Now, let's see how we can add it and trigger it when some content loads.

Note: You can check out this repository and cross-check the code if need be while reading this guide.

Using React Spinners in Our Application

react-spinner is a collection of many spinners that we can use in our React applications. To use React spinner, we must first install the library in our project's directory by running any of the following command:

$ npm install --save react-spinners
// Or
$ yarn add react-spinners

Once that is done, we can now import the particular loader we want to use (in this case, a ClipLoader) and set some styling like size and color:

import React, { useState, useEffect } from 'react';
import ClipLoader from 'react-spinners/ClipLoader';

const App = () => {
  const [loadingInProgress, setLoading] = useState(false);
  
  <!-- ... -->
  
  return (
    <div className="container">
      {loadingInProgress ? (
        <div className="loader-container">
          <ClipLoader color={'#fff'} size={150} />
        </div>
      ) : (
        <div className="main-content">
          <h1>Hello World!</h1>
          <p>
            This is a demo Project to show how to add animated loading with
            React.
          </p>
          <div className="buttons">
            <button className="btn">
              <a href="#">Read Article</a>
            </button>
            <button className="btn get-quote" onClick={getRandomQuote}>
              Generate Quote
            </button>
          </div>
          <div className="quote-section">
            <blockquote className="quote">{quote.content}</blockquote>-{' '}
            <span className="author">{quote.author}</span>
          </div>
        </div>
      )}
    </div>
  );
};

export default App;

The react-spinner library has a lot of useful features, for example, we can use it to handle loading without using ternary operators:

<ClipLoader color={'#fff'} loading={loadingInProgress} size={150} />

Instead of using ternary operator to display the content based on the value of the loadingInProgress variable, we've simply used loading={loadingInProgress} instead.

We can also alter the CSS used to control the spinner-icon by using an override attribute:

import { useState } from "react";
import { css } from "@emotion/react";
import ClipLoader from "react-spinners/ClipLoader";

const override = css`
  display: block;
  margin: 0 auto;
  border-color: red;
`;

function App() {
  let [loadingInProgress, setLoading] = useState(true);

  return (
    <div className="container">
      <ClipLoader color={'#fff'} loading={loadingInProgress} css={override} size={150} />

    // ...
    </div>
  );
}

export default App;

Note: We can read more about react-spinner in the documentation, where we can see a list of available sinners as well.

Conclusion

In this short guide, we've taken a look at how you can use react-spinners to add a loading spinner to an element in React.

Original article source at: https://stackabuse.com

#react #loading #animation 

Create a Loading Animation in React with react-spinners

Create A Loading animation in React with React-spinners

Create A Loading animation in React with React-spinners

Introduction

When creating React applications that fetch content from external sources that take some time to load, it is always a good idea to provide a pleasant user experience by engaging users and keeping their attention with a loader, as this helps users understand what is going on rather than leaving them to speculate.

In this guide, we will learn how to display loader animation when loading an application and retrieving content from external sources, by utilizing the react-spinners library.

To that end - we'll build a small application that fetches quotes, with a loading screen while a quote is being fetched:

react-loading-animation-example

If you'd like to learn more about creating spinners from scratch, read our "How to Create a Loading Animation in React from Scratch"!

Creating a Sample React App

Let's begin by looking at our React markup. Basically, we have two <div> elements in the parent <div> (for the sake of simplicity) - one is the loader-container and the second is the main-content:

import React from 'react';

const App = () => {
  return (
    <div className="container">
      <div className="loader-container">
        <div className="spinner"></div>
      </div>

      <div className="main-content">
        <h1>Hello World!</h1>
        <p>
          This is a demo Project to show how to add animated loading with React.
        </p>
        <div className="buttons">
          <button className="btn">
            <a href="#">Read Article</a>
          </button>
          <button className="btn get-quote">
            Generate Quote
          </button>
        </div>
        <div className="quote-section">
          <blockquote className="quote">
            If you do not express your own original ideas, if you do not listen
            to your own being, you will have betrayed yourself.
          </blockquote>
          - <span className="author">Rollo May</span>
        </div>
      </div>
    </div>
  );
};

export default App;

So far, we've only created a <div> for our loader. Now, let's see how we can add it and trigger it when some content loads.

Note: You can check out this repository and cross-check the code if need be while reading this guide.

Using React Spinners in Our Application

react-spinner is a collection of many spinners that we can use in our React applications. To use React spinner, we must first install the library in our project's directory by running any of the following command:

$ npm install --save react-spinners
// Or
$ yarn add react-spinners

Once that is done, we can now import the particular loader we want to use (in this case, a ClipLoader) and set some styling like size and color:

import React, { useState, useEffect } from 'react';
import ClipLoader from 'react-spinners/ClipLoader';

const App = () => {
  const [loadingInProgress, setLoading] = useState(false);
  
  <!-- ... -->
  
  return (
    <div className="container">
      {loadingInProgress ? (
        <div className="loader-container">
          <ClipLoader color={'#fff'} size={150} />
        </div>
      ) : (
        <div className="main-content">
          <h1>Hello World!</h1>
          <p>
            This is a demo Project to show how to add animated loading with
            React.
          </p>
          <div className="buttons">
            <button className="btn">
              <a href="#">Read Article</a>
            </button>
            <button className="btn get-quote" onClick={getRandomQuote}>
              Generate Quote
            </button>
          </div>
          <div className="quote-section">
            <blockquote className="quote">{quote.content}</blockquote>-{' '}
            <span className="author">{quote.author}</span>
          </div>
        </div>
      )}
    </div>
  );
};

export default App;

The react-spinner library has a lot of useful features, for example, we can use it to handle loading without using ternary operators:

<ClipLoader color={'#fff'} loading={loadingInProgress} size={150} />

Instead of using ternary operator to display the content based on the value of the loadingInProgress variable, we've simply used loading={loadingInProgress} instead.

We can also alter the CSS used to control the spinner-icon by using an override attribute:

import { useState } from "react";
import { css } from "@emotion/react";
import ClipLoader from "react-spinners/ClipLoader";

const override = css`
  display: block;
  margin: 0 auto;
  border-color: red;
`;

function App() {
  let [loadingInProgress, setLoading] = useState(true);

  return (
    <div className="container">
      <ClipLoader color={'#fff'} loading={loadingInProgress} css={override} size={150} />

    // ...
    </div>
  );
}

export default App;

Note: We can read more about react-spinner in the documentation, where we can see a list of available sinners as well.

Conclusion

In this short guide, we've taken a look at how you can use react-spinners to add a loading spinner to an element in React.

Original article source at: https://stackabuse.com/

#react #loading #animation 

Create A Loading animation in React with React-spinners
Gordon  Murray

Gordon Murray

1673466300

How to Loading and Indexing Data In MarkLogic

With MarkLogic being a document-oriented database, data is commonly stored in a JSON or XML document format.

If the data to bring into the MarkLogic is not already structured in JSON or XML means if it is currently in a relational database, there are various ways to export or transform it from the source.

For example, many relational databases provide an option to export relational data in XML or in JSON format, or a SQL script could be written to fetch the data from the database, outputting it in an XML or JSON structure. Or, using Marklogic rows from a .csv file can be imported as XML and JSON documents.

In any case, it is normal to first denormalize the data being exported from the relational database to first put the content back together in its original state. Denormalization, which naturally occurs when working with documents in their original form, greatly reduces the need for joins and acceleration performance.

Schema Agnostic

As we know that schema is something having a set of rules for a particular structure of the database. While we talk about data quality then schemas are helpful as quality matters a lot with quality reliability and a proper actional database is going to present.

Now if we talk about the schema-agnostic then it is something the database is not bounded by any schema but it is aware of it. Schemas are optional in MarkLogic. Data is going to be loaded in its original data form. To address a group of documents within a database, directories, collections and internal structure of documents can be used. With MarkLogic easily supports data from disparate systems all in the same database.

Required Document Size and Structure

When loading a document, it is the best choice to have one document per entity. Marklogic is the most performant with many small documents, rather than one large document. The target document size is 1KB to 100KB but can be larger.

For Example, rather than loading a bunch of students all as one document, have each student be a document.

Whenever defining a document remember that use XML document and attribute names or JSON property names. Make document names human-readable so do not create generic names. Using this convention help indexes be efficient.

<items>

<item>

<product> Mouse </product>

<price> 1000 </price>

<quantity> 3 </quantity>

</item>

<item>

<product> Keyboard </product>

<price> 2000 </price>

<quantity> 2 </quantity>

</item>

</items>

Indexing Documents

As documents are loaded, all the words in each document and the structure of each document, are indexed. So documents are easily searchable.

The document can be loaded into the MarkLogic in many ways:

  • MarkLogic Content Pump.
  • Data movement SDK.
  • Rest APIs
  • Java API or Node js API.
  • XQuery
  • Javascript Functions.

Reading a Document

To read a document, the URI of the document is used.

XQuery Example : fn:doc("college/course-101.json")

JavaScript Example : fn:doc("account/order-202.json")

Rest API Example : curl --abc --user admin:admin: -X GET "http://localhost:8055/v1/document?uri=/accounting/order-10072.json"

Splitting feature of MLCP

MLCP has the feature of splitting the long XML documents, where each occurrence of a designated element becomes an individual XML document in the database. This is useful when multiple records are all contained within one large XML file. Such as a list of students, courses, details, etc.

The -input_file_type aggregates option is used to split a large document into individual documents. The aggregate_record-element option is used element used to designate a new document. The -uri_id is used to create a URI for each document.

While it is fine to have a mix of XML and JSON documents in the same database, it is also possible to transform content from one format to other. You can easily transform the files by following the below steps.

xquery version "1.0-ml";

import module namespace json = "http://abc.com/xdmp/json" at "abc/json/json.xqy";

json:transform-to-json(fn:doc("doc-01.xml"), json:config("custom"))

A Marklogic content pump can be used to import the rows from the .csv file to a MarkLogic database. We are able to the data during the process or afterward in the database. Ways to modify content once it is already in the database include using the data movement SDK, XQuery, Js, etc.

Conclusion

As we know that MarkLogic is a database that facilitates many things like we can load the data, indexing the data, transforming the data, and splitting the data.

References:

Original article source at: https://blog.knoldus.com/

#loading #data #index 

How to Loading and Indexing Data In MarkLogic
Rupert  Beatty

Rupert Beatty

1673345400

Windless Makes It Easy to Implement invisible Layout Loading View

Windless

Windless makes it easy to implement invisible layout loading view.

basic.giftable.gifcollection.gif

Requirements

  • iOS 8.0+
  • Xcode 9.0+
  • Swift 4.0+

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 1.1+ is required to build Windless 4.0+.

To integrate Windless into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'Windless', '~> 0.1.5'
end

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Windless into your Xcode project using Carthage, specify it in your Cartfile:

github "Interactive-Studio/Windless" ~> 0.1.5

Run carthage update to build the framework and drag the built Windless.framework into your Xcode project.

Manually

If you prefer not to use either of the aforementioned dependency managers, you can integrate Windless into your project manually.


Usage

Code

import Windless

class ViewController: UIViewController {

    lazy var contentsView = UIView()
    
    var subView1 = UIView()
    var subView2 = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addSubview(contentsView)
        contentsView.addSubview(subView1)
        contentsView.addSubview(subView2)
        
        // start
        contentsView.windless
                .setupWindlessableViews([subView1, subView2])
                .start()
                
        // stop
        contentsView.windless.end()
    }

}

Storyboard, Xib

If you use Storyboard or xib, you only need to set the isWindlessable flag to true for the views you want to show as fake in the view inspector of the view, and you do not have to pass the view through the setupWindlessableViews method.

import Windless

class ViewController: UIViewController {

    @IBOutlet weak var contentsView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        contentsView.windless.start()
    }
}

Multiline

Depending on the lineHeight value and the spacing value, UILabel and UITextView will reconstruct the layout when the windless animation runs.

public protocol CanBeMultipleLines {

    var lineHeight: CGFloat { get set }

    var spacing: CGFloat { get set }
}
ConfigurationResult

Custom Options

There are several customizable options in Windless.

public class WindlessConfiguration {
    
    /// The direction of windless animation. Defaults to rightDiagonal.
    public var direction: WindlessDirection = .rightDiagonal
    
    /// The speed of windless animation. Defaults to 1.
    public var speed: Float = 1
    
    /// The duration of the fade used when windless begins. Defaults to 0.
    public var beginTime: CFTimeInterval = 0
    
    /// The time interval windless in seconds. Defaults to 4.
    public var duration: CFTimeInterval = 4
    
    /// The time interval between windless in seconds. Defaults to 2.
    public var pauseDuration: CFTimeInterval = 2
    
    /// gradient animation timingFunction default easeOut
    public var timingFuction: CAMediaTimingFunction = .easeOut
    
    /// gradient layer center color default .lightGray
    public var animationLayerColor: UIColor = .lightGray
    
    /// Mask layer background color default .groupTableViewBackground
    public var animationBackgroundColor: UIColor = .groupTableViewBackground
    
    /// The opacity of the content while it is windless. Defaults to 0.8.
    public var animationLayerOpacity: CGFloat = 0.8
}

To set the options, use the apply method as shown below.

import Windless

class ViewController: UIViewController {

    @IBOutlet weak var contentsView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        contentsView.windless
            .apply {
                $0.beginTime = 1
                $0.pauseDuration = 2
                $0.duration = 3
                $0.animationLayerOpacity = 0.5
            }
            .start()
    }
}

If you want to know more detailed usage, please refer to Example.

Looks

The isWindlessable value determines how the loading view looks. The images below show how the loading screen will look according to the isWindlessable value.

isWindlessable= 🌀

ConfigurationResult

Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Credits

Download Details:

Author: ParkGwangBeom
Source Code: https://github.com/ParkGwangBeom/Windless 
License: MIT license

#swift #ios #skeleton #animation #loading 

Windless Makes It Easy to Implement invisible Layout Loading View
Kevon  Krajcik

Kevon Krajcik

1672799439

How to Create A Logistic Regression Classifier in PyTorch

In this PyTorch tutorial we will learn about How to Create A Logistic Regression Classifier in PyTorch. Logistic regression is a type of regression that predicts the probability of an event. It is used for classification problems and has many applications in the fields of machine learning, artificial intelligence, and data mining.

The formula of logistic regression is to apply a sigmoid function to the output of a linear function. This article discusses how you can build a logistic regression classifier. While previously you have been working on a single-varable dataset, here we’ll be using a popular MNIST dataset to train and test our model. After going through this article, you’ll learn:

  • How to use logistic regression in PyTorch and how it can be applied to real-world problems.
  • How to load and analyze torchvision datasets.
  • How to build and train a logistic regression classifier on image datasets.

Let’s get started.

Overview

This tutorial is in four parts; they are

  • The MNIST Dataset
  • Load Dataset into DataLoader
  • Build the Model with nn.Module
  • Training the Classifier

The MNIST Dataset

You will train and test a logistic regression model with MNIST dataset. This dataset contains 6000 images for training and 10000 images for testing the out-of-sample performance.

The MNIST dataset is so popular that it is part of PyTorch. Here is how you can load the training and testing samples of the MNIST dataset in PyTorch.

import torch
import torchvision.transforms as transforms
from torchvision import datasets
 
# loading training data
train_dataset = datasets.MNIST(root='./data', 
                               train=True, 
                               transform=transforms.ToTensor(),
                               download=True)
#loading test data
test_dataset = datasets.MNIST(root='./data', 
                              train=False, 
                              transform=transforms.ToTensor())

The dataset will be downloaded and extracted to the directory as below.

Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to ./data/MNIST/raw/train-images-idx3-ubyte.gz
  0%|          | 0/9912422 [00:00<?, ?it/s]
Extracting ./data/MNIST/raw/train-images-idx3-ubyte.gz to ./data/MNIST/raw
 
Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz to ./data/MNIST/raw/train-labels-idx1-ubyte.gz
  0%|          | 0/28881 [00:00<?, ?it/s]
Extracting ./data/MNIST/raw/train-labels-idx1-ubyte.gz to ./data/MNIST/raw
 
Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz to ./data/MNIST/raw/t10k-images-idx3-ubyte.gz
  0%|          | 0/1648877 [00:00<?, ?it/s]
Extracting ./data/MNIST/raw/t10k-images-idx3-ubyte.gz to ./data/MNIST/raw
 
Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz to ./data/MNIST/raw/t10k-labels-idx1-ubyte.gz
  0%|          | 0/4542 [00:00<?, ?it/s]
Extracting ./data/MNIST/raw/t10k-labels-idx1-ubyte.gz to ./data/MNIST/raw

Let’s verify number of training and testing samples in the dataset.


print("number of training samples: " + str(len(train_dataset)) + "\n" +
      "number of testing samples: " + str(len(test_dataset)))

It prints


number of training samples: 60000
number of testing samples: 10000

Each sample in the dataset is a pair of image and label. To inspect the data type and size of the first element in the training data, you can use type() and size() methods.

print("datatype of the 1st training sample: ", train_dataset[0][0].type())
print("size of the 1st training sample: ", train_dataset[0][0].size())

This prints

datatype of the 1st training sample:  torch.FloatTensor
size of the 1st training sample:  torch.Size([1, 28, 28])

You can access samples from a dataset using list indexing. The first sample in the dataset is a FloatTensor and it is a 28×28-pixel image in grayscale (i.e., one channel), hence the size [1, 28, 28].

Now, let’s check the labels of the first two samples in the training set.

# check the label of first two training sample
print("label of the first taining sample: ", train_dataset[0][1])
print("label of the second taining sample: ", train_dataset[1][1])

This shows

label of the first taining sample:  5
label of the second taining sample:  0

From the above, you can see that the first two images in the training set represent “5” and “0”. Let’s show these two images to confirm.

img_5 = train_dataset[0][0].numpy().reshape(28, 28)
plt.imshow(img_5, cmap='gray')
plt.show()
img_0 = train_dataset[1][0].numpy().reshape(28, 28)
plt.imshow(img_0, cmap='gray')
plt.show()

You should see these two digits:

Load Dataset into DataLoader

Usually, you do not use the dataset directly in training but through a DataLoader class. This allows you to read data in batches, not samples.

In the following, data is loaded into a DataLoader with batch size at 32.

...
from torch.utils.data import DataLoader
 
# load train and test data samples into dataloader
batach_size = 32
train_loader = DataLoader(dataset=train_dataset, batch_size=batach_size, shuffle=True) 
test_loader = DataLoader(dataset=test_dataset, batch_size=batach_size, shuffle=False)

Build the Model with nn.Module

Let’s build the model class with nn.Module for our logistic regression model. This class is similar to that in the previous posts but the numbers of input and output are configurable.

# build custom module for logistic regression
class LogisticRegression(torch.nn.Module):    
    # build the constructor
    def __init__(self, n_inputs, n_outputs):
        super(LogisticRegression, self).__init__()
        self.linear = torch.nn.Linear(n_inputs, n_outputs)
    # make predictions
    def forward(self, x):
        y_pred = torch.sigmoid(self.linear(x))
        return y_pred

This model will take a 28×28-pixel image of handwritten digits as input and classify them into one of the 10 output classes of digits 0 to 9. So, here is how you can instantiate the model.

# instantiate the model
n_inputs = 28*28 # makes a 1D vector of 784
n_outputs = 10
log_regr = LogisticRegression(n_inputs, n_outputs)

Training the Classifier

You will train this model with stochastic gradient descent as the optimizer with learning rate 0.001 and cross-entropy as the loss metric.

Then, the model is trained for 50 epochs. Note that you have use view() method to flatten the image matrices into rows to fit the same of the logistic regression model input.

...
 
# defining the optimizer
optimizer = torch.optim.SGD(log_regr.parameters(), lr=0.001)
# defining Cross-Entropy loss
criterion = torch.nn.CrossEntropyLoss()
 
epochs = 50
Loss = []
acc = []
for epoch in range(epochs):
    for i, (images, labels) in enumerate(train_loader):
        optimizer.zero_grad()
        outputs = log_regr(images.view(-1, 28*28))
        loss = criterion(outputs, labels)
        # Loss.append(loss.item())
        loss.backward()
        optimizer.step()
    Loss.append(loss.item())
    correct = 0
    for images, labels in test_loader:
        outputs = log_regr(images.view(-1, 28*28))
        _, predicted = torch.max(outputs.data, 1)
        correct += (predicted == labels).sum()
    accuracy = 100 * (correct.item()) / len(test_dataset)
    acc.append(accuracy)
    print('Epoch: {}. Loss: {}. Accuracy: {}'.format(epoch, loss.item(), accuracy))

During training, you should see the progress like the following:

Epoch: 0. Loss: 2.211054563522339. Accuracy: 61.63
Epoch: 1. Loss: 2.1178536415100098. Accuracy: 74.81
Epoch: 2. Loss: 2.0735440254211426. Accuracy: 78.47
Epoch: 3. Loss: 2.040225028991699. Accuracy: 80.17
Epoch: 4. Loss: 1.9637292623519897. Accuracy: 81.05
Epoch: 5. Loss: 2.000900983810425. Accuracy: 81.44
...
Epoch: 45. Loss: 1.6549798250198364. Accuracy: 86.3
Epoch: 46. Loss: 1.7053509950637817. Accuracy: 86.31
Epoch: 47. Loss: 1.7396119832992554. Accuracy: 86.36
Epoch: 48. Loss: 1.6963073015213013. Accuracy: 86.37
Epoch: 49. Loss: 1.6838685274124146. Accuracy: 86.46

You have achieved an accuracy of around 86% by training the model for only 50 epochs. Accuracy can be improved further if the model is trained longer.

Let’s visualize how the graphs for loss and accuracy look like. The following is the loss:

plt.plot(Loss)
plt.xlabel("no. of epochs")
plt.ylabel("total loss")
plt.title("Loss")
plt.show()

And this is for accuracy:

plt.plot(acc)
plt.xlabel("no. of epochs")
plt.ylabel("total accuracy")
plt.title("Accuracy")
plt.show()

Putting everything together, the following is the complete code:

import torch
import torchvision.transforms as transforms
from torchvision import datasets
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
 
# loading training data
train_dataset = datasets.MNIST(root='./data', 
                               train=True, 
                               transform=transforms.ToTensor(),
                               download=True)
# loading test data
test_dataset = datasets.MNIST(root='./data', 
                              train=False, 
                              transform=transforms.ToTensor())
 
print("number of training samples: " + str(len(train_dataset)) + "\n" +
      "number of testing samples: " + str(len(test_dataset)))
print("datatype of the 1st training sample: ", train_dataset[0][0].type())
print("size of the 1st training sample: ", train_dataset[0][0].size())
 
# check the label of first two training sample
print("label of the first taining sample: ", train_dataset[0][1])
print("label of the second taining sample: ", train_dataset[1][1])
 
img_5 = train_dataset[0][0].numpy().reshape(28, 28)
plt.imshow(img_5, cmap='gray')
plt.show()
img_0 = train_dataset[1][0].numpy().reshape(28, 28)
plt.imshow(img_0, cmap='gray')
plt.show()
 
# load train and test data samples into dataloader
batach_size = 32
train_loader = DataLoader(dataset=train_dataset, batch_size=batach_size, shuffle=True) 
test_loader = DataLoader(dataset=test_dataset, batch_size=batach_size, shuffle=False)
 
# build custom module for logistic regression
class LogisticRegression(torch.nn.Module):    
    # build the constructor
    def __init__(self, n_inputs, n_outputs):
        super().__init__()
        self.linear = torch.nn.Linear(n_inputs, n_outputs)
    # make predictions
    def forward(self, x):
        y_pred = torch.sigmoid(self.linear(x))
        return y_pred
 
# instantiate the model
n_inputs = 28*28 # makes a 1D vector of 784
n_outputs = 10
log_regr = LogisticRegression(n_inputs, n_outputs)
 
# defining the optimizer
optimizer = torch.optim.SGD(log_regr.parameters(), lr=0.001)
# defining Cross-Entropy loss
criterion = torch.nn.CrossEntropyLoss()
 
epochs = 50
Loss = []
acc = []
for epoch in range(epochs):
    for i, (images, labels) in enumerate(train_loader):
        optimizer.zero_grad()
        outputs = log_regr(images.view(-1, 28*28))
        loss = criterion(outputs, labels)
        # Loss.append(loss.item())
        loss.backward()
        optimizer.step()
    Loss.append(loss.item())
    correct = 0
    for images, labels in test_loader:
        outputs = log_regr(images.view(-1, 28*28))
        _, predicted = torch.max(outputs.data, 1)
        correct += (predicted == labels).sum()
    accuracy = 100 * (correct.item()) / len(test_dataset)
    acc.append(accuracy)
    print('Epoch: {}. Loss: {}. Accuracy: {}'.format(epoch, loss.item(), accuracy))
 
plt.plot(Loss)
plt.xlabel("no. of epochs")
plt.ylabel("total loss")
plt.title("Loss")
plt.show()
 
plt.plot(acc)
plt.xlabel("no. of epochs")
plt.ylabel("total accuracy")
plt.title("Accuracy")
plt.show()

Summary

In this tutorial, you learned how to build a multi-class logistic regression classifier in PyTorch. Particularly, you learned.

  • How to use logistic regression in PyTorch and how it can be applied to real-world problems.
  • How to load and analyze torchvision datasets.
  • How to build and train a logistic regression classifier on image datasets.

Original article sourced at: https://machinelearningmastery.com

#pytorch 

How to Create A Logistic Regression Classifier in PyTorch
Rupert  Beatty

Rupert Beatty

1666950180

Placeholder Views Based on Content, Loading, Error Or Empty States

StatefulViewController

A protocol to enable UIViewControllers or UIViews to present placeholder views based on content, loading, error or empty states.

StatefulViewController Example

Overview

In a networked application a view controller or custom view typically has the following states that need to be communicated to the user:

  • Loading: The content is currently loaded over the network.
  • Content: The content is available and presented to the user.
  • Empty: There is currently no content available to display.
  • Error: An error occurred whilst downloading content.

As trivial as this flow may sound, there are a lot of cases that result in a rather large decision tree.

Decision Tree

StatefulViewController is a concrete implementation of this particular decision tree. (If you want to create your own modified version, you might be interested in the state machine that is used to show and hide views.)

Version Compatibility

Current Swift compatibility breakdown:

Swift VersionFramework Version
3.03.x
2.32.x
2.21.x

Usage

This guide describes the use of the StatefulViewController protocol on UIViewController. However, you can also adopt the StatefulViewController protocol on any UIViewController subclass, such as UITableViewController or UICollectionViewController, as well as your custom UIView subclasses.

First, make sure your view controller adopts to the StatefulViewController protocol.

class MyViewController: UIViewController, StatefulViewController {
    // ...
}

Then, configure the loadingView, emptyView and errorView properties (provided by the StatefulViewController protocol) in viewDidLoad.

override func viewDidLoad() {
    super.viewDidLoad()

    // Setup placeholder views
    loadingView = // UIView
    emptyView = // UIView
    errorView = // UIView
}

In addition, call the setupInitialViewState() method in viewWillAppear: in order to setup the initial state of the controller.

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    setupInitialViewState()
}

After that, simply tell the view controller whenever content is loading and StatefulViewController will take care of showing and hiding the correct loading, error and empty view for you.

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    loadDeliciousWines()
}

func loadDeliciousWines() {
    startLoading()

    let url = NSURL(string: "http://example.com/api")
    let session = NSURLSession.sharedSession()
    session.dataTaskWithURL(url) { (let data, let response, let error) in
        endLoading(error: error)
    }.resume()
}

Life cycle

StatefulViewController calls the hasContent method to check if there is any content to display. If you do not override this method in your own class, StatefulViewController will always assume that there is content to display.

func hasContent() -> Bool {
    return datasourceArray.count > 0
}

Optionally, you might also be interested to respond to an error even if content is already shown. StatefulViewController will not show its errorView in this case, because there is already content that can be shown.

To e.g. show a custom alert or other unobtrusive error message, use handleErrorWhenContentAvailable: to manually present the error to the user.

func handleErrorWhenContentAvailable(error: ErrorType) {
    let alertController = UIAlertController(title: "Ooops", message: "Something went wrong.", preferredStyle: .Alert)
    alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
    presentViewController(alertController, animated: true, completion: nil)
}

Custom Placeholder View insets

Per default, StatefulViewController presents all configured placeholder views fullscreen (i.e. with 0 insets from top, bottom, left & right from the superview). In case a placeholder view should have custom insets the configured placeholderview may conform to the StatefulPlaceholderView protocol and override the placeholderViewInsets method to return custom edge insets.

class MyPlaceholderView: UIView, StatefulPlaceholderView {
    func placeholderViewInsets() -> UIEdgeInsets {
        return UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
    }
}

 

View State Machine

Note: The following section is only intended for those, who want to create a stateful controller that differs from the flow described above.

You can also use the underlying view state machine to create a similar implementation for your custom flow of showing/hiding views.

let stateMachine = ViewStateMachine(view: view)

// Add states
stateMachine["loading"] = loadingView
stateMachine["other"] = otherView

// Transition to state
stateMachine.transitionToState(.View("loading"), animated: true) {
    println("finished switching to loading view")
}

// Hide all views
stateMachine.transitionToState(.None, animated: true) {
    println("all views hidden now")
}

Installation

Carthage

Add the following line to your Cartfile.

github "aschuch/StatefulViewController" ~> 3.0

Then run carthage update.

CocoaPods

Add the following line to your Podfile.

pod "StatefulViewController", "~> 3.0"

Then run pod install with CocoaPods 0.36 or newer.

Manually

Just drag and drop the two .swift files in the StatefulViewController folder into your project.

Tests

Open the Xcode project and press ⌘-U to run the tests.

Alternatively, all tests can be run from the terminal using xctool.

xctool -scheme StatefulViewControllerTests -sdk iphonesimulator test

Todo

  • Default loading, error, empty views
  • Protocol on views that notifies them of removal and add
  • Views can provide delays in order to tell the state machine to show/remove them only after a specific delay (e.g. for hide and show animations)

Contributing

  • Create something awesome, make the code better, add some functionality, whatever (this is the hardest part).
  • Fork it
  • Create new branch to make your changes
  • Commit all your changes to your branch
  • Submit a pull request

Contact

Feel free to get in touch.

Download Details:

Author: Aschuch
Source Code: https://github.com/aschuch/StatefulViewController  
License: MIT license

#swift #state #management #loading #error #empty 

Placeholder Views Based on Content, Loading, Error Or Empty States
Rupert  Beatty

Rupert Beatty

1666797900

HGPlaceholders: Nice Library to Show Placeholders and Empty States

HGPlaceholders

Example

default.gif custom.gif

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 8.0+
  • Xcode 9.2

You also may like

  • HGCircularSlider - A custom reusable circular slider control for iOS application.
  • HGRippleRadarView - A beautiful radar view to show nearby users with ripple animation, fully customizable

Installation

HGPlaceholders is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'HGPlaceholders'

HGPlaceholders is also available through Carthage. To install it, simply add the following line to your Cartfile:

github "HamzaGhazouani/HGPlaceholders"

Usage

  1. Inherit your UITableView class from TableView Or inherit UICollectionView from CollectionView
  2. Call the placeholder to show
  • tableView.showLoadingPlaceholder() or collectionView.showLoadingPlaceholder()
  • tableView.showNoResultsPlaceholder() or collectionView.showNoResultsPlaceholder()
  • tableView.showErrorPlaceholder() or collectionView.showErrorPlaceholder()
  • tableView.showNoConnectionPlaceholder() or collectionView.showNoConnectionPlaceholder()

Customization

If you want to change only images, just set them in your asset with this names (the framework check firstly in the main bundle):

  • loading : "hg_default-loading"
  • no_connection : "hg_default-no_connection"
  • no_results : "hg_default-no_results"
  • error : "hg_default-error"

The framework contains different defaults placeholders:

  • Basic :

tableView.placeholdersProvider = .basic or collectionView.placeholdersProvider = .basic

  • Default :

tableView.placeholdersProvider = .default or collectionView.placeholdersProvider = .default

  • Default2 :

tableView.placeholdersProvider = .default2 or collectionView.placeholdersProvider = .default2

  • Hallowen :

tableView.placeholdersProvider = .halloween or collectionView.placeholdersProvider = .halloween // for fun :)`

If you want to change the default palceholders for all table views in your project:

class ProjectNameTableView: TableView {

    override func customSetup() {
        placeholdersProvider = .basic
    }
}
class ProjectNameCollectionView: CollectionView {

    override func customSetup() {
        placeholdersProvider = .basic
    }
}

You can also add new placeholders fully customizable, you should keep in mind that the view will take table view frame, and placeholder can have only one action, please check the example project

Creating a new theme from scratch

static var summer: PlaceholdersProvider {
        
        var commonStyle = PlaceholderStyle()
        commonStyle.backgroundColor = UIColor(red: 1.0, green: 236.0/255, blue: 209.0/255.0, alpha: 1.0)
        commonStyle.actionBackgroundColor = .black
        commonStyle.actionTitleColor = .white
        commonStyle.titleColor = .black
        commonStyle.isAnimated = false
        
        commonStyle.titleFont = UIFont(name: "AvenirNextCondensed-HeavyItalic", size: 19)!
        commonStyle.subtitleFont = UIFont(name: "AvenirNextCondensed-Italic", size: 19)!
        commonStyle.actionTitleFont = UIFont(name: "AvenirNextCondensed-Heavy", size: 19)!
        
        var loadingStyle = commonStyle
        loadingStyle.actionBackgroundColor = .clear
        loadingStyle.actionTitleColor = .gray
        
        var loadingData: PlaceholderData = .loading
        loadingData.image = #imageLiteral(resourceName: "summer-hat")
        let loading = Placeholder(data: loadingData, style: loadingStyle, key: .loadingKey)
        
        var errorData: PlaceholderData = .error
        errorData.image = #imageLiteral(resourceName: "summer-ball")
        let error = Placeholder(data: errorData, style: commonStyle, key: .errorKey)
        
        var noResultsData: PlaceholderData = .noResults
        noResultsData.image = #imageLiteral(resourceName: "summer-cocktail")
        let noResults = Placeholder(data: noResultsData, style: commonStyle, key: .noResultsKey)
        
        var noConnectionData: PlaceholderData = .noConnection
        noConnectionData.image = #imageLiteral(resourceName: "summer-beach-slippers")
        let noConnection = Placeholder(data: noConnectionData, style: commonStyle, key: .noConnectionKey)
        
        let placeholdersProvider = PlaceholdersProvider(loading: loading, error: error, noResults: noResults, noConnection: noConnection)
        
        let xibPlaceholder = Placeholder(cellIdentifier: "CustomPlaceholderCell", key: PlaceholderKey.custom(key: "XIB"))
        
        placeholdersProvider.add(placeholders: xibPlaceholder)
        
        return placeholdersProvider
    }

Adding a custom placeholder to an existing theme

   private static var starWarsPlaceholder: Placeholder {
       var starwarsStyle = PlaceholderStyle()
       starwarsStyle.backgroundColor = .black
       starwarsStyle.actionBackgroundColor = .clear
       starwarsStyle.actionTitleColor = .white
       starwarsStyle.titleColor = .white
       starwarsStyle.isAnimated = false
       
       var starwarsData = PlaceholderData()
       starwarsData.title = NSLocalizedString("\"This is a new day, a\nnew beginning\"", comment: "")
       starwarsData.subtitle = NSLocalizedString("Star Wars", comment: "")
       starwarsData.image = #imageLiteral(resourceName: "star_wars")
       starwarsData.action = NSLocalizedString("OK!", comment: "")
       
       let placeholder = Placeholder(data: starwarsData, style: starwarsStyle, key: PlaceholderKey.custom(key: "starWars"))
       
       return placeholder
   }
   
   let provider = PlaceholdersProvider.summer 
   provider.addPlaceholders(MyUtilityClass.starWarsPlaceholder) 

Documentation

Full documentation is available on CocoaDocs.
You can also install documentation locally using jazzy.

Download Details:

Author: HamzaGhazouani
Source Code: https://github.com/HamzaGhazouani/HGPlaceholders 
License: MIT license

#swift #ios #loading 

HGPlaceholders: Nice Library to Show Placeholders and Empty States
Rupert  Beatty

Rupert Beatty

1665798300

SkeletonView: An Elegant Way to Show Users That Something is Happening

SkeletonView

Today almost all apps have async processes, such as API requests, long running processes, etc. While the processes are working, usually developers place a loading view to show users that something is going on.

SkeletonView has been conceived to address this need, an elegant way to show users that something is happening and also prepare them for which contents are waiting.

Enjoy it! 🙂

🌟 Features

  • Easy to use
  • All UIViews are skeletonables
  • Fully customizable
  • Universal (iPhone & iPad)
  • Interface Builder friendly
  • Simple Swift syntax
  • Lightweight readable codebase

🎬 Guides

SkeletonView Guides - Getting startedHow to Create Loading View with Skeleton View in Swift 5.2 by iKh4ever StudioCreate Skeleton Loading View in App (Swift 5) - Xcode 11, 2020 by iOS AcademyCómo crear una ANIMACIÓN de CARGA de DATOS en iOS by MoureDev

📲 Installation

pod 'SkeletonView'
github "Juanpe/SkeletonView"
dependencies: [
  .package(url: "https://github.com/Juanpe/SkeletonView.git", from: "1.7.0")
]

📣 IMPORTANT!

Since version 1.30.0, SkeletonView supports XCFrameworks, so if you want to install it as a XCFramework, please use this repo instead.

🐒 Usage

Only 3 steps needed to use SkeletonView:

1️⃣ Import SkeletonView in proper place.

import SkeletonView

2️⃣ Now, set which views will be skeletonables. You achieve this in two ways:

Using code:

avatarImageView.isSkeletonable = true

Using IB/Storyboards:

3️⃣ Once you've set the views, you can show the skeleton. To do so, you have 4 choices:

(1) view.showSkeleton()                 // Solid
(2) view.showGradientSkeleton()         // Gradient
(3) view.showAnimatedSkeleton()         // Solid animated
(4) view.showAnimatedGradientSkeleton() // Gradient animated

Preview

SolidGradientSolid AnimatedGradient Animated
solid_animated.gifgradient_animated.gif

📣 IMPORTANT!

SkeletonView is recursive, so if you want show the skeleton in all skeletonable views, you only need to call the show method in the main container view. For example, with UIViewControllers.

🌿 Collections

SkeletonView is compatible with UITableView and UICollectionView.

UITableView

If you want to show the skeleton in a UITableView, you need to conform to SkeletonTableViewDataSource protocol.

public protocol SkeletonTableViewDataSource: UITableViewDataSource {
    func numSections(in collectionSkeletonView: UITableView) -> Int // Default: 1
    func collectionSkeletonView(_ skeletonView: UITableView, numberOfRowsInSection section: Int) -> Int
    func collectionSkeletonView(_ skeletonView: UITableView, cellIdentifierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier
    func collectionSkeletonView(_ skeletonView: UITableView, skeletonCellForRowAt indexPath: IndexPath) -> UITableViewCell? // Default: nil
    func collectionSkeletonView(_ skeletonView: UITableView, prepareCellForSkeleton cell: UITableViewCell, at indexPath: IndexPath)
}

As you can see, this protocol inherits from UITableViewDataSource, so you can replace this protocol with the skeleton protocol.

This protocol has a default implementation for some methods. For example, the number of rows for each section is calculated in runtime:

func collectionSkeletonView(_ skeletonView: UITableView, numberOfRowsInSection section: Int) -> Int
// Default:
// It calculates how many cells need to populate whole tableview

📣 IMPORTANT!

If you return UITableView.automaticNumberOfSkeletonRows in the above method, it acts like the default behavior (i.e. it calculates how many cells needed to populate the whole tableview).

There is only one method you need to implement to let Skeleton know the cell identifier. This method doesn't have default implementation:

func collectionSkeletonView(_ skeletonView: UITableView, cellIdentifierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier {
   return "CellIdentifier"
}

By default, the library dequeues the cells from each indexPath, but you can also do this if you want to make some changes before the skeleton appears:

func collectionSkeletonView(_ skeletonView: UITableView, skeletonCellForRowAt indexPath: IndexPath) -> UITableViewCell? {
    let cell = skeletonView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath) as? Cell
    cell?.textField.isHidden = indexPath.row == 0
    return cell
}

If you prefer to leave the deque part to the library you can configure the cell using this method:

func collectionSkeletonView(_ skeletonView: UITableView, prepareCellForSkeleton cell: UITableViewCell, at indexPath: IndexPath) {
    let cell = cell as? Cell
    cell?.textField.isHidden = indexPath.row == 0
}

Besides, you can skeletonize both the headers and footers. You need to conform to SkeletonTableViewDelegate protocol.

public protocol SkeletonTableViewDelegate: UITableViewDelegate {
    func collectionSkeletonView(_ skeletonView: UITableView, identifierForHeaderInSection section: Int) -> ReusableHeaderFooterIdentifier? // default: nil
    func collectionSkeletonView(_ skeletonView: UITableView, identifierForFooterInSection section: Int) -> ReusableHeaderFooterIdentifier? // default: nil
}

📣 IMPORTANT!

1️⃣ If you are using resizable cells (tableView.rowHeight = UITableViewAutomaticDimension), it's mandatory define the estimatedRowHeight.

2️⃣ When you add elements in a UITableViewCell you should add it to contentView and not to the cell directly.

self.contentView.addSubview(titleLabel) ✅         
self.addSubview(titleLabel) ❌

UICollectionView

For UICollectionView, you need to conform to SkeletonCollectionViewDataSource protocol.

public protocol SkeletonCollectionViewDataSource: UICollectionViewDataSource {
    func numSections(in collectionSkeletonView: UICollectionView) -> Int  // default: 1
    func collectionSkeletonView(_ skeletonView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    func collectionSkeletonView(_ skeletonView: UICollectionView, cellIdentifierForItemAt indexPath: IndexPath) -> ReusableCellIdentifier
    func collectionSkeletonView(_ skeletonView: UICollectionView, supplementaryViewIdentifierOfKind: String, at indexPath: IndexPath) -> ReusableCellIdentifier? // default: nil
    func collectionSkeletonView(_ skeletonView: UICollectionView, skeletonCellForItemAt indexPath: IndexPath) -> UICollectionViewCell?  // default: nil
    func collectionSkeletonView(_ skeletonView: UICollectionView, prepareCellForSkeleton cell: UICollectionViewCell, at indexPath: IndexPath)
    func collectionSkeletonView(_ skeletonView: UICollectionView, prepareViewForSkeleton view: UICollectionReusableView, at indexPath: IndexPath)
}

The rest of the process is the same as UITableView

🔠 Texts

When using elements with text, SkeletonView draws lines to simulate text.

You can set some properties for multilines elements.

PropertyTypeDefaultPreview
lastLineFillPercentCGFloat70
linesCornerRadiusInt0
skeletonLineSpacingCGFloat10
skeletonPaddingInsetsUIEdgeInsets.zero
skeletonTextLineHeightSkeletonTextLineHeight.fixed(15)
skeletonTextNumberOfLinesSkeletonTextNumberOfLines.inherited


To modify the percent or radius using code, set the properties:

descriptionTextView.lastLineFillPercent = 50
descriptionTextView.linesCornerRadius = 5

Or, if you prefer use IB/Storyboard:


 

How to define the number of lines?

By default, the number of lines is the same as the value of the numberOfLines property. And, if it's set to zero, it'll calculate how many lines are needed to populate the whole skeleton and draw it.

However, if you want to set a specific number of skeleton lines you can do it by setting the skeletonTextNumberOfLines property. This property has two possible values, inherited which returns numberOfLines value and custom(Int) which returns the specific number of lines specified as the associated value.

For example:

label.skeletonTextNumberOfLines = 3   // .custom(3)

⚠️ DEPRECATED!

useFontLineHeight has been deprecated. You can use skeletonTextLineHeight instead:

descriptionTextView.skeletonTextLineHeight = .relativeToFont

📣 IMPORTANT!

Please note that for views without multiple lines, the single line will be considered as the last line.

🦋 Appearance

The skeletons have a default appearance. So, when you don't specify the color, gradient or multilines properties, SkeletonView uses the default values.

Default values:

  • tintColor: UIColor
    • default: .skeletonDefault (same as .clouds but adaptive to dark mode)
  • gradient: SkeletonGradient
    • default: SkeletonGradient(baseColor: .skeletonDefault)
  • multilineHeight: CGFloat
    • default: 15
  • multilineSpacing: CGFloat
    • default: 10
  • multilineLastLineFillPercent: Int
    • default: 70
  • multilineCornerRadius: Int
    • default: 0
  • skeletonCornerRadius: CGFloat (IBInspectable) (Make your skeleton view with corner)
    • default: 0

To get these default values you can use SkeletonAppearance.default. Using this property you can set the values as well:

SkeletonAppearance.default.multilineHeight = 20
SkeletonAppearance.default.tintColor = .green

⚠️ DEPRECATED!

useFontLineHeight has been deprecated. You can use textLineHeight instead:

SkeletonAppearance.default.textLineHeight = .relativeToFont

🎨 Custom colors

You can decide which color the skeleton is tinted with. You only need to pass as a parameter the color or gradient you want.

Using solid colors

view.showSkeleton(usingColor: UIColor.gray) // Solid
// or
view.showSkeleton(usingColor: UIColor(red: 25.0, green: 30.0, blue: 255.0, alpha: 1.0))

Using gradients

let gradient = SkeletonGradient(baseColor: UIColor.midnightBlue)
view.showGradientSkeleton(usingGradient: gradient) // Gradient

Besides, SkeletonView features 20 flat colors 🤙🏼

UIColor.turquoise, UIColor.greenSea, UIColor.sunFlower, UIColor.flatOrange ...

Image captured from website https://flatuicolors.com

🏃‍♀️ Animations

SkeletonView has two built-in animations, pulse for solid skeletons and sliding for gradients.

Besides, if you want to do your own skeleton animation, it's really easy.

Skeleton provides the showAnimatedSkeleton function which has a SkeletonLayerAnimation closure where you can define your custom animation.

public typealias SkeletonLayerAnimation = (CALayer) -> CAAnimation

You can call the function like this:

view.showAnimatedSkeleton { (layer) -> CAAnimation in
  let animation = CAAnimation()
  // Customize here your animation

  return animation
}

It's available SkeletonAnimationBuilder. It's a builder to make SkeletonLayerAnimation.

Today, you can create sliding animations for gradients, deciding the direction and setting the duration of the animation (default = 1.5s).

// func makeSlidingAnimation(withDirection direction: GradientDirection, duration: CFTimeInterval = 1.5) -> SkeletonLayerAnimation

let animation = SkeletonAnimationBuilder().makeSlidingAnimation(withDirection: .leftToRight)
view.showAnimatedGradientSkeleton(usingGradient: gradient, animation: animation)

GradientDirection is an enum, with theses cases:

DirectionPreview
.leftRightsliding_left_to_right.gif
.rightLeftsliding_right_to_left.gif
.topBottomsliding_top_to_bottom.gif
.bottomTopsliding_bottom_to_top.gif
.topLeftBottomRightsliding_topLeft_to_bottomRight.gif
.bottomRightTopLeftsliding_bottomRight_to_topLeft.gif

😉 TRICK!

Exist another way to create sliding animations, just using this shortcut:

let animation = GradientDirection.leftToRight.slidingAnimation()

🏄 Transitions

SkeletonView has built-in transitions to show or hide the skeletons in a smoother way 🤙

To use the transition, simply add the transition parameter to your showSkeleton() or hideSkeleton() function with the transition time, like this:

view.showSkeleton(transition: .crossDissolve(0.25))     //Show skeleton cross dissolve transition with 0.25 seconds fade time
view.hideSkeleton(transition: .crossDissolve(0.25))     //Hide skeleton cross dissolve transition with 0.25 seconds fade time

The default value is crossDissolve(0.25)

Preview

NoneCross dissolve
skeleton_transition_nofade.gifskeleton_transition_fade.gif

✨ Miscellaneous

Hierarchy

Since SkeletonView is recursive, and we want skeleton to be very efficient, we want to stop recursion as soon as possible. For this reason, you must set the container view as Skeletonable, because Skeleton will stop looking for skeletonable subviews as soon as a view is not Skeletonable, breaking then the recursion.

Because an image is worth a thousand words:

In this example we have a UIViewController with a ContainerView and a UITableView. When the view is ready, we show the skeleton using this method:

view.showSkeleton()

isSkeletonable= ☠️

ConfigurationResult

Skeleton views layout

Sometimes skeleton layout may not fit your layout because the parent view bounds have changed. For example, rotating the device.

You can relayout the skeleton views like so:

override func viewDidLayoutSubviews() {
    view.layoutSkeletonIfNeeded()
}

📣 IMPORTANT!

You shouldn't call this method. From version 1.8.1 you don't need to call this method, the library does automatically. So, you can use this method ONLY in the cases when you need to update the layout of the skeleton manually.

Update skeleton

You can change the skeleton configuration at any time like its colour, animation, etc. with the following methods:

(1) view.updateSkeleton()                 // Solid
(2) view.updateGradientSkeleton()         // Gradient
(3) view.updateAnimatedSkeleton()         // Solid animated
(4) view.updateAnimatedGradientSkeleton() // Gradient animated

Hiding views when the animation starts

Sometimes you wanna hide some view when the animation starts, so there is a quick property that you can use to make this happen:

view.isHiddenWhenSkeletonIsActive = true  // This works only when isSkeletonable = true

Don't modify user interaction when the skeleton is active

By default, the user interaction is disabled for skeletonized items, but if you don't want to modify the user interaction indicator when skeleton is active, you can use the isUserInteractionDisabledWhenSkeletonIsActive property:

view.isUserInteractionDisabledWhenSkeletonIsActive = false  // The view will be active when the skeleton will be active.

Don't use the font line height for the skeleton lines in labels

False to disable skeleton to auto-adjust to font height for a UILabel or UITextView. By default, the skeleton lines height is auto-adjusted to font height to more accurately reflect the text in the label rect rather than using the bounding box.

label.useFontLineHeight = false

Delayed show skeleton

You can delay the presentation of the skeleton if the views update quickly.

func showSkeleton(usingColor: UIColor,
                  animated: Bool,
                  delay: TimeInterval,
                  transition: SkeletonTransitionStyle)
func showGradientSkeleton(usingGradient: SkeletonGradient,
                          animated: Bool,
                          delay: TimeInterval,
                          transition: SkeletonTransitionStyle)

Debug

To facilitate the debug tasks when something is not working fine. SkeletonView has some new tools.

First, UIView has available a property with his skeleton info:

var sk.skeletonTreeDescription: String

Besides, you can activate the new debug mode. You just add the environment variable SKELETON_DEBUG and activate it.

Then, when the skeleton appears, you can see the view hierarchy in the Xcode console.

{ 
  "type" : "UIView", // UITableView, UILabel...
  "isSkeletonable" : true,
  "reference" : "0x000000014751ce30",
  "children" : [
    {
      "type" : "UIView",
      "isSkeletonable" : true,
      "children" : [ ... ],
      "reference" : "0x000000014751cfa0"
    }
  ]
}

Supported OS & SDK Versions

  • iOS 9.0+
  • tvOS 9.0+
  • Swift 5.3

❤️ Contributing

This is an open source project, so feel free to contribute. How?

  • Open an issue.
  • Send feedback via email.
  • Propose your own fixes, suggestions and open a pull request with the changes.

See all contributors

For more information, please read the contributing guidelines.

📢 Mentions

🌎 README is available in other languages: 🇪🇸 . 🇨🇳 . 🇧🇷 . 🇰🇷 . 🇫🇷 . 🇩🇪

Download Details:

Author: Juanpe
Source Code: https://github.com/Juanpe/SkeletonView 
License: MIT license

#swift #ios #facebook #loading 

SkeletonView: An Elegant Way to Show Users That Something is Happening

Data Analysis with R Programming

Data Analysis Using R Programming | Data Analytics With R | R Programming Tutorial

This video by simplilearn is based on Data analytics using R programming. Data Analytics with R is the most sought profile in the current IT industry especially in the business analytics and data analytics segment. R programming language has a wide spectrum of tools, libraries and function that simplify the tedious task of processing, and reporting the data. 


Exploratory Data Analysis in R Programming

Exploratory Data Analysis or EDA is a statistical approach or technique for analyzing data sets in order to summarize their important and main characteristics generally by using some visual aids. The EDA approach can be used to gather knowledge about the following aspects of data:

  • Main characteristics or features of the data.
  • The variables and their relationships.
  • Finding out the important variables that can be used in our problem.

EDA is an iterative approach that includes:

  • Generating questions about our data
  • Searching for the answers by using visualization, transformation, and modeling of our data.
  • Using the lessons that we learn in order to refine our set of questions or to generate a new set of questions.

Exploratory Data Analysis in R

In R Language, we are going to perform EDA under two broad classifications:

  • Descriptive Statistics, which includes mean, median, mode, inter-quartile range, and so on.
  • Graphical Methods, which includes histogram, density estimation, box plots, and so on.

Before we start working with EDA, we must perform the data inspection properly. Here in our analysis, we will be using the loafercreek from the soilDB package in R. We are going to inspect our data in order to find all the typos and blatant errors. Further EDA can be used to determine and identify the outliers and perform the required statistical analysis. For performing the EDA, we will have to install and load the following packages:

  • “aqp” package
  • “ggplot2” package
  • “soilDB” package

We can install these packages from the R console using the install.packages() command and load them into our R Script by using the library() command. We will now see how to inspect our data and remove the typos and blatant errors. 

Data Inspection for EDA in R

To ensure that we are dealing with the right information we need a clear view of your data at every stage of the transformation process. Data Inspection is the act of viewing data for verification and debugging purposes, before, during, or after a translation. Now let’s see how to inspect and remove the errors and typos from the data.

Example:

# Data Inspection in EDA
# loading the required packages
library(aqp)
library(soilDB)

# Load from the loafercreek dataset
data("loafercreek")

# Construct generalized horizon designations
n < - c("A", "BAt", "Bt1", "Bt2", "Cr", "R")

# REGEX rules
p < - c("A", "BA|AB", "Bt|Bw", "Bt3|Bt4|2B|C",
		"Cr", "R")

# Compute genhz labels and
# add to loafercreek dataset
loafercreek$genhz < - generalize.hz(
	loafercreek$hzname,
	n, p)

# Extract the horizon table
h < - horizons(loafercreek)

# Examine the matching of pairing of
# the genhz label to the hznames
table(h$genhz, h$hzname)

vars < - c("genhz", "clay", "total_frags_pct",
		"phfield", "effclass")
summary(h[, vars])

sort(unique(h$hzname))
h$hzname < - ifelse(h$hzname == "BT",
					"Bt", h$hzname)

Output:

> table(h$genhz, h$hzname)
          
           2BCt 2Bt1 2Bt2 2Bt3 2Bt4 2Bt5 2CB 2CBt 2Cr 2Crt 2R  A A1 A2 AB ABt Ad Ap  B BA BAt BC BCt Bt Bt1 Bt2 Bt3 Bt4 Bw Bw1 Bw2 Bw3  C
  A           0    0    0    0    0    0   0    0   0    0  0 97  7  7  0   0  1  1  0  0   0  0   0  0   0   0   0   0  0   0   0   0  0
  BAt         0    0    0    0    0    0   0    0   0    0  0  0  0  0  1   0  0  0  0 31   8  0   0  0   0   0   0   0  0   0   0   0  0
  Bt1         0    0    0    0    0    0   0    0   0    0  0  0  0  0  0   2  0  0  0  0   0  0   0  8  94  89   0   0 10   2   2   1  0
  Bt2         1    2    7    8    6    1   1    1   0    0  0  0  0  0  0   0  0  0  0  0   0  5  16  0   0   0  47   8  0   0   0   0  6
  Cr          0    0    0    0    0    0   0    0   4    2  0  0  0  0  0   0  0  0  0  0   0  0   0  0   0   0   0   0  0   0   0   0  0
  R           0    0    0    0    0    0   0    0   0    0  5  0  0  0  0   0  0  0  0  0   0  0   0  0   0   0   0   0  0   0   0   0  0
  not-used    0    0    0    0    0    0   0    0   0    0  0  0  0  0  0   0  0  0  1  0   0  0   0  0   0   0   0   0  0   0   0   0  0
          
           CBt Cd Cr Cr/R Crt H1 Oi  R Rt
  A          0  0  0    0   0  0  0  0  0
  BAt        0  0  0    0   0  0  0  0  0
  Bt1        0  0  0    0   0  0  0  0  0
  Bt2        6  1  0    0   0  0  0  0  0
  Cr         0  0 49    0  20  0  0  0  0
  R          0  0  0    1   0  0  0 41  1
  not-used   0  0  0    0   0  1 24  0  0

> summary(h[, vars])
      genhz          clay       total_frags_pct    phfield            effclass  
 A       :113   Min.   :10.00   Min.   : 0.00   Min.   :4.90   very slight:  0  
 BAt     : 40   1st Qu.:18.00   1st Qu.: 0.00   1st Qu.:6.00   slight     :  0  
 Bt1     :208   Median :22.00   Median : 5.00   Median :6.30   strong     :  0  
 Bt2     :116   Mean   :23.67   Mean   :14.18   Mean   :6.18   violent    :  0  
 Cr      : 75   3rd Qu.:28.00   3rd Qu.:20.00   3rd Qu.:6.50   none       : 86  
 R       : 48   Max.   :60.00   Max.   :95.00   Max.   :7.00   NA's       :540  
 not-used: 26   NA's   :173                     NA's   :381                     

> sort(unique(h$hzname))
 [1] "2BCt" "2Bt1" "2Bt2" "2Bt3" "2Bt4" "2Bt5" "2CB"  "2CBt" "2Cr"  "2Crt" "2R"   "A"    "A1"   "A2"   "AB"   "ABt"  "Ad"   "Ap"   "B"   
[20] "BA"   "BAt"  "BC"   "BCt"  "Bt"   "Bt1"  "Bt2"  "Bt3"  "Bt4"  "Bw"   "Bw1"  "Bw2"  "Bw3"  "C"    "CBt"  "Cd"   "Cr"   "Cr/R" "Crt" 
[39] "H1"   "Oi"   "R"    "Rt" 

Now proceed with the EDA.

Descriptive Statistics in EDA 

For Descriptive Statistics in order to perform EDA in R, we will divide all the functions into the following categories: 

  • Measures of central tendency
  • Measures of dispersion
  • Correlation

We will try to determine the mid-point values using the functions under the Measures of Central tendency. Under this section, we will be calculating the mean, median, mode, and frequencies.

Example 1: Now see the measures of central tendency in this example.

# EDA
# Descriptive Statistics
# Measures of Central Tendency

#loading the required packages
library(aqp)
library(soilDB)

# Load from the loafercreek dataset
data("loafercreek")

# Construct generalized horizon designations
n <- c("A", "BAt", "Bt1", "Bt2", "Cr", "R")

# REGEX rules
p <- c("A", "BA|AB", "Bt|Bw", "Bt3|Bt4|2B|C",
	"Cr", "R")

# Compute genhz labels and
# add to loafercreek dataset
loafercreek$genhz <- generalize.hz(
					loafercreek$hzname,
					n, p)

# Extract the horizon table
h <- horizons(loafercreek)

# Examine the matching of pairing
# of the genhz label to the hznames
table(h$genhz, h$hzname)

vars <- c("genhz", "clay", "total_frags_pct",
		"phfield", "effclass")
summary(h[, vars])

sort(unique(h$hzname))
h$hzname <- ifelse(h$hzname == "BT",
				"Bt", h$hzname)

# first remove missing values
# and create a new vector
clay <- na.exclude(h$clay)

mean(clay)
median(clay)
sort(table(round(h$clay)),
	decreasing = TRUE)[1]
table(h$genhz)
# append the table with
# row and column sums
addmargins(table(h$genhz,
				h$texcl))

# calculate the proportions
# relative to the rows, margin = 1
# calculates for rows, margin = 2 calculates
# for columns, margin = NULL calculates
# for total observations
round(prop.table(table(h$genhz, h$texture_class),
				margin = 1) * 100)
knitr::kable(addmargins(table(h$genhz, h$texcl)))

aggregate(clay ~ genhz, data = h, mean)
aggregate(clay ~ genhz, data = h, median)
aggregate(clay ~ genhz, data = h, summary)

 Output: 

> mean(clay)
[1] 23.6713
> median(clay)
[1] 22
> sort(table(round(h$clay)), decreasing = TRUE)[1]
25 
41 
> table(h$genhz)

       A      BAt      Bt1      Bt2       Cr        R not-used 
     113       40      208      116       75       48       26 

> addmargins(table(h$genhz, h$texcl))
          
           cos   s  fs vfs lcos  ls lfs lvfs cosl  sl fsl vfsl   l sil  si scl  cl sicl  sc sic   c Sum
  A          0   0   0   0    0   0   0    0    0   6   0    0  78  27   0   0   0    0   0   0   0 111
  BAt        0   0   0   0    0   0   0    0    0   1   0    0  31   4   0   0   2    1   0   0   0  39
  Bt1        0   0   0   0    0   0   0    0    0   1   0    0 125  20   0   4  46    5   0   1   2 204
  Bt2        0   0   0   0    0   0   0    0    0   0   0    0  28   5   0   5  52    3   0   1  16 110
  Cr         0   0   0   0    0   0   0    0    0   0   0    0   0   0   0   0   0    0   0   0   1   1
  R          0   0   0   0    0   0   0    0    0   0   0    0   0   0   0   0   0    0   0   0   0   0
  not-used   0   0   0   0    0   0   0    0    0   0   0    0   0   0   0   0   1    0   0   0   0   1
  Sum        0   0   0   0    0   0   0    0    0   8   0    0 262  56   0   9 101    9   0   2  19 466

> round(prop.table(table(h$genhz, h$texture_class), margin = 1) * 100)
          
            br   c  cb  cl  gr   l  pg scl sic sicl sil  sl spm
  A          0   0   0   0   0  70   0   0   0    0  24   5   0
  BAt        0   0   0   5   0  79   0   0   0    3  10   3   0
  Bt1        0   1   0  23   0  61   0   2   0    2  10   0   0
  Bt2        0  14   1  46   2  25   1   4   1    3   4   0   0
  Cr        98   2   0   0   0   0   0   0   0    0   0   0   0
  R        100   0   0   0   0   0   0   0   0    0   0   0   0
  not-used   0   0   0   4   0   0   0   0   0    0   0   0  96

> knitr::kable(addmargins(table(h$genhz, h$texcl)))


|         | cos|  s| fs| vfs| lcos| ls| lfs| lvfs| cosl| sl| fsl| vfsl|   l| sil| si| scl|  cl| sicl| sc| sic|  c| Sum|
|:--------|---:|--:|--:|---:|----:|--:|---:|----:|----:|--:|---:|----:|---:|---:|--:|---:|---:|----:|--:|---:|--:|---:|
|A        |   0|  0|  0|   0|    0|  0|   0|    0|    0|  6|   0|    0|  78|  27|  0|   0|   0|    0|  0|   0|  0| 111|
|BAt      |   0|  0|  0|   0|    0|  0|   0|    0|    0|  1|   0|    0|  31|   4|  0|   0|   2|    1|  0|   0|  0|  39|
|Bt1      |   0|  0|  0|   0|    0|  0|   0|    0|    0|  1|   0|    0| 125|  20|  0|   4|  46|    5|  0|   1|  2| 204|
|Bt2      |   0|  0|  0|   0|    0|  0|   0|    0|    0|  0|   0|    0|  28|   5|  0|   5|  52|    3|  0|   1| 16| 110|
|Cr       |   0|  0|  0|   0|    0|  0|   0|    0|    0|  0|   0|    0|   0|   0|  0|   0|   0|    0|  0|   0|  1|   1|
|R        |   0|  0|  0|   0|    0|  0|   0|    0|    0|  0|   0|    0|   0|   0|  0|   0|   0|    0|  0|   0|  0|   0|
|not-used |   0|  0|  0|   0|    0|  0|   0|    0|    0|  0|   0|    0|   0|   0|  0|   0|   1|    0|  0|   0|  0|   1|
|Sum      |   0|  0|  0|   0|    0|  0|   0|    0|    0|  8|   0|    0| 262|  56|  0|   9| 101|    9|  0|   2| 19| 466|
> aggregate(clay ~ genhz, data = h, mean)
  genhz     clay
1     A 16.23113
2   BAt 19.53889
3   Bt1 24.14221
4   Bt2 31.35045
5    Cr 15.00000

> aggregate(clay ~ genhz, data = h, median)
  genhz clay
1     A 16.0
2   BAt 19.5
3   Bt1 24.0
4   Bt2 30.0
5    Cr 15.0

> aggregate(clay ~ genhz, data = h, summary)
  genhz clay.Min. clay.1st Qu. clay.Median clay.Mean clay.3rd Qu. clay.Max.
1     A  10.00000     14.00000    16.00000  16.23113     18.00000  25.00000
2   BAt  14.00000     17.00000    19.50000  19.53889     20.00000  28.00000
3   Bt1  12.00000     20.00000    24.00000  24.14221     28.00000  51.40000
4   Bt2  10.00000     26.00000    30.00000  31.35045     35.00000  60.00000
5    Cr  15.00000     15.00000    15.00000  15.00000     15.00000  15.00000

Now we will see the functions under Measures of Dispersion. In this category, we are going to determine the spread values around the mid-point. Here we are going to calculate the variance, standard deviation, range, inter-quartile range, coefficient of variance, and quartiles. 

Example 2:

We shall see the measures of dispersion in this example. 

# EDA
# Descriptive Statistics
# Measures of Dispersion

# loading the packages
library(aqp)
library(soilDB)

# Load from the loafercreek dataset
data("loafercreek")

# Construct generalized horizon designations
n <- c("A", "BAt", "Bt1", "Bt2", "Cr", "R")

# REGEX rules
p <- c("A", "BA|AB", "Bt|Bw", "Bt3|Bt4|2B|C",
	"Cr", "R")

# Compute genhz labels and add
# to loafercreek dataset
loafercreek$genhz <- generalize.hz(
					loafercreek$hzname,
					n, p)

# Extract the horizon table
h <- horizons(loafercreek)

# Examine the matching of pairing of
# the genhz label to the hznames
table(h$genhz, h$hzname)

vars <- c("genhz", "clay", "total_frags_pct",
		"phfield", "effclass")
summary(h[, vars])

sort(unique(h$hzname))
h$hzname <- ifelse(h$hzname == "BT",
				"Bt", h$hzname)

# first remove missing values
# and create a new vector
clay <- na.exclude(h$clay)
var(h$clay, na.rm=TRUE)
sd(h$clay, na.rm = TRUE)
cv <- sd(clay) / mean(clay) * 100
cv
quantile(clay)
range(clay)
IQR(clay)

 Output: 

> var(h$clay, na.rm=TRUE)
[1] 64.89187

> sd(h$clay, na.rm = TRUE)
[1] 8.055549

> cv
[1] 34.03087

> quantile(clay)
  0%  25%  50%  75% 100% 
  10   18   22   28   60 

> range(clay)
[1] 10 60

> IQR(clay)
[1] 10

Now we will work on Correlation. In this part, all the calculated correlation coefficient values of all variables in tabulated as the Correlation Matrix. This gives us a quantitative measure in order to guide our decision-making process. 

Example 3:

We shall now see the correlation in this example. 

# EDA
# Descriptive Statistics
# Correlation

# loading the required packages
library(aqp)
library(soilDB)

# Load from the loafercreek dataset
data("loafercreek")

# Construct generalized horizon designations
n <- c("A", "BAt", "Bt1", "Bt2", "Cr", "R")

# REGEX rules
p <- c("A", "BA|AB", "Bt|Bw", "Bt3|Bt4|2B|C",
	"Cr", "R")

# Compute genhz labels and add
# to loafercreek dataset
loafercreek$genhz <- generalize.hz(
					loafercreek$hzname,
					n, p)

# Extract the horizon table
h <- horizons(loafercreek)

# Examine the matching of pairing
# of the genhz label to the hznames
table(h$genhz, h$hzname)

vars <- c("genhz", "clay", "total_frags_pct",
		"phfield", "effclass")
summary(h[, vars])

sort(unique(h$hzname))
h$hzname <- ifelse(h$hzname == "BT",
				"Bt", h$hzname)

# first remove missing values
# and create a new vector
clay <- na.exclude(h$clay)

# Compute the middle horizon depth
h$hzdepm <- (h$hzdepb + h$hzdept) / 2
vars <- c("hzdepm", "clay", "sand",
		"total_frags_pct", "phfield")
round(cor(h[, vars], use = "complete.obs"), 2)

Output: 

                hzdepm  clay  sand total_frags_pct phfield
hzdepm            1.00  0.59 -0.08            0.50   -0.03
clay              0.59  1.00 -0.17            0.28    0.13
sand             -0.08 -0.17  1.00           -0.05    0.12
total_frags_pct   0.50  0.28 -0.05            1.00   -0.16
phfield          -0.03  0.13  0.12           -0.16    1.00

Hence, the above three classifications deal with the Descriptive statistics part of EDA. Now we shall move on to the Graphical Method of representing EDA.

Graphical Method in EDA 

Since we have already checked our data for missing values, blatant errors, and typos, we can now examine our data graphically in order to perform EDA. We will see the graphical representation under the following categories: 

  • Distributions
  • Scatter and Line plot

Under the Distribution, we shall examine our data using the bar plot, Histogram, Density curve, box plots, and QQplot. 

Example 1: 

We shall see how distribution graphs can be used to examine data in EDA in this example.

# EDA Graphical Method Distributions

# loading the required packages
library("ggplot2")
library(aqp)
library(soilDB)

# Load from the loafercreek dataset
data("loafercreek")

# Construct generalized horizon designations
n <- c("A", "BAt", "Bt1", "Bt2", "Cr", "R")

# REGEX rules
p <- c("A", "BA|AB", "Bt|Bw", "Bt3|Bt4|2B|C",
	"Cr", "R")

# Compute genhz labels and add
# to loafercreek dataset
loafercreek$genhz <- generalize.hz(
					loafercreek$hzname, n, p)

# Extract the horizon table
h <- horizons(loafercreek)

# Examine the matching of pairing
# of the genhz label to the hznames
table(h$genhz, h$hzname)

vars <- c("genhz", "clay", "total_frags_pct",
		"phfield", "effclass")
summary(h[, vars])

sort(unique(h$hzname))
h$hzname <- ifelse(h$hzname == "BT",
				"Bt", h$hzname)

# graphs
# bar plot
ggplot(h, aes(x = texcl)) +geom_bar()

# histogram
ggplot(h, aes(x = clay)) +
geom_histogram(bins = nclass.Sturges(h$clay))

# density curve
ggplot(h, aes(x = clay)) + geom_density()

# box plot
ggplot(h, (aes(x = genhz, y = clay))) +
geom_boxplot()

# QQ Plot for Clay
ggplot(h, aes(sample = clay)) +
geom_qq() +
geom_qq_line()

Output:

BAR PLOTHISTOGRAMDENSITY CURVEBOX PLOTQQ PLOT

Now we will move on to the Scatter and Line plot. In this category, we are going to see two types of plotting,- scatter plot and line plot. Plotting points of one interval or ratio variable against variable are known as a scatter plot. 

Example 2:

We shall now see how to use scatter and line plots to examine our data. 

# EDA
# Graphical Method
# Scatter and Line plot

# loading the required packages
library("ggplot2")
library(aqp)
library(soilDB)

# Load from the loafercreek dataset
data("loafercreek")

# Construct generalized horizon designations
n <- c("A", "BAt", "Bt1", "Bt2", "Cr", "R")

# REGEX rules
p <- c("A", "BA|AB", "Bt|Bw", "Bt3|Bt4|2B|C",
	"Cr", "R")

# Compute genhz labels and add
# to loafercreek dataset
loafercreek$genhz <- generalize.hz(
					loafercreek$hzname, n, p)

# Extract the horizon table
h <- horizons(loafercreek)

# Examine the matching of pairing
# of the genhz label to the hznames
table(h$genhz, h$hzname)

vars <- c("genhz", "clay", "total_frags_pct",
		"phfield", "effclass")
summary(h[, vars])

sort(unique(h$hzname))
h$hzname <- ifelse(h$hzname == "BT",
				"Bt", h$hzname)

# graph
# scatter plot
ggplot(h, aes(x = clay, y = hzdepm)) +
geom_point() +
ylim(100, 0)

# line plot
ggplot(h, aes(y = clay, x = hzdepm,
					group = peiid)) +
geom_line() + coord_flip() + xlim(100, 0)

Output:

SCATTER PLOTLINE PLOT


#dataanalysis #r

Data Analysis with R Programming
Reid  Rohan

Reid Rohan

1663003440

8 Best Libraries for Module Or Loading System with JavaScript

In today's post we will learn about 8 Best Libraries for Module or loading system with JavaScript. 

Module Systems provide a way to manage dependencies in JavaScript.

In vanilla client-side JavaScript development, dependencies are implicit: they need to be defined manually, sometimes they need to be also defined in certain order.

Node.js Modules is an extension to CommonJS Modules (1.1).

Asynchronous Module Definition (AMD) is the most popular for client-side code.

Universal Module Definition (UMD) is a set of boilerplate recipes that attempt to bridge the differences between AMD and Node.js.

Table of contents:

  • RequireJS - A file and module loader for JavaScript.
  • Browserify - Browser-side require() the node.js way.
  • SeaJS - A Module Loader for the Web.
  • HeadJS - The only script in your HEAD.
  • Lazyload - Tiny, dependency-free async JavaScript and CSS loader.
  • Script.js - Asynchronous JavaScript loader and dependency manager.
  • Systemjs - AMD, CJS & ES6 spec-compliant module loader.
  • LodJS - Module loader based on AMD.

1 - RequireJS: A file and module loader for JavaScript.

Directories

  • dist: Scripts and assets to generate the requirejs.org docs, and for generating a require.js release.
  • docs: The raw HTML files for the requirejs.org docs. Only includes the body of each page. Files in dist are used to generate a complete HTML page.
  • tests: Tests for require.js.
  • testBaseUrl.js: A file used in the tests inside tests. Purposely placed outside the tests directory for testing paths that go outside a baseUrl.
  • updatesubs.sh: Updates projects that depend on require.js Assumes the projects are siblings to this directory and have specific names. Useful to copy require.js to dependent projects easily while in development.

Tests

This repo assumes some other repos are checked out as siblings to this repo:

git clone https://github.com/requirejs/text.git
git clone https://github.com/requirejs/i18n.git
git clone https://github.com/requirejs/domReady.git
git clone https://github.com/requirejs/requirejs.git

So when the above clones are done, the directory structure should look like:

  • domReady
  • i18n
  • text
  • requirejs (this repo)

You will need to be connected to the internet because the JSONP and remoteUrls tests access the internet to complete their tests.

Serve the directory with these 4 siblings from a web server. It can be a local web server.

Open requirejs/tests/index.html in all the browsers, click the arrow button to run all the tests.

View on Github

2 - Browserify: Browser-side require() the node.js way.

getting started

If you're new to browserify, check out the browserify handbook and the resources on browserify.org.

example

Whip up a file, main.js with some require()s in it. You can use relative paths like './foo.js' and '../lib/bar.js' or module paths like 'gamma' that will search node_modules/ using node's module lookup algorithm.

var foo = require('./foo.js');
var bar = require('../lib/bar.js');
var gamma = require('gamma');

var elem = document.getElementById('result');
var x = foo(100) + bar('baz');
elem.textContent = gamma(x);

Export functionality by assigning onto module.exports or exports:

module.exports = function (n) { return n * 111 }

Now just use the browserify command to build a bundle starting at main.js:

$ browserify main.js > bundle.js

All of the modules that main.js needs are included in the bundle.js from a recursive walk of the require() graph using required.

To use this bundle, just toss a <script src="bundle.js"></script> into your html!

install

With npm do:

npm install browserify

View on Github

3 - SeaJS: A Module Loader for the Web.

Sea.js is a module loader for the web. It is designed to change the way that you organize JavaScript. With Sea.js, it is pleasure to build scalable web applications.

The official site: https://seajs.github.io/seajs/

Questions?

If you have any questions, please feel free to ask through New Issue.

Reporting an Issue

Make sure the problem you're addressing is reproducible. Use http://jsbin.com/ or http://jsfiddle.net/ to provide a test page. Indicate what browsers the issue can be reproduced in. What version of Sea.js is the issue reproducible in. Is it reproducible after updating to the latest version?

View on Github

4 - HeadJS: The only script in your HEAD.

Responsive Design, Feature Detections, and Resource Loading

  • Speed up your apps: Load JS & CSS asyncronously and in parallel, but execute them in order
  • Load one asset if a condition is met, else fallback and load a different one
  • Manage script dependencies, and execute callbacks once they are loaded
  • Cross-browser compatible « pseudo media-queries » let you code against different resolutions & devices
  • Fix quirks in specific browsers by quickly applying dedicated CSS/JS logic
  • Detect various browsers & their versions
  • Check if the client supports a certain Browser, HTML5, or CSS3 feature
  • Automatically generates JS and CSS classes for browsers & features that where detected
  • Automatically generates CSS classes, to know what page or section a user is viewing
  • Know if the user is in landscape or portrait mode
  • Or whether the client is using a mobile or desktop device
  • Get old browsers to support HTML5 elements like nav, sidebar, header, footer, ...
  • ...
  • Make it, The only script in your <HEAD>
    • A concise solution to universal problems

View on Github

5 - Lazyload: Tiny, dependency-free async JavaScript and CSS loader.

LazyLoad is a tiny (only 966 bytes minified and gzipped), dependency-free JavaScript utility that makes it super easy to load external JavaScript and CSS files on demand.

Whenever possible, LazyLoad will automatically load resources in parallel while ensuring execution order when you specify an array of URLs to load. In browsers that don't preserve the execution order of asynchronously-loaded scripts, LazyLoad will safely load the scripts sequentially.

Usage

Using LazyLoad is simple. Just call the appropriate method -- css() to load CSS, js() to load JavaScript -- and pass in a URL or array of URLs to load. You can also provide a callback function if you'd like to be notified when the resources have finished loading, as well as an argument to pass to the callback and a context in which to execute the callback.

// Load a single JavaScript file and execute a callback when it finishes.
LazyLoad.js('http://example.com/foo.js', function () {
  alert('foo.js has been loaded');
});

// Load multiple JS files and execute a callback when they've all finished.
LazyLoad.js(['foo.js', 'bar.js', 'baz.js'], function () {
  alert('all files have been loaded');
});

// Load a CSS file and pass an argument to the callback function.
LazyLoad.css('foo.css', function (arg) {
  alert(arg);
}, 'foo.css has been loaded');

// Load a CSS file and execute the callback in a different scope.
LazyLoad.css('foo.css', function () {
  alert(this.foo); // displays 'bar'
}, null, {foo: 'bar'});

Supported Browsers

  • Firefox 2+
  • Google Chrome
  • Internet Explorer 6+
  • Opera 9+
  • Safari 3+
  • Mobile Safari
  • Android

Other browsers may work, but haven't been tested. It's a safe bet that anything based on a recent version of Gecko or WebKit will probably work.

Downloads

View on Github

6 - Script.js: Asynchronous JavaScript loader and dependency manager.

Browser Support

  • IE 6+
  • Opera 10+
  • Safari 3+
  • Chrome 1+
  • Firefox 2+

Examples

old school - blocks CSS, Images, AND JS!

<script src="jquery.js"></script>
<script src="my-jquery-plugin.js"></script>
<script src="my-app-that-uses-plugin.js"></script>

middle school - loads as non-blocking, but has multiple dependents

$script('jquery.js', function () {
  $script('my-jquery-plugin.js', function () {
    $script('my-app-that-uses-plugin.js')
  })
})

new school - loads as non-blocking, and ALL js files load asynchronously

// load jquery and plugin at the same time. name it 'bundle'
$script(['jquery.js', 'my-jquery-plugin.js'], 'bundle')

// load your usage
$script('my-app-that-uses-plugin.js')


/*--- in my-jquery-plugin.js ---*/
$script.ready('bundle', function() {
  // jquery & plugin (this file) are both ready
  // plugin code...
})


/*--- in my-app-that-uses-plugin.js ---*/
$script.ready('bundle', function() {
  // use your plugin :)
})

View on Github

7 - Systemjs: AMD, CJS & ES6 spec-compliant module loader.

Getting Started

Introduction video.

The systemjs-examples repo contains a variety of examples demonstrating how to use SystemJS.

Installation

npm install systemjs

Example Usage

Loading a System.register module

You can load System.register modules with a script element in your HTML:

<script src="system.js"></script>
<script type="systemjs-module" src="/js/main.js"></script>
<script type="systemjs-module" src="import:name-of-module"></script>

Loading with System.import

You can also dynamically load modules at any time with System.import():

System.import('/js/main.js');

where main.js is a module available in the System.register module format.

Bundling workflow

For an example of a bundling workflow, see the Rollup Code Splitting starter project - https://github.com/rollup/rollup-starter-code-splitting.

Note that when building System modules you typically want to ensure anonymous System.register statements like:

System.register([], function () { ... });

are emitted, as these can be loaded in a way that behaves the same as normal ES modules, and not named register statements like:

System.register('name', [], function () { ... });

While these can be supported with the named register extension, this approach is typically not recommended for modern modules workflows.

View on Github

8 - LodJS: Module loader based on AMD.

How to Use?

Traditional Usage

<script src="lodjs.js"></script>

Bower

$ bower install lodjs
$ bower install git://github.com/yanhaijing/lodjs.git

Quick Start

Define Module

We use lodJS's global function define to define a module, for example, in mod.js, we have the following code:

define(function () {
	return 123;
});

Module Usage

The use method in lodJS uses a module. The following code can use the module defined above:

lodjs.use('mod', function (mod) {
	console.log(mod);// Outputs 123
});

For more examples, please see the directory of demo.

View on Github

Thank you for following this article.

#javascript #loading #modules 

8 Best Libraries for Module Or Loading System with JavaScript
Hunter  Krajcik

Hunter Krajcik

1662064800

A Useful Collection Of Widgets to Display Loading States

TODO: Put a short description of the package here that helps potential users know whether this package might be useful for them.

Features

TODO: List what your package can do. Maybe include images, gifs, or videos.

Getting started

TODO: List prerequisites and provide or point to information on how to start using the package.

Usage

TODO: Include short and useful examples for package users. Add longer examples to /example folder.

const like = 'sample';

Additional information

TODO: Tell users more about the package: where to find more information, how to contribute to the package, how to file issues, what response they can expect from the package authors, and more.

Use this package as a library

Depend on it

Run this command:

With Flutter:

 $ flutter pub add center_loading

This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):

dependencies:
  center_loading: ^0.0.3

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import 'package:center_loading/center_loading.dart';

Download Details:

Author: ftognetto
Source Code: https://github.com/ftognetto/center_loading 
License: MIT license

#flutter #dart #loading 

A Useful Collection Of Widgets to Display Loading States