<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Barri </title>
    <description>The latest articles on DEV Community by Barri  (@barrisam).</description>
    <link>https://dev.to/barrisam</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F271944%2F99c86584-9e11-4f12-83c0-65865779a383.png</url>
      <title>DEV Community: Barri </title>
      <link>https://dev.to/barrisam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/barrisam"/>
    <language>en</language>
    <item>
      <title>5 Open Source Large Language Models APIs for Developers</title>
      <dc:creator>Barri </dc:creator>
      <pubDate>Wed, 01 May 2024 20:41:56 +0000</pubDate>
      <link>https://dev.to/barrisam/5-open-source-large-language-models-apis-for-developers-4j99</link>
      <guid>https://dev.to/barrisam/5-open-source-large-language-models-apis-for-developers-4j99</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Large language models (LLMs) have revolutionized the field of natural language processing (NLP) in recent years, enabling a wide range of new applications, such as writing services, translation, editing, and conversations with chatboxes. With the increasing popularity of LLMs, there is a growing demand for open-source APIs that allow developers to easily integrate these models into their projects.&lt;/p&gt;

&lt;p&gt;From text generation and language translation to code synthesis and sentiment analysis, these APIs serve as essential tools for developers and businesses seeking to integrate cutting-edge language models seamlessly into their applications.&lt;/p&gt;

&lt;p&gt;This comprehensive article aims to provide insights into the distinctive features, how they can be installed, and the usability of each open-source API. It delves into five of the most popular and widely utilized APIs for large language models, exploring the diverse functionalities they offer and their applications in the realm of NLP and offering a valuable resource for those navigating the ever-expanding landscape of language processing technologies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Open Source Large Language Models APIs for Developers
&lt;/h2&gt;

&lt;p&gt;Here are 5 open-source APIs for large language models&lt;/p&gt;

&lt;h3&gt;
  
  
  1. BERT API
&lt;/h3&gt;

&lt;p&gt;BERT, which stands for Bidirectional Encoder Representations from Transformers, is a Natural Language Processing (NLP) model developed by Google. It can be used for translation, text generation, and text summarization.&lt;/p&gt;

&lt;p&gt;To make it more accessible to developers, Google released pre-trained models along with the TensorFlow library. These pre-trained models come in different sizes, and developers can choose the one that best fits their specific requirements and computational resources.&lt;/p&gt;

&lt;p&gt;Also, developers can obtain the pre-trained BERT model from various sources, including the official BERT GitHub repository or model hubs like Hugging Face’s Transformers library. A library that provides interfaces for both TensorFlow and PyTorch.&lt;/p&gt;

&lt;h4&gt;
  
  
  How to install and use BERT API in Python
&lt;/h4&gt;

&lt;p&gt;Using the BERT API involves several steps, from installing the necessary libraries to loading the pre-trained BERT model and generating text or performing other NLP tasks. Here’s a general overview of the process:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install the Transformers library using pip:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;pip install transformers&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will install the BERT API along with other NLP models and tools.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Import the necessary classes from the Transformers library:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;from transformers import AutoTokenizer, AutoModelForSequenceClassification&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;These classes provide tokenization and classification capabilities for BERT models.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Load the pre-trained BERT model you want to use:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;model_name = “bert-base-uncased”&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Load the model from the Hugging Face model hub:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

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

&lt;/div&gt;



&lt;p&gt;This will load the pre-trained BERT model and tokenizer for the specified model name.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prepare Input Text and encode the input text into tokens using the tokenizer:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`input_text = “This is text to be processed”
encoded_text = tokenizer(input_text, truncation=True, padding=True, return_tensors=”pt”)`

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

&lt;/div&gt;



&lt;p&gt;This will convert the input text into numerical representations the BERT model can understand.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Perform a BERT Task. For example, text classification.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;predictions = model(encoded_text)
predicted_label = predictions[0][0]

print(predicted_label)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will classify the input text into a specified set of labels.&lt;/p&gt;

&lt;h4&gt;
  
  
  Some key features of BERT include:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;BERT is pre-trained on a massive amount of text data, allowing it to learn general language representations. The pre-training involves predicting missing words in sentences (masked language model) and determining whether two sentences follow each other in the original text (next sentence prediction).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;BERT has achieved state-of-the-art results on a wide range of NLP benchmarks and competitions. Its ability to capture intricate contextual relationships in language has made it a go-to choice for many NLP tasks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;BERT has been extended to support multiple languages, enabling its application in a global context for tasks like language translation and cross-lingual information retrieval.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;BERT is designed to be fine-tuned for specific downstream tasks, such as text classification, named entity recognition, question answering, and more. This transfer learning approach allows BERT to leverage its pre-trained knowledge for various NLP applications.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  2. ChatGPT API
&lt;/h3&gt;

&lt;p&gt;Unarguably one of the biggest and powerful large language models and the most popular LLM in the world. ChatGPT (Chat Generative Pre-Trained Transformer) API attracts users in millions. The ChatGPTAPI is an interface created by OpenAI that allows developers to integrate GPT3 and GPT4 models into private, personal applications or programming languages like Python to build intelligent, conversational interfaces.&lt;/p&gt;

&lt;p&gt;ChatGPT is renowned for its abilities to generate human-like quality text, translate languages, write programming code, write different kinds of creative content, and answer questions in an informative way.&lt;/p&gt;

&lt;p&gt;Integrating ChatGPT into a programming language involves establishing a connection between the ChatGPT API and a chosen programming language. This allows you to leverage ChatGPT’s text-generation capabilities within your programming environment.&lt;/p&gt;

&lt;p&gt;The specific steps and required libraries will vary depending on the programming language you’re using, but the general process involves the following steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create API Key. Generate a unique access code to enable communication and authentication with the API. You can generate an API key here.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install OpenAI API: Begin by installing the OpenAI API using your programming language’s package manager.&lt;br&gt;
pip install openai&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the OpenAI API is installed, import the classes and use the bindings and your secret key to run the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from openai import OpenAI
client = OpenAI(
 api_key="Your_API_Key",
)
chat_completion = client.chat.completions.create(
 model=”gpt-3.5-turbo”,
 messages=[{“role”: “user”, “content”: “Hello world”}]
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Key features of ChatGPT include:
&lt;/h4&gt;

&lt;p&gt;ChatGPT is known for its following key features:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Large and powerful Model:&lt;/strong&gt; ChatGPT is one of the largest open-source LLMs available. This means that it is capable of performing a wide range of tasks and generating high-quality output.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Versatility:&lt;/strong&gt; ChatGPT can be used for a variety of NLP tasks, including text generation, translation, question answering, and code generation. This makes it a valuable tool for a wide range of applications.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;3. Pathways System:&lt;/strong&gt; ChatGPT is trained using the Pathways system, a novel training paradigm that enhances the model’s efficiency and adaptability. This system allows ChatGPT to learn from multiple sources of data and adapt to new tasks more effectively&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Chain-of-Thought Reasoning:&lt;/strong&gt; ChatGPT employs a chain-of-thought reasoning approach, allowing it to break down complex problems into smaller, more manageable steps. This capability enhances its ability to answer open-ended questions, provide explanations for its reasoning, and generate more coherent and relevant text.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Dense Decoder-Only Transformer:&lt;/strong&gt; ChatGPT utilizes a dense decoder-only transformer architecture, which improves its fluency and coherence in generated text. This architecture enables the model to better capture long-range dependencies in language and produce more natural-sounding text.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. LLAMA
&lt;/h3&gt;

&lt;p&gt;LLAMA (Large Language Model Meta AI) is a large language model developed by Meta AI. Described “an easy-to-use API for LLAMA mocan dels. It is available in multiple sizes (7B, 13B, 33B, and 65B parameters). It is free for commercial and research purposes and it aims to democratize access to large language models by requiring less computing power and resources for training and deployment.&lt;/p&gt;

&lt;p&gt;LLAMA is capable of performing many kinds of tasks, including text generation, translation, question answering, and code generation. The new LLAMA 2 model is even more impressive. It was trained on 40% more data than LLAMA 1 and has double the context length.&lt;/p&gt;

&lt;p&gt;Available as an open-source API, it can be used with a variety of programming languages. There is also a pre-trained LLAMA model available, so you can get started right away.&lt;/p&gt;

&lt;p&gt;To use LLAMA, you will need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install the LLAMA API: The LLAMA API can be installed using your programming language’s package manager.
For python, that would be&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;pip install llama-cpp-python&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Or you can download the model from the official website.&lt;br&gt;
Or load a pre-trained LLAMA model from a file or a remote repository.&lt;/p&gt;

&lt;p&gt;Now, import the classes and specify the model path&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from llama_cpp import Llama
llm = Llama(model_path=”./models/7B/ggml-model.bin”)
output = llm(“Q: Name the planets in the solar system? A: “, max_tokens=32, stop=[“Q:”, “\n”], echo=True)
print(output)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Process your input text: The LLAMA API can accept text input in a variety of formats, including plain text, HTML, and JSON.&lt;br&gt;
Generate output text: The LLAMA API can generate text in a variety of formats, including plain text, HTML, and JSON.&lt;/p&gt;
&lt;h4&gt;
  
  
  Key features of LLAMA:
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;1. Large and powerful:&lt;/strong&gt; LLAMA is one of the largest open-source LLMs available. This means that it is capable of performing a wide range of tasks and generating high-quality output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Versatile:&lt;/strong&gt; LLAMA can be used for a variety of NLP tasks, including text generation, translation, question answering, code generation, creative writing, and code completion. This makes it a valuable tool for a wide range of applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Open-source:&lt;/strong&gt; LLAMA is an open-source model, which means that it is freely available for anyone to use. This makes it a great choice for developers who want to experiment with LLMs.&lt;/p&gt;
&lt;h3&gt;
  
  
  4. PaLM API
&lt;/h3&gt;

&lt;p&gt;PaLM (Pathways Language Model) is a groundbreaking LLM introduced by Google AI in early 2022. The PaLM API and MakerSuite make it fast and easy for developers to use Google’s large language models to build innovative AI applications.&lt;/p&gt;

&lt;p&gt;With a staggering 540 billion parameters, PaLM stands as one of the largest and most powerful language models ever created. Its immense capacity enables PaLM to excel in a wide range of natural language processing (NLP) tasks and allows it to process complex prompts quickly.&lt;/p&gt;

&lt;p&gt;Installing the PaLM API is a straightforward process that involves setting up your environment and accessing the API through a package manager. Here’s a step-by-step guide with Python:&lt;/p&gt;

&lt;p&gt;Get an API key. You can generate one with Google or MakerSuite.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Install using pip
pip install google-generativeai

Import the libraries and configure your API key
import google.generativeai as palm

palm.configure(api_key=API_KEY)

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

&lt;/div&gt;



&lt;p&gt;Using any of the models, you can prompt it to write a short story for your blog&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;model_name=”models/text-bison-001"
prompt=’’Write a short story of a girl and a little dinosaur. 
Summarize it to four paragraphs of 100 words each. ‘’’

completion=palm.generate_text(
 model=model_name,
 prompt=prompt,
 temperature=0.99,
 max_output_tokens=800,
)
print(completion.result)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also install PaLM on Google Cloud Platform (GCP) and use FireBase extensions to summarize text or perform other NLP tasks.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key features of PaLM
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;1. Multilingual Translation:&lt;/strong&gt; PaLM can accurately translate text between a wide range of languages, preserving the nuances and context of the original text. It can also adapt its translation style to different languages, considering cultural and linguistic differences.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Domain-Specific Translation:&lt;/strong&gt; PaLM can be fine-tuned for specific domains, such as legal or medical translation, to improve accuracy and relevance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Reasoning and Inference:&lt;/strong&gt; PaLM can employ chain-of-thought reasoning and inference to understand the context and implications of questions, leading to more accurate and relevant answers.&lt;br&gt;
Knowledge Base Access: PaLM can access and integrate information from external knowledge sources, such as Google Search, to provide more comprehensive and contextual answers.&lt;/p&gt;

&lt;p&gt;**4. Code Optimization: **PaLM can generate code that adheres to established coding style conventions and best practices, making it easier to integrate into existing codebases.It can also optimize generated code for performance and efficiency, reducing computational overhead and resource utilization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Fine-Tuning:&lt;/strong&gt; PaLM can be fine-tuned for specific tasks or domains, tailoring its capabilities to address specialized requirements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Research and Development:&lt;/strong&gt; PaLM is an active area of research, with ongoing efforts to enhance its capabilities and expand its applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. BLOOM API
&lt;/h3&gt;

&lt;p&gt;The Bloom (BigScience Large Open-Science Open-Access Multilingual) Large Language Model API is a powerful tool for natural language processing (NLP) that utilizes a massive, 176-billion-parameter language model called BLOOM. It was developed by BigScience and built on transformers.&lt;/p&gt;

&lt;p&gt;It’s the first major open-source project to authoritatively evaluate and rank human-level performance in a range of languages, with ground-breaking tools for evaluating them.&lt;/p&gt;

&lt;p&gt;Installing the Bloom API (Bloom Large Language Model) involves setting up your environment and accessing the API through Hugging Face. Here’s a step-by-step guide:&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Python 3.7 or higher: Ensure you have Python 3.7 or higher installed on your system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pip package manager: Install the pip package manager if you don’t already have it&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hugging Face Transformers library: Install the Hugging Face - -&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Transformers library using pip. This library provides the necessary tools for interacting with the Bloom API.&lt;br&gt;
pip install transformers&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Import the necessary classes from the Transformers library in your Python script. This will allow you to access the Bloom API functionality.&lt;br&gt;
&lt;code&gt;from transformers import AutoTokenizer, AutoModelForSequenceClassification&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Specify the pre-trained Bloom model you want to use. The Bloom API currently offers two models: Bloom 176B and Bloom 1.5B.&lt;br&gt;
&lt;code&gt;model_name = “bigscience/bloom”&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Download the pre-trained Bloom model from the Hugging Face model hub. This will download the model’s weights and configuration files.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Key Features of Bloom LLM API
&lt;/h4&gt;

&lt;p&gt;The Bloom LLM API stands out for its exceptional capabilities and versatile applications. Here are some of its key features:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Large Model:&lt;/strong&gt; Bloom LLM boasts a massive parameter count of 176 billion, enabling it to capture intricate patterns and nuances in human language.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Multilingual Support:&lt;/strong&gt; Bloom LLM can process and generate text in 46 natural languages, making it suitable for cross-lingual communication and content creation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Creative Text Generation:&lt;/strong&gt;&lt;br&gt;
 Bloom LLM can generate creative text formats, including poems, code, scripts, musical pieces, emails, letters, and more.&lt;br&gt;
Translation Accuracy: Bloom LLM excels at translation, accurately converting text between multiple languages while preserving context and meaning.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Question Answering Prowess:&lt;/strong&gt; Bloom LLM provides comprehensive and informative answers to complex and open-ended questions, even when the questions are ambiguous or have multiple interpretations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Code Generation Versatility:&lt;/strong&gt; Bloom LLM can generate high-quality code in various programming languages, assisting developers in their coding tasks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Summarization Simplicity:&lt;/strong&gt; Bloom LLM effectively summarizes lengthy texts, capturing the essential information while maintaining clarity and coherence.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Code Completion Efficiency:&lt;/strong&gt; Bloom LLM suggests relevant code snippets when provided with partial code, improving coding efficiency and reducing errors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Debugging Assistance:&lt;/strong&gt; Bloom LLM helps programmers identify and resolve errors in their code, facilitating efficient debugging and code optimization.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key features to consider when choosing an LLM API
&lt;/h3&gt;

&lt;p&gt;When choosing an LLM API, there are several key factors to consider, including:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Model size:&lt;/strong&gt; The size of the LLM model is a key factor to consider, as it will affect the model’s capabilities and performance. Larger models are generally more powerful and capable of performing a wider range of tasks, but they can also be slower to run and more expensive to use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task support:&lt;/strong&gt; It is important to choose an LLM API that supports the specific tasks you need it for. Some APIs are specifically designed for certain tasks, such as text generation or translation, while others are more general-purpose.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pricing:&lt;/strong&gt; LLM APIs can vary widely in terms of pricing. Some APIs are free to use, while others charge a fee per request or month. It is important to choose an API that fits your budget.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Documentation and support:&lt;/strong&gt; It is important to choose an LLM with good documentation and support. This will help you when you run into issues.&lt;/p&gt;

</description>
      <category>python</category>
      <category>ai</category>
      <category>machinelearning</category>
      <category>chatgpt</category>
    </item>
    <item>
      <title>Text to speech with Javascript</title>
      <dc:creator>Barri </dc:creator>
      <pubDate>Mon, 05 Feb 2024 00:01:32 +0000</pubDate>
      <link>https://dev.to/barrisam/text-to-speech-with-javascript-2cjk</link>
      <guid>https://dev.to/barrisam/text-to-speech-with-javascript-2cjk</guid>
      <description>&lt;p&gt;Text-to-speech (TTS) is an assistive technology that has gained much popularity over the years. It is also called 'read aloud' technology. This is because TTS converts written words or text into speech by reading them aloud. &lt;/p&gt;

&lt;p&gt;TTS has been incorporated into many websites, apps and digital devices. It is a notable alternative to text, extending the reach of content and audience. It is being used today not just as an alternative for text, but also for educational purposes. &lt;/p&gt;

&lt;p&gt;Though written text still reigns supreme, TTS popularity is largely based on the advantages it offers over static text. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It helps people with reading difficulties&lt;/li&gt;
&lt;li&gt;It is convenient &lt;/li&gt;
&lt;li&gt;It helps people with alternative learning styles&lt;/li&gt;
&lt;li&gt;It is accessible.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How TTS works
&lt;/h2&gt;

&lt;p&gt;Majority of TTS  are inbuilt. The browser, app or software have inbuilt tools to convert text to speech. Example, Google Doc has an accessibility setting where readers can choose to 'Turn on screen reader support'.&lt;/p&gt;

&lt;p&gt;Some TTS are softwares downloaded to a device and enabled on a browser or page where it is needed. This works primarily for pages or apps without an inbuilt TTS.&lt;/p&gt;

&lt;p&gt;TTS are applied in different forms. Some highlight words as they are read. Some have options like start, stop, pause and cancel, giving the reader more control over how it is used. They are also options to change the voices of the reader.&lt;/p&gt;

&lt;p&gt;For the sake of this article, we will look at text-to-speech API on websites using JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why JavaScript?
&lt;/h2&gt;

&lt;p&gt;JavaScript is a modern programming language that extensively covers all web-related technology. It is also called the language of the web. &lt;br&gt;
Javascript, fused with HTML5 has a broad reach of DOMs and APIs that make it easier to write some functionality into a website, including a text-to-speech functionality which uses the Web Speech API.&lt;/p&gt;
&lt;h2&gt;
  
  
  Web Speech API
&lt;/h2&gt;

&lt;p&gt;Web speech API is an API that allows us to incorporate voice data or speech into web apps. It provides two distinct functionalities - SpeechSynthesis (text-to-speech) and Speech Recognition. &lt;/p&gt;

&lt;p&gt;SpeechSynthesis is the synthesizer that allows apps to read text aloud from a device or app. It is the control interface of Web Speech API text-to-speech service.&lt;/p&gt;

&lt;p&gt;Speech recognition is different from text-to-speech because it involves giving voice commands to the application. &lt;/p&gt;
&lt;h2&gt;
  
  
  Getting Started with SpeechSynthesis
&lt;/h2&gt;

&lt;p&gt;The speechsynthesis functionality is a robust controller with properties and methods that regulates how text is converted to speech. To convert text-to-speech, we only need to create an instance of the speechsynthesisutterance class and configure it with the properties and methods attached to it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let speech = new SpeechSynthesisUtterance();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Speechsynthesis has six properties, they include, &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Language:&lt;/strong&gt; This gets and sets the language of the utterance.&lt;br&gt;
&lt;strong&gt;Pitch:&lt;/strong&gt; Sets the pitch at which the utterance will be spoken at. It ranges from 0 - 2. 0 being the lowest and 2 being the highest. We can adjust it using a slider.&lt;br&gt;
&lt;strong&gt;Rate:&lt;/strong&gt; Sets the rate at which the utterance will be spoken at. The rate ranges from 0.1 to 10, with 0.1 being the lowest and 10 being the highest. Visually, we can set it using a slider.&lt;br&gt;
&lt;strong&gt;Volume:&lt;/strong&gt; Sets the volume at which the utterance will be spoken. The volume is a range from 0 to 1. 0 being lowest and 1 being the highest. We will set it visually using a slider.&lt;br&gt;
&lt;strong&gt;Text:&lt;/strong&gt; Gets and sets the text that will be synthesized.&lt;br&gt;
&lt;strong&gt;Voice:&lt;/strong&gt; Sets the voice that will be spoken.&lt;/p&gt;

&lt;p&gt;It takes methods like &lt;br&gt;
.cancel(): This is like a stop. It removes all the utterances from the utterance queue.&lt;br&gt;
.getvoices(): This gets the voices available on the Web Speech API synthesizer.&lt;br&gt;
.pause(): This pauses an utterance&lt;br&gt;
.resume(): This is fired when an utterance is paused.&lt;br&gt;
.speak(): This reads an utterance aloud.&lt;/p&gt;

&lt;p&gt;To simply convert a text to speech&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
let speaknow = new SpeechSynthesisUtterance('Hello world!');
window.speechSynthesis.speak(speaknow);

&amp;lt;/script&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;To check for browser support, since not all browsers support the API&lt;br&gt;
&lt;/p&gt;

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

   &amp;lt;button onclick="play()"&amp;gt;Play&amp;lt;/button&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;


&amp;lt;script
 function play() {
      if ('speechSynthesis' in window) {
        let working = new SpeechSynthesisUtterance("This is working");
        window.speechSynthesis.speak(working);
      }
   else{
     document.write("Browser not supported")
   }
    }
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we will create a simple demo with HTML, CSS and JS to show how Web Speech API can be implemented in browsers and websites.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8" /&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;title&amp;gt;Web API TTS&amp;lt;/title&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1"&amp;gt;

&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;div&amp;gt;
  &amp;lt;h3&amp;gt; Select Voices &amp;lt;/h3&amp;gt;
  &amp;lt;select id = "voices" &amp;gt;
    &amp;lt;option&amp;gt; option 1 &amp;lt;/option&amp;gt;
  &amp;lt;/select&amp;gt;
  &amp;lt;/div&amp;gt;

  &amp;lt;div id = "vpr"&amp;gt;
    &amp;lt;h5&amp;gt; Volume &amp;lt;/h5&amp;gt;
  &amp;lt;input type="range" min="0" max="1" value="0.5" step="0.1" id="volume" /&amp;gt;
        &amp;lt;span id="vol-label"&amp;gt;1&amp;lt;/span&amp;gt;

    &amp;lt;h5&amp;gt; Rate &amp;lt;/h5&amp;gt;
  &amp;lt;input type="range" min="0" max="10" value="0.5" step="0.1" id="rate" /&amp;gt;
        &amp;lt;span id="rate-lab"&amp;gt;1&amp;lt;/span&amp;gt;

    &amp;lt;h5&amp;gt; Pitch &amp;lt;/h5&amp;gt;
  &amp;lt;input type="range" min="0" max="2" value="1" step="0.1" id="pitch" /&amp;gt;
        &amp;lt;span id="pitch-lab"&amp;gt;0.5&amp;lt;/span&amp;gt;
  &amp;lt;/div&amp;gt;

  &amp;lt;textarea rows = "9" cols = "60" name = "description" id = "lines"&amp;gt;
            Enter text here...
         &amp;lt;/textarea&amp;gt;&amp;lt;br&amp;gt;

    &amp;lt;button class = "buttons" style = "background: green;" id = "speak"&amp;gt; Speak &amp;lt;/button&amp;gt;
    &amp;lt;button class = "buttons" style = "background: orange" id = "pause"&amp;gt; Pause &amp;lt;/button&amp;gt;
    &amp;lt;button class = "buttons" style = "background: lightgreen" id = 'resume'&amp;gt;Resume &amp;lt;/button&amp;gt;
    &amp;lt;button class = "buttons" style = "background: red" id = 'cancel'&amp;gt; Cancel &amp;lt;/button&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  CSS
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;html, body{
    height: 100%
  }

select{
  padding: 3px;
  margin: 10px 0;
}

#vpr {
  display:inline-block;
  padding: 30px 10px;
}

.buttons{
  display: inline-block;
  padding: 0.6em 1.5em;
  margin: 0 0.3em 0.3em 0;
  border-radius: 5px;
  box-sizing: border-box;
  font-family: 'Roboto', sans-serif;
  font-weight: 400;
  font-size: 14px;
  color: black;
  text-align: center;

}

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// First we initialize new SpeechSynthesisUtterance object
let tts = new SpeechSynthesisUtterance();

// Setting the Speech Language
tts.lang = "en";

//Populating the select dropdown with the list of available voices on Web Speech API

let speechvoices = []; // global array of available voices

window.speechSynthesis.onvoiceschanged = () =&amp;gt; {
  // To get the list of voices using getVoices() function
  speechvoices = window.speechSynthesis.getVoices();

  // We need to populate the section and set the first voice
  tts.voice = speechvoices[0];

  let select_voice = document.getElementById("voices");
  speechvoices.forEach((voice, i) =&amp;gt; (select_voice.options[i] = new Option(voice.name, i)));
};

//SETTING THE CONTROLS - SPEAK, PLAY, PAUSE AND RESUME

//SPEAK
//first we get the value of the textarea or document
document.getElementById("speak").addEventListener("click", () =&amp;gt; {
  tts.text = document.getElementById("lines").value;

  //then we implement the speechsynthesis instance
  window.speechSynthesis.speak(tts);
});


//PAUSE
document.getElementById("pause").addEventListener("click", () =&amp;gt; {
  // Pause the speechSynthesis instance
  window.speechSynthesis.pause();
});

//RESUME
document.getElementById("resume").addEventListener("click", () =&amp;gt; {
  // Resume the paused speechSynthesis instance
  window.speechSynthesis.resume();

  });

//CANCEL
document.querySelector("cancel").addEventListener("click", () =&amp;gt; {
 // Cancel the speechSynthesis instance
  window.speechSynthesis.cancel();
});

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//TO SET THE VOLUME, PITCH, AND RATE

 //Volume 

  //We get the volume value from the input
  document.getDocumentById("volume").addEventListener("input", () =&amp;gt; {
  const vol = document.getDocumentById("volume").value;

  // Set volume property of the SpeechSynthesisUtterance instance
  tts.volume = vol;

  // Updating the volume label
  document.querySelector("#vol-label").innerHTML = vol;
});


  //RATE
   // We get the rate Value from the input
document.getDocumentById("rate").addEventListener("input", () =&amp;gt; {
  const rate = document.getDocumentById("rate").value;

  // Set rate property of the SpeechSynthesisUtterance instance
  tts.rate = rate;

  // Updating the rate label
  document.getDocumentById("rate-lab").innerHTML = rate;
});


//PITCH
  // We get the pitch Value from the input
document.getElementById("pitch").addEventListener("input", () =&amp;gt; {

  const pitch = document.getElementById("pitch").value;

  // Setting thepitch property of the SpeechSynthesisUtterance instance
  tts.pitch = pitch;
  // Updating the pitch label
  document.getDocumentById("pitch-lab").innerHTML = pitch;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although we have populated the voices in the drop down, they won't change to the selected voice unless we use the onchange function to target that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// This changes the voice of the speaker or utterance to the selected voice
document.getDocumentById("voices").addEventListener("change", () =&amp;gt; {
  tts.voice = voices[document.getDocumentById("voices").value];
});

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Browser Compatibility
&lt;/h2&gt;

&lt;p&gt;Web API SpeechSynthesis enjoys full support of Chrome, Edge, Firefox, Opera and Safari. Internet Explorer does not support this API. The &lt;code&gt;onvoiceschanged()&lt;/code&gt; method is the only method not supported by Safari and Opera.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Helpful Javascript Oneliners to Make Your Code Cleaner</title>
      <dc:creator>Barri </dc:creator>
      <pubDate>Wed, 31 Jan 2024 22:24:50 +0000</pubDate>
      <link>https://dev.to/barrisam/helpful-javascript-oneliners-to-make-your-code-cleaner-2n7o</link>
      <guid>https://dev.to/barrisam/helpful-javascript-oneliners-to-make-your-code-cleaner-2n7o</guid>
      <description>&lt;p&gt;Javascript is unarguably one of the most popular programming languages in the world today. It is home to vanilla programming, multiple frameworks, complex apis, and a wide range of libraries, modules and functions. &lt;/p&gt;

&lt;p&gt;Whether you are a newbie or a more experienced developer, using one-liners is a cool way to do more things with Javascript. In this article, we will be looking at over 12 Javascript one-liners that will make your code cleaner. They are simple, easy to remember, and show proficiency in using methods.&lt;/p&gt;

&lt;p&gt;Lets go!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Eligibility to vote&lt;/strong&gt;&lt;br&gt;
This function checks if a citizen is eligible to vote. The minimum age of voting is set to 18. It uses a function called ternary operator.&lt;br&gt;
// To check the eligibility to vote&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let age = 25;
let votingelig = (age &amp;gt;= 18) ? "You are eligible to vote." : "You are not eligible to vote yet";

document.write(votingelig);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;To get the last occurrence of a value&lt;/strong&gt;&lt;br&gt;
We can get the last occurance of a string using lastIndexOf() method to locate/search that specific value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let lastoccurance = 'Jerusalem'.lastIndexOf('e');

document.write(lastoccurance);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Getting the domain name from an email&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here, we use substring() and IndexOf() methods to extract a substring from a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let getdomain = 'codecodablog@gmail.com'.substring('codecodablog@gmail.com'.indexOf('@') + 1);

document.write(getdomain);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;No Fives&lt;/strong&gt;&lt;br&gt;
This program aims to get the count of numbers within a certain range but will not count any number with a 5 in it. For example, the range of numbers between 4 and 16 will return 11 because 5 and 15 were omitted. The result though can have a five.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Getting the count of a range of numbers but no fives
nofives = (a, b) =&amp;gt; Array.from(Array(b - a + 1), (b, i) =&amp;gt; a + i).filter(b =&amp;gt; !/5/.test(b)).length;


document.write(nofives(12,49))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Find the length of the shortest word&lt;/strong&gt;&lt;br&gt;
Given a string of text, this program will return the length of the shortest word.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//To get the length of the shortest word in the text
const findShortestword = (s) =&amp;gt; s.split(" ").sort((a, b) =&amp;gt; b.length - a.length).pop().length;

document.write(findShortestword("Bad situations cannot last"))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Checking a number to see if it is positive, negative or zero&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here, we will check if a number is positive, negative or zero&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// program to check if number is positive, negative or zero
let d = -10;
let result = (d &amp;gt;= 0) ? (d == 0 ? "zero" : "positive") : "negative";
document.write(`Number is ${result}.`);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Getting the square root of numbers&lt;/strong&gt;&lt;br&gt;
To get the square root of a set of numbers, we use the map() and Math() inbuilt functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//simple calculations to get the square root of a set of numbers
let sqroot = [9, 36, 49].map(Math.sqrt);
document.write(sqroot);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The sum of numbers&lt;/strong&gt;&lt;br&gt;
To calculate the sum of numbers in a given array we use the reduce() function. The reduce() function like it's name implies reduces an array to a single value. Its return value is stored in the form of totals.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//calculating the sum of the numbers
const Sum = nos =&amp;gt; nos.reduce((a,b) =&amp;gt; a + b, 0)
document.write(Sum([8, 34, 6, 29, 2, 1]))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;To get minimum value&lt;/strong&gt;&lt;br&gt;
This is just the opposite of getting the maximum value. In this case, we want the minimum value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//min number
const minnum = nos =&amp;gt; Math.min(...nos);

document.write(minnum([5, 10, 15, 20, 25, 30, 35]))

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Calculating a random number from a range of numbers&lt;/strong&gt;&lt;br&gt;
Using Math() function, here we get a random number from a range of numbers&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//calculating a random number from a range of numbers

let a = Math.floor(Math.random() * (45 - 35 + 1)) + 35
document.write(a)

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Converting a string to an array&lt;/strong&gt;&lt;br&gt;
To convert a string to an array, we use the split() method. split() divides a string into an array of substrings. Split() accepts a separator as one of it's parameters to determine where each split should occur. &lt;/p&gt;

&lt;p&gt;If the separator is not given, the split returns the entire string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//converting a string to an array
let str = 'This is a string'.split(' ');

document.write(str);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Executing a function on every element of an array&lt;/strong&gt;&lt;br&gt;
To perform a function on every element of an array, we will use the forEach() method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let letters = ['X', 'Y', 'Z'];

letters.forEach(function (e) {
    document.write(e);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Multiple Variable Assignment&lt;/strong&gt;&lt;br&gt;
Javascript is flexible enough to allow us reassign variables and this can be done in a single line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//multiple assignment
let [w,x,y,z] = [30,84,28,"BABY"]
document.write(w, x, y, z)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding of inbuilt functions play a large role in writing shorter codes. &lt;/p&gt;

&lt;p&gt;Arrow functions, introduced in ES6, is also a fun way of writing shorter functions. They take only one statement that returns a value. Brackets, the function and return keyword are also not needed. This makes them the perfect syntax for one-liners. &lt;/p&gt;

&lt;p&gt;Ternary operators also cut down the overuse of 'if' in conditional statements, allowing simple logical statements to be written in just one line. &lt;/p&gt;

&lt;p&gt;To become a pro at writing one-liners, it is important to check these out and understand them. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>code</category>
    </item>
    <item>
      <title>Javascript Promise</title>
      <dc:creator>Barri </dc:creator>
      <pubDate>Tue, 30 Jan 2024 22:23:51 +0000</pubDate>
      <link>https://dev.to/barrisam/javascript-promise-3g6o</link>
      <guid>https://dev.to/barrisam/javascript-promise-3g6o</guid>
      <description>&lt;p&gt;Javascript Promise is one of the features that was rolled out in ES6 in 2015. Promises in Javascript operate within the same premise as a promise in real life. As a person can make a promise, so can Javascript.&lt;/p&gt;

&lt;p&gt;For example, I could say, "I promise to learn Javascript Promise this year". &lt;/p&gt;

&lt;p&gt;Now promises aren't just made. When we make promises, we strive to fulfil them and sometimes we fail. If I learn Javascript this year, then I will keep my promise. If I don't, then I failed to keep that promise. &lt;/p&gt;

&lt;p&gt;So a promise ends up either being fulfilled and kept or unfulfilled and failed. These terms are translated in Javascript as resolved or rejected. A fulfilled promise is a resolved one, while a failed one is a rejected promise.&lt;/p&gt;

&lt;h2&gt;
  
  
  Callbacks and Asynchronous actions
&lt;/h2&gt;

&lt;p&gt;Before we delve fully into promise, we will look at callbacks and asynchronous programming. Why do we need to know about callbacks and asynchronous programming? Because they are tied to promises and promises are one of the ways to deal with them.&lt;/p&gt;

&lt;p&gt;A callback function is a function that is written as an argument and passed to another function (the outer function), and invoked there. The passed argument can be executed by the outer function immediately or later. Javascript supports callbacks and implements them in subroutines, lambda expressions, blocks or function pointers. &lt;/p&gt;

&lt;p&gt;There are two types of callbacks named based on how they control data flow at runtime. They are synchronous callbacks which are the default callbacks or deferred callbacks, also called asynchronous callbacks. &lt;/p&gt;

&lt;p&gt;Synchronous callbacks are the callbacks that are executed immediately while asynchronous callbacks are executed later. &lt;/p&gt;

&lt;p&gt;Asynchronous in Javascript refers to two events or threads happening at the same time, simultaneously. Not waiting for the previous one to complete. In asynchronous programming, a program can make a request from another software or server and do other things in the background while it waits for a reply.&lt;/p&gt;

&lt;h2&gt;
  
  
  Javascript Promise and why it is important.
&lt;/h2&gt;

&lt;p&gt;A Javascript promise is an asynchronous process that produces an unknown value at a later time. According to MDN "A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason."&lt;/p&gt;

&lt;p&gt;Javascript is single threaded. This means two or more scripts cannot run at the same time. They always have to run one after the other. This makes programming with it slow, time consuming and monotonous. Once a network sends a request, it can do nothing else until it receives a response. Only one request is sent at a time. &lt;/p&gt;

&lt;p&gt;Objects and events in programming try to emulate human behaviour and Javascript's single threading does not replicate human behaviour. Humans are multi-threaded. We perform different actions simultaneously and asynchronously. A human can talk and cook at the same time. We can type with both hands. &lt;/p&gt;

&lt;p&gt;Promises, callbacks and asynchronous programming are a great way to perform multi-thread actions. Multiple requests and time-consuming actions can be performed simultaneously without blocking the main thread. &lt;/p&gt;

&lt;h2&gt;
  
  
  Features of Javascript Promise
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;1. A promise is asynchronous in nature.&lt;/li&gt;
&lt;li&gt;2. It has two default callbacks - reject and resolve.&lt;/li&gt;
&lt;li&gt;3. The output, value or result is unknown at the time of creation.&lt;/li&gt;
&lt;li&gt;4. It is a good way to overcome the issues that arise from simple and nested callbacks.&lt;/li&gt;
&lt;li&gt;5. The result of a promise is immutable. It's state cannot be changed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Syntax of a Javascript promise
&lt;/h2&gt;

&lt;p&gt;A promise object is created by using the new keyword followed by the keyword - Promise. It takes  function and one one argument. This argument takes two default callbacks - reject and resolve.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var dosomething = new Promise(function(resolve, reject) {
 // code goes here

 if (/* expression goes here */) {
   resolve("Yes, this works!");
 }
 else {
   reject("Oh No! It didn't work");
 }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Resolved holds the output for a successful promise while reject holds the output for an unsuccessful promise.&lt;/p&gt;

&lt;h2&gt;
  
  
  .then() and .catch() handlers
&lt;/h2&gt;

&lt;p&gt;.then() and .catch() are two two handlers attached to callbacks in promises. .then() is attached to a resolved promise while .catch() is attached to a rejected promise.&lt;br&gt;
Let us look at demos on how to use promises and their handlers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
&amp;lt;script&amp;gt;
let man = new Promise(function(resolve, reject){

    man_full = true;
    if(man_full)
     resolve()
     else
     reject()

});
   man.then(function(){
       document.write("Stop Eating!")
   }).catch(function(){
       document.write("You stop eat more!")
   })

   //output: Stop Eating
   //if we change the value of man_full to false, it becomes 'You should eat more'
&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Promises work with arrow functions which were also implemented in ES6. In the next demo, we will use an arrow function with parameters to give custom outputs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
&amp;lt;script&amp;gt;
let sum = new Promise((resolve, reject) =&amp;gt;{
    let add = 24 + 24;
    if (add == 49){
        resolve('Correct Answer!')
    }
    else {
        reject('Incorrect!')
    };
});

sum.then((custom_msg) =&amp;gt;{
    document.write("This is a "+ custom_msg)
}).catch((custom_msg) =&amp;gt; {
    document.write("This is " + custom_msg)
});

//the custom msg with resolve callback is appended to the message for .then()
//the custom msg with reject callback is appended to the message for .catch()
        &amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Static methods of promises
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Promise.all()&lt;/strong&gt;&lt;br&gt;
This is a promise API that executes two or more promises in parallel. For example, if we have to download certain files all at once, it will process them and give a value once they are all resolved. &lt;br&gt;
It takes an array of promises and returns a new promise. The new promise is the value&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
&amp;lt;script&amp;gt;
const download_one = new Promise((resolve, reject) =&amp;gt;{
    setTimeout(() =&amp;gt; {
        resolve('First download completed');
    }, 5000);
})


const download_two = new Promise((resolve, reject) =&amp;gt;{
    setTimeout(() =&amp;gt; {
        resolve('Second download completed');
    }, 3000);
})
const download_three = new Promise((resolve, reject) =&amp;gt;{
    setTimeout(() =&amp;gt; {
        resolve('Third download completed');
    }, 1000);
})

Promise.all([
     download_one,
     download_two,
     download_three
]).then((msgs) =&amp;gt; {
     document.write(msgs);
}).catch((msgs) =&amp;gt; {
     document.write(msgs);
})

&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It takes about 5 seconds for the output to display since the longest time is 5 seconds for the first download. The order of the output appears the same way as they are written even though the first download takes the longest to process. &lt;/p&gt;

&lt;p&gt;This shows just how promise.all() works, executing and processing actions in parallel. If for any reason any of the promises listed is failed or rejected, promise.all() rejects everything and the error message is displayed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const download_one = new Promise((resolve, reject) =&amp;gt;{
    setTimeout(() =&amp;gt; {
        resolve('First download completed');
    }, 5000);
})

const download_two = new Promise((resolve, reject) =&amp;gt;{
    setTimeout(() =&amp;gt; {
        reject(new Error('Error with second download'));
    }, 3000);
})

const download_three = new Promise((resolve, reject) =&amp;gt;{
    setTimeout(() =&amp;gt; {
        resolve('Third download completed');
    }, 1000);
})

Promise.all([
     download_one,
     download_two,
     download_three
]).then((msgs) =&amp;gt; {
     document.write(msgs);
}).catch((msgs) =&amp;gt; {
     document.write(msgs);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Promise.allSettled()&lt;/strong&gt;&lt;br&gt;
Since promise.all() takes an 'all or nothing' approach where it rejects the entire list if one promise is rejected, promise.allSettled() takes a more relaxed approach. It insteads shows the status of each promise according to the value or result.&lt;br&gt;
Fulfilled promise = result&lt;br&gt;
Rejected promise = error&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
&amp;lt;script&amp;gt;
const download_one = new Promise((resolve, reject) =&amp;gt;{
    setTimeout(() =&amp;gt; {
        resolve('First download completed');
    }, 5000);
})

const download_two = new Promise((resolve, reject) =&amp;gt;{
    setTimeout(() =&amp;gt; {
        reject(new Error('Error with second download'));
    }, 3000);
})

const download_three = new Promise((resolve, reject) =&amp;gt;{
    setTimeout(() =&amp;gt; {
        resolve('Third download completed');
    }, 1000);
})

Promise.allSettled([
     download_one,
     download_two,
     download_three
]).then((msgs) =&amp;gt; {
     console.log(msgs);
}).catch((msgs) =&amp;gt; {
     console.log(msgs);
})

/*
Output
0:{status: "fulfilled", value: "First download completed"}
1: {status: "rejected", reason: Error: Error with second download}
2: {status: "fulfilled", value: "Third download completed"}
*/
&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Promise.race()&lt;/strong&gt;&lt;br&gt;
promise.race() returns an output once one of the promises is completed. Unlike promise.all(), it doesn't wait for the entire promises listed to be completed before it returns a value. It doesn't matter if that promise is a fulfilled or rejected promise.&lt;br&gt;
&lt;/p&gt;

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

const download_one = new Promise((resolve, reject) =&amp;gt;{
    setTimeout(() =&amp;gt; {
        resolve('First download completed');
    }, 5000);
})

const download_two = new Promise((resolve, reject) =&amp;gt;{
    setTimeout(() =&amp;gt; {
        reject(new Error('Error with second download'));
    }, 3000);
})

const download_three = new Promise((resolve, reject) =&amp;gt;{
    setTimeout(() =&amp;gt; {
        resolve('Third download completed');
    }, 1000);
})

Promise.race([
     download_two,
     download_one,
     download_three
]).then((msgs) =&amp;gt; {
     console.log(msgs);
}).catch((msgs) =&amp;gt; {
     console.log(msgs);
})

//OUTPUT
//Third download completed
&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output says 'Third download completed', because the value for the third download is displayed first, since it has the shortest settimeout.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Promise.any()&lt;/strong&gt;&lt;br&gt;
Promise.any() behaves similarly to promise.race(), but it picks the first fulfilled or resolved promise to display.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
&amp;lt;script&amp;gt;
const download_one = new Promise((resolve, reject) =&amp;gt;{
    setTimeout(() =&amp;gt; {
        resolve('First download completed');
    }, 5000);
})

&amp;lt;body&amp;gt;
&amp;lt;script&amp;gt;
const download_two = new Promise((resolve, reject) =&amp;gt;{
    setTimeout(() =&amp;gt; {
        reject(new Error('Error with second download'));
    }, 1000);
})

const download_three = new Promise((resolve, reject) =&amp;gt;{
    setTimeout(() =&amp;gt; {
        resolve('Third download completed');
    }, 3000);
})

Promise.any([
     download_two,
     download_one,
     download_three
]).then((msgs) =&amp;gt; {
     console.log(msgs);
}).catch((msgs) =&amp;gt; {
     console.log(msgs);
})

//OUTPUT
//Third download completed

&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second download was reset to have the shortest timeout of 1 second. But because it is a failed promise, it wasn't acknowledged and the second shortest - the third download was picked.&lt;/p&gt;

&lt;h2&gt;
  
  
  States of a Promise
&lt;/h2&gt;

&lt;p&gt;A promise has four states or behavior&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pending:
The pending state of a promise is the waiting state of the promise. This is the state when we neither have a resolved nor rejected promise.&lt;/li&gt;
&lt;li&gt;Fulfilled: 
This is an asynchronous operation that was completed successfully. It returned a value or a fulfilled promise.&lt;/li&gt;
&lt;li&gt;Rejected:
An asynchronous operation was not successful. It returned an error and a rejected promise.&lt;/li&gt;
&lt;li&gt;Settled:
This is a processed promise that has been rejected or fulfilled. It is the final outcome of an asynchronous promise operation. A settled promise is immutable.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Using promise with asynchronous operations has many benefits. It flattens the code, makes it more readable and maintainable. It also helps to handle asynchronous operations better, much better than callbacks. &lt;br&gt;
Promise is a modern object and it works well with modern browsers, but if browser compatibility issues arise, these could be overridden with polyfills.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to Style Streamlit Metrics in Custom CSS</title>
      <dc:creator>Barri </dc:creator>
      <pubDate>Sat, 18 Mar 2023 08:22:02 +0000</pubDate>
      <link>https://dev.to/barrisam/how-to-style-streamlit-metrics-in-custom-css-4h14</link>
      <guid>https://dev.to/barrisam/how-to-style-streamlit-metrics-in-custom-css-4h14</guid>
      <description>&lt;p&gt;Steamlit has established itself as an interactive, open-source framework for visualising and displaying charts, graphs, plots and ML models in a web app. Like its predecessor and rival, Dash, it plays a big role in analytics, AI, and ML visualization — areas where traditional BI dashboards cannot reach.&lt;/p&gt;

&lt;p&gt;There are many advantages to using streamlit for data science, machine learning and visualisation. I find streamlit very intuitive and I like its simplicity and ease of use. The learning curve is short, it provides valuable components called widgets for easy arrangement and ordering of data, but my personal favourite is that everything is done in pure Python and deployed as a webapp. Streamlit framework supports all major libraries associated with data science and data visualization. This has made it a personal favourite of many and has pushed its fast rise in a short time.&lt;/p&gt;

&lt;p&gt;Unlike Dash, Streamlit is limited in how it can be styled within the framework of the app. This can be frustrating for users who want to work with colors, and different styles to grab the attention of the audience. Using the markdown functionality of streamlit, I’ll be walking us through how we can include an external CSS file to style a metric widget.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you’ll need
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A working streamlit application&lt;/li&gt;
&lt;li&gt;Knowledge of basic CSS&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Metric Widget
&lt;/h2&gt;

&lt;p&gt;The metric widget is an &lt;a href="https://docs.streamlit.io/library/api-reference/data/st.metric" rel="noopener noreferrer"&gt;API reference&lt;/a&gt; that displays KPIs and quantitative figures, like numbers in a card-like format. It takes five parameters — label, value, delta, delta_color and help.&lt;/p&gt;

&lt;p&gt;The label is the title for the metric. The value is the value of the metric. This is the text/number that will be displayed boldly. The delta is the indicator of how the metric has changed and is displayed with an arrow below the value. The delta_color is the colour of the delta and help is an optional tooltip displayed next to the label.&lt;/p&gt;

&lt;p&gt;Streamlit's official API documentation for the metric widget has this code as their use case.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;col1, col2, col3 = st.columns(3)&lt;br&gt;
col1.metric(“Temperature”, “70 °F”, “1.2 °F”) """Temperature is the label, 70 °F is the value and 1.2 °F is the delta."""&lt;br&gt;
col2.metric(“Wind”, “9 mph”, “-8%”)&lt;br&gt;
col3.metric(“Humidity”, “86%”, “4%”)&lt;br&gt;
"""A neat combination of st.columns with metrics."""&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;The output is the image above. It places the three metrics side by side, but there is no telling where each metric card starts or ends. All figures and their delta are on a bland white background.&lt;/p&gt;

&lt;h2&gt;
  
  
  Analyzing the data
&lt;/h2&gt;

&lt;p&gt;To create our metric, I’ll be using the &lt;a href="https://www.kaggle.com/datasets/timmate/avocado-prices-2020" rel="noopener noreferrer"&gt;Avocado Prices (2020) dataset&lt;/a&gt; from Kaggle. We will analyze it to determine what our metric values will be. In this case, I want the total sales of each year (2015–2020) and the percentage increase in sales between 2015–2020 to be displayed as a metric.&lt;/p&gt;

&lt;p&gt;To get each year, I subsetted the year column and filtered it by the different years. Each year has its own variable — the value of which is rounded to one decimal place. This variable will be the value of our metric.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import pandas as pd&lt;br&gt;
import streamlit as st&lt;br&gt;
import plotly.express as px&lt;br&gt;
st.header(“Avocado Interactive Sales Visualization with Streamlit”)&lt;br&gt;
df = pd.read_csv(“avocado-updated-2020.csv”)&lt;br&gt;
"""PANDAS ANALYSIS"""&lt;br&gt;
df.drop_duplicates(inplace = True) """drop duplicates"""&lt;br&gt;
st.write(“””Total Bags of Avocados Sold Through 2015–2020“””)&lt;br&gt;
""" to calculate the total number of bags sold each year"""&lt;br&gt;
"""2015"""&lt;br&gt;
yr_15 = round(df[df[‘year’] == 2015][‘total_bags’].sum())&lt;br&gt;
"""2016"""&lt;br&gt;
yr_16 = round(df[df[‘year’] == 2016][‘total_bags’].sum())&lt;br&gt;
"""2017"""&lt;br&gt;
yr_17 = round(df[df[‘year’] == 2017][‘total_bags’].sum())&lt;br&gt;
"""2018"""&lt;br&gt;
yr_18 = round(df[df[‘year’] == 2018][‘total_bags’].sum())&lt;br&gt;
"""2019"""&lt;br&gt;
yr_19 = round(df[df[‘year’] == 2019][‘total_bags’].sum())&lt;br&gt;
"""2020"""&lt;br&gt;
yr_20 = round(df[df[‘year’] == 2020][‘total_bags’].sum())&lt;br&gt;
"""calculate percentage increase between 2015 and 2020"""&lt;br&gt;
per_increase = round((int(yr_20 — yr_15)/ yr_15) * 100, 1)&lt;br&gt;
percent_increase = f”{per_increase}%”&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  St.Markdown and Unsafe HTML
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create a new file and name it using the .css extension. For this project, our CSS sheet is called ‘style.css’.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Within our python file, we need to create a path that links to the external CSS file. We will use st.markdown to link to the file and override Streamlit’s default position not to allow HTML code in their application.&lt;/p&gt;

&lt;p&gt;Streamlit is concerned about developers deploying unsafe apps if HTML is allowed in the code. The idea is that users should always be able to trust Streamlit applications, and with HTML in the code, applications become vulnerable and susceptible to hacks. To turn it on, we will switch the unsafe_allow_html to become True.&lt;/p&gt;

&lt;p&gt;The Markdown code plays an important role here. It houses the HTML code that reads the CSS styling and also turns on the HTML.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"""LINK TO THE CSS FILE"""&lt;br&gt;
with open(‘style.css’)as f:&lt;br&gt;
 st.markdown(f”&amp;lt;style&amp;gt;{f.read()}&amp;lt;/style&amp;gt;”, unsafe_allow_html = True)&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the metrics
&lt;/h2&gt;

&lt;p&gt;I created two different sets of columns. The first set takes four metrics and the second takes three.&lt;/p&gt;

&lt;p&gt;The delta in this analysis is the percentage difference between the previous year and the current year.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"""WE CREATE FOUR COLUMNS HERE TO HOLD THE METRIC"""&lt;br&gt;
col1, col2, col3, col4 = st.columns(4)&lt;br&gt;
col1.metric(label = “2015 Sales”,&lt;br&gt;
 value = (yr_15),&lt;br&gt;
)&lt;br&gt;
col2.metric(label = “2016 Sales”,&lt;br&gt;
 value = (yr_16),&lt;br&gt;
 delta = round(float((yr_16 — yr_15)/yr_15) *100, 1)&lt;br&gt;
)&lt;br&gt;
col3.metric(label = “2017 Sales”,&lt;br&gt;
 value = (yr_17),&lt;br&gt;
 delta = round(float((yr_17 — yr_16)/yr_16) *100)&lt;br&gt;
)&lt;br&gt;
col4.metric(label = “2018 Sales”,&lt;br&gt;
 value = (yr_18),&lt;br&gt;
 delta = round(float((yr_18 — yr_17)/yr_17) *100,1)&lt;br&gt;
)&lt;br&gt;
st.markdown(“&amp;lt;hr/&amp;gt;”, unsafe_allow_html = True)&lt;br&gt;
col5, col6, col7 = st.columns(3)&lt;br&gt;
col5.metric(label = “2019 Sales”,&lt;br&gt;
 value = (yr_19),&lt;br&gt;
 delta = round(float((yr_19 — yr_18)/yr_18) *100, 1)&lt;br&gt;
)&lt;br&gt;
col6.metric(label = “2020 Sales”,&lt;br&gt;
 value = (yr_20),&lt;br&gt;
 delta = round(float((yr_20 — yr_19)/yr_19) *100)&lt;br&gt;
)&lt;br&gt;
col7.metric(label = “% increase between 2015–2020”,&lt;br&gt;
 value = (percent_increase)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is what the dashboard looks like.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Styling the CSS
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Inspect the web app page where your streamlit application is running and extract the id of the metric columns. They usually don’t change so you can use the one in the CSS code of this tutorial.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;NB: The id for three columns is different from that of the four columns. If your page has multiple columns of the same number, one id affects them all.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example, if you have a set of three columns to hold three metrics and another three columns to hold different plots; when you inspect it, you’ll find they both have the same CSS id. Any CSS styling done to the column with metrics will affect the column with plots.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/* use this code inside your css file */&lt;br&gt;
div.css-12w0qpk.e1tzin5v2{&lt;br&gt;
 background-color: #f5f5f5;&lt;br&gt;
 border: 2px solid;&lt;br&gt;
 padding: 20px 20px 20px 20px;&lt;br&gt;
 border-radius: 10px;&lt;br&gt;
 color: #ffc300;&lt;br&gt;
 box-shadow: 10px;&lt;br&gt;
}&lt;br&gt;
div.css-1r6slb0.e1tzin5v2{&lt;br&gt;
 background-color: #f5f5f5;&lt;br&gt;
 border: 2px solid; /* #900c3f */&lt;br&gt;
 border-radius: 10px;&lt;br&gt;
 padding: 20px 20px 20px 70px;&lt;br&gt;
 color: green;&lt;br&gt;
}&lt;br&gt;
div.css-50ug3q.e16fv1kl3{&lt;br&gt;
 font-weight: 900;&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Our CSS code changes the background color, highlights the borders around each metric value, and changes the font color, font size of the text and paddings in each border. In the end, we have a dashboard that looks like this:&lt;/p&gt;

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

&lt;p&gt;The app is deployed &lt;a href="https://barrisam-streamlit-css-styling-avocado-sales-sg3un4.streamlit.app/" rel="noopener noreferrer"&gt;here&lt;/a&gt; on Streamlit Cloud.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
There are many ways you can style metrics on your dashboards. One fun way would be to make them circular. This would look good on short figures or text. You should also check out Streamlit's full documentation &lt;a href="https://docs.streamlit.io/" rel="noopener noreferrer"&gt;here&lt;/a&gt; on how to use metrics and markdowns.&lt;/p&gt;

</description>
      <category>streamlit</category>
      <category>visualization</category>
      <category>python</category>
      <category>analytics</category>
    </item>
    <item>
      <title>How not to remain in the learning phase</title>
      <dc:creator>Barri </dc:creator>
      <pubDate>Mon, 28 Sep 2020 20:42:01 +0000</pubDate>
      <link>https://dev.to/barrisam/how-not-to-remain-in-the-learning-phase-n4a</link>
      <guid>https://dev.to/barrisam/how-not-to-remain-in-the-learning-phase-n4a</guid>
      <description>&lt;p&gt;When i first started learning how to code, i took tutorials from Bucky Roberts on Youtube, then i went to w3school, freecodecamp, udemy and andela - learning the same stuff. &lt;/p&gt;

&lt;p&gt;At first i told myself it was because i really had to understand what i was doing, but i was stuck in the learning phase. I wanted to learn everything and never felt good enough to try out things on my own. Most importantly, i didn't know when to stop learning. I feel much better now that i have broken free from that cycle, but i also feel concerned about other newbies who might be stuck in the same predicament. &lt;/p&gt;

&lt;p&gt;So, when did you know it was time to put yourself out there? When did you actively stop learning?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>writing</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Understanding Python's Dictionary and Data Structure</title>
      <dc:creator>Barri </dc:creator>
      <pubDate>Thu, 14 Nov 2019 18:12:07 +0000</pubDate>
      <link>https://dev.to/barrisam/understanding-python-s-dictionary-and-data-structure-1bn9</link>
      <guid>https://dev.to/barrisam/understanding-python-s-dictionary-and-data-structure-1bn9</guid>
      <description>&lt;p&gt;Dictionaries are one of python's powerful datatype and yet they are quite underrated. Focus is placed more on Lists as they behave like arrays. Unlike lists, dictionaries are enclosed in curly braces - {} - and the values are unordered and changeable. Dictionaries make use of keys and values. The index is the key. It is unique and immutable (meaning it must either be a string, tuple or integer). The values are more flexible and can be any datatype. The key to its associated value is called a key-value pair.&lt;/p&gt;

&lt;p&gt;The keys and It's associated value is separated by a semicolon. As an example, we'll write a dictionary that maps the value of countries and their capitals.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;countries = {"USA": "Washington", "Nigeria" : "Abuja", "Croatia": "Zagreb", "Ghana" : "Capetown"}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;The dictionary keys here are USA, Nigeria, Croatia, and Ghana, which happens to be the name of the countries. While Washington, Abuja, Zagreb, and Capetown are the values and are capitals of the respective countries.&lt;/p&gt;

&lt;h4&gt;
  
  
  Accessing data in a dictionary
&lt;/h4&gt;

&lt;p&gt;To print out the entire dictionary&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(countries)&lt;/code&gt;&lt;br&gt;&lt;br&gt;
&lt;code&gt;#{"USA": "Washington", "Nigeria" : "Abuja", "Croatia": "Zagreb", "Ghana" : "Capetown"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To access particular or specific information, we use the variable name with the key inside square brackets.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;countries['Nigeria']&lt;/code&gt;&lt;br&gt;
&lt;code&gt;#Abuja&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This prints 'Abuja' as output, which is the capital of Nigeria is Abuja&lt;br&gt;
Note that the keys are case sensitive. countries['nigeria'] will return an error while countries['Nigeria'] will return 'Abuja'.&lt;/p&gt;
&lt;h4&gt;
  
  
  Updating a dictionary
&lt;/h4&gt;

&lt;p&gt;Let's say we made a mistake, just like we did since Capetown is not the capital of Ghana (haha, you didn't know that, did you?) and we need to update and correct this information. To change the value of Ghana,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;countries['Ghana'] = 'Accra'&lt;br&gt;
print(countries)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;#{"USA": "Washington", "Nigeria" : "Abuja", "Croatia": "Zagreb", "Ghana" : "Accra"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You will notice the value of Ghana has been modified to Accra.&lt;br&gt;
We can also add a new country to the list by doing the same thing we did with Ghana, only this time, the key would be the name of the new country we are to add to the list.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;countries['Japan'] = 'Tokyo'&lt;br&gt;
print(countries)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;#notice that japan has been added to the list.&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Deleting a dictionary
&lt;/h4&gt;

&lt;p&gt;You can remove targeted keys from a dictionary. Note that you can't delete a key without deleting its associated value. They both go together. You can also delete the entire dictionary using the del keyword.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;del countries&lt;/code&gt; &lt;br&gt;
&lt;code&gt;#this will delete the entire countries dictionary. Don't use this unless you are absolutely sure you won't need the dictionary ever.&lt;/code&gt;&lt;br&gt;
&lt;code&gt;del countries['Ghana']&lt;/code&gt;&lt;br&gt;
&lt;code&gt;# will delete Ghana and it's value, Accra.&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Using get()
&lt;/h4&gt;

&lt;p&gt;The method get() returns a value for the given key. If the key is not available it returns None or an optional fallback or default value.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print('Capital of Nigeria is', countries.get('Nigeria'))&lt;/code&gt;&lt;br&gt;
&lt;code&gt;#Capital of Nigeria is Abuja&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The output returns Abuja because Nigeria is on the dictionary collection. If we were to get the capital of a country like Zambia, the output would be&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print('Capital of Zambia is', countries.get('Zambia'))&lt;/code&gt;&lt;br&gt;
&lt;code&gt;#Capital of Zambia is None&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is because Zambia is not on the list. We can also set an optional value to be returned if a key is not found.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;('Capital of Zambia is', countries.get('Zambia', 'notfound'))&lt;/code&gt;&lt;br&gt;
&lt;code&gt;#Capital of Zambia is notfound&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Using the 'in'  keyword for dictionaries
&lt;/h4&gt;

&lt;p&gt;Like lists, we can loop through dictionaries using the 'in' operator keyword.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for x in countries:&lt;br&gt;
     print(x)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;#USA, Nigeria, Croatia and Japan&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This returns only the keys of the dictionary. To return the values as well, we will use the value() function&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for x in countries.values():&lt;br&gt;
    print(x)&lt;br&gt;
or&lt;br&gt;
for x in countries:&lt;br&gt;
   print((countries[x]))&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;#this returns all the values of the keys&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To get the keys only, we can use the keys() function&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for x in countries.keys():&lt;br&gt;
    print(x)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;To get both keys and values mapped together, we will use the items() function&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for x in countries.items():&lt;br&gt;
    print(x)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# ('USA', 'Washington')&lt;br&gt;
  ('Nigeria', 'Abuja')&lt;br&gt;
  ('Croatia', 'Zagreb')&lt;br&gt;
  ('Japan', 'Tokyo')&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Nested Dictionaries
&lt;/h4&gt;

&lt;p&gt;Dictionary values can be of any data type. It can hold lists and even other dictionaries. In this part, I'll show you how nested dictionaries are accessed.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;countries = {'USA' : 'Washington', 'Nigeria' : 'Abuja', 'Croatia' : 'Zagreb', 'Japan' : 'Tokyo', 'SouthAfrica' : {"Johannesburg":['Pretoria', 'Soweto', 3]}}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;I added another dictionary with key as 'Johannesburg' which contains a list of 3 items. This shows the dynamicity of dictionaries. To print out Soweto from this list, &lt;/p&gt;

&lt;p&gt;&lt;code&gt;countries['SouthAfrica']['Johannesburg'][1]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;[1] is the index number of Soweto from the list.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Python dictionaries have methods and functions that I could not cover here. You can check out&lt;br&gt;
&lt;a href="https://www.programiz.com/python-programming/methods/dictionary/"&gt;https://www.programiz.com/python-programming/methods/dictionary/&lt;/a&gt; to read more on it.&lt;/p&gt;

</description>
      <category>python</category>
      <category>software</category>
      <category>dictionary</category>
      <category>development</category>
    </item>
  </channel>
</rss>
