top
Sort by :

Polymer And PWA: Understanding The “Why” — Part 1

In this article series, we will understand-A brief history of the evolution of webThe drawbacks of the current state of web platform?How web components proposes to solve those issues?How Polymer fairs as a web component library and a lightweight frontend framework?The out of the box support for PWA and other performance best practices polymer providesYou can easily get started with progressive web app polymers by understanding all the above concepts clearly.The creation of HTML & JSHTML was created to share documents over the network. It supports three features mainly, i.e. display text, images, and interlink with other HTML documents. Everything was pre-created and servers distributed only static HTML pages. Below is the image of a typical browser from the 1990s.Then they wanted to programmatically control HTML and add behaviors to HTML and browser. Hence Javascript was created.Evolution of browsersWeb servers started to serve dynamic content unlike the static contents before. This lead to HTML spec evolution. Browsers started sending data to servers via forms and then AJAX. Browsers got more powerful as Javascript spec progressed. We got to a stage called WEB 2.0 where we started doing complex business logic processing at frontend. Patterns for development emerged and grew into libraries and frameworks. This has emerged polymer as an important library for building a progressive web app from scratch.Framework WarsThere were a lot of competing frameworks to enable the complex frontend apps. Each had their own set of philosophies and build tools. This was essentially the web performance race with every new framework/version improving upon its predecessors. But, creating progressive web apps with polymer supports modern enhanced web platform.Where is the web platform today?We are cluttered and fragmented with frameworks. Each one has a twisted philosophy of performance and has laborious (automated yes, but try tweaking them) build process. All of this is a workaround on how the platform is today.What did we lose?Interoperability: We can’t use components built in different frameworks together with ease. Imagine if you are a big organization and your departments would have a lot of similar looking UI with slight modifications. Now, each department would choose their own framework to build the UI. As an organization, we can’t leverage the work of other teams.Simplicity: We are all aware of the term javascript fatigue. There is so much churn in the frontend world each day that we have to make ourselves familiar with so many things. Each framework enforces its own abstractions of concepts and architecture that your skills are not transferable. You need to be a seasoned engineer to switch between frameworks and write near-perfect code. Others would have a steep learning curve and might take time picking up best practices.Things we didn’t anticipateWhen the frameworks came out they were mainly run on desktops and laptops. No one thought about the mobile web. Most frameworks are too heavy to run on mobile. The parse, compile, execute and render would put a huge load on the mobile CPU. But the computational power on mobile is limited. They don’t have big heatsinks like desktops. Without a heatsink, there would be an upper limit on the processing power of a mobile CPU.Mutation of HTML                                                                       This is a screenshot of Youtube webpage HTML codeIn the Image above we can see how a typical HTML looks like for any website today. I call that built with <div> soup.                                                                                                          Div SoupIt is common to see code like <div class=”tree-control”> where the behavior of tree control is forced on to a scaffolding HTML element div.                                                                                                   Forced BehaviorHTML moved away from original purpose and capabilities. HTML was not meant for Web Apps. All it was supposed to do was have the ability to display documents over the network and link to other documents (https://www.w3.org/History/1989/proposal.html). So the frameworks that exist today are a workaround on the way the platform evolved and trying to do things which were not supported by the original vision.Time to redesign the HTML/Web                                                                 This is a screenshot of Youtube written with web componentsWhat if we create a element <tree-control> instead of writing <div class=”tree-control”>. That would be an easy and natural to understand. It puts the information where it belongs and preserves the semantics.But <tree-control> is not a default HTML element. How would browser parse it? Well, web components are the answer to that question.Web ComponentsWeb components are based on four main specifications that introduces you to progressive web app polymers.Custom ElementsThe Custom Elements specification lays the foundation for designing and using new types of DOM elements.Shadow DOMThe shadow DOM specification defines how to use encapsulated style and markup in web components.HTML importsThe HTML imports specification defines the inclusion and reuse of HTML documents in other HTML documents.HTML TemplateThe HTML template element specification defines how to declare fragments of markup that go unused at page load, but can be instantiated later on at runtime.( Intro on web components borrowed from https://www.webcomponents.org/introduction )In simple terms, web component allows you to define your own HTML tags, enables you to define blocks of markup with the ability to inject dynamic content, gives you the ability to scope/encapsulate markup and styles in a separate DOM tree and provides a way to include and reuse HTML documents in other HTML documents.                                                        You might say, hey!! popular frameworks already do something like that!You might say, hey!! popular frameworks already do something like that! So let me take a look at the diagram below to understand the differenceYour typical frameworks operate at the Javascript level, which means when you insert your template into the page it parses the HTML, identifies if a definition for <tree-control> is present. if so it takes control of it and handles its lifecycle and its DOM activities. All of this is not native. So there is only so much performance optimization it can give us.Whereas when you use web components we tell the browser what to do when it encounters a <tree-control> tag. We describe its life cycle to the browser. The browser is informed of the DOM manipulations. So what happens here is that browser is in control. It runs on a native layer which means that it will be faster than what frameworks do. This gives your web pages a default win.Hope this Progressive web app polymers tutorial helped you to understand the basics of Polymer and PWA.In the next part of the article, we shall explore Polymer, a javascript library that helps you create custom reusable HTML web components and all benefits that come with it.
Rated 4.0/5 based on 30 customer reviews
Polymer And PWA: Understanding The “Why” — Part 1 125 Polymer And PWA: Understanding The “Why” — Part 1 Blog
Ashrith Kulai 20 Apr 2018
In this article series, we will understand-A brief history of the evolution of webThe drawbacks of the current state of web platform?How web components proposes to solve those issues?How Polymer fairs...
Continue reading

Building Machine Learning Model is fun using Orange

Introduction:Data has become a powerful source of earning and predict future and people will seek to utilize it even if they don’t know exactly how. Machine learning will become a usual part of programmer’s resume, data scientists will be as common as accountants. Nowadays and for the next approximately two decades, we will continue to see a major need for machine learning and data science specialists to help apply machine learning technologies to application areas where they aren't applied today.Nowadays people prefer GUI based tools instead of more coding stuff. Orange is one of the popular open source machine learning and data visualization tool for beginners. People who don’t know more about coding and willing to visualize pattern and other stuff can easily work with Orange.Why Orange:Orange is an open-source software package released under GPL that powers Python scripts with its rich compilation of mining and machine learning algorithms for data pre-processing, classification, modeling, regression, clustering and other miscellaneous functions.Orange also comes with a visual programming environment and its workbench consists of tools for importing data, dragging and dropping widgets, and links to connect different widgets for completing the workflow.Orange uses common Python open-source libraries for scientific computing, such as numpy, scipy, and scikit-learn, while its graphical user interface operates within the cross-platform Qt framework.Getting Started:Here is how to get started with data visualization in orange.Download Orange=> Go to https://orange.biolab.si and click on Download.For Linux Users:AnacondaIf you are using python provided by Anaconda distribution, you are almost ready to go. If not, follow these steps to download Orange:conda config --add channels conda-forgeand runconda install orange3 conda install -c defaults pyqt=5 qtPIPOrange can also be installed from the Python Package Index. You may need additional system packages provided by your distribution.pip install orange3Run shortcode to verify your setup Orange successfully. Open your Python Terminal and run the following code :>>> import Orange >>> Orange.version.version '3.2.dev0+8907f35' >>>Note: If You find result shown above then you successfully setup Orange. In case you get an error like this :from Orange.data import _variable ImportError: cannot import name '_variable'Kindly Follow These Steps: Install Orange From SourceMac and Windows user can easily setup orange in their system step by step just by following the official docs of Orange:Official docs for setup OrangeAfter installation let's start working with OrangeVisualization of DataSet in Orange:A primary goal of data visualization is to communicate information clearly and efficiently via statistical graphics, plots and information graphics.The Main goal of data visualization is to communicate information clearly and effectively through graphical means. It doesn't mean that data visualization needs to look boring to be functional or extremely sophisticated to look beautiful. To convey ideas effectively, both aesthetic form and functionality need to go hand in hand, providing insights into a rather sparse and complex data set by communicating its key-aspects in a more intuitive way. Yet designers often fail to achieve a balance between form and function, creating gorgeous data visualizations which fail to serve their main purpose — to communicate information.” -Friedman (2008)Open Orange on your system & create your own new Workflow:After you clicked on “New” in the above step, this is what you should have come up with:In this tutorial, we are going to see the steps for Visualization of DataSet in orange:Step 1: Without data, there is no existence of Machine Learning. So, In our first step we import our dataset and in this tutorial, I use example dataset available in Orange Directory. We import zoo.tab dataset in file widget:Step 2: In next step, we need data tables to view our dataset. For this, we use Data Table widget.When we double-click on the data table widget we can visualize our data in actual format.Step 3: This is the last step where we will understand our data, with the help of visualization. Orange make visualization pretty much easier. We just add one more widget and choose which format we would like to visualize our data like Scatter Plot.This completely works on the concept of neurons, data transfer from one layer to another layer when we connect data table to scatter plot widget then we find an actual representation of our data in the form of scatter plot.Closing Note:Orange is the most powerful tool used for almost any kind of analysis and visualizing dataset is fun using Orange. The default installation includes a number of machine learning, preprocessing and data visualization algorithms in 6 widget sets (data, visualize, classify, regression, evaluate and unsupervised). Additional functionalities are available as add-ons (bioinformatics, data fusion and, text-mining).Hope, this tutorial helps you to understand how to visualize data set using orange. It is very important to understand the flow of data, this helps you to figure out problems easily.Keep Practice with OrangeHappy Coding
Rated 4.0/5 based on 29 customer reviews
Building Machine Learning Model is fun using Orange

Building Machine Learning Model is fun using Orange

Blog
Introduction:Data has become a powerful source of earning and predict future and people will seek to utilize it even if they don’t know exactly how. Machine learning will become a usual part of prog...
Continue reading

Unleash the Power of Django CMS

IntroductionDjango is a powerful Python-based web framework, that allows developers to get a web application up and running quickly. It's amazing for building web applications, with large amounts of data, but it lacks the basic features of a content management system. Django CMS is an add-on to Django that provides this functionality. This Django CMS tutorial for beginners will help you to learn how to unleash Django CMS power that helps to build an easy-to-use, easy-to-maintain website.1. Installing and Configuring Django CMS1.1 Why Django CMS?Django is great for data-driven web applications. It makes it very simple for e-commerce sites with large product catalogs or news site that publishes multiple articles daily. But, Django lacks the ability to create standalone pages from the admin system such as for smaller website with just a few pages of content about their services which needs to be updated every once in a while. This isn't convenient for a business owner, they need to call up their developer every time something needs to be changed. There are a lot of CMS options like WordPress. It's easy for non-developers to get a site up and running. The problems arise when you want to customize how your content is laid out on your page. It’s great if you're writing a blog post, but on a static page, there may be specific areas of content you'd like to edit independently of any other. You may accomplish this with WordPress by writing shortcodes but these are often complex and confusing to create. So,"Why Django CMS?". We ’ll discuss this and the main principles behind Django CMS with the help of a real scenario by developing a website for friend’s pizza restaurant.Some of the content on his site are his menu items and his daily signature specials. This has been a pain point for him as he wishes to edit his menus every few weeks and his daily specials on a daily basis. Both of these pieces of content currently reside in the same large content area. In order to preserve the layout of the page, HTML code has to be placed inside the content area. What he'd really like is a way to edit his menus and his daily specials independently from one another and not risk breaking his homepage. Here's where we finally get to know why we would use Django CMS over others, it allows us to custom-build individual content areas that are completely independent of one another. We can define the type and the quantity of content that can be placed in these areas, it significantly reduces user error. Django CMS integrates seamlessly with Django's existing functionality so you'll continue to get the fantastic data processing it already provides.1.2 Installing Django CMSSo now let's get started with Django CMS and create a new site. The first thing we need to do is set up our development environment. We'll be using a package called virtualenv to set up a virtual environment for our project, making it isolated from any other projects that we may be working on. If you don't already have virtualenv on your machine, you can install it using PIP.Let's create a directory for our project and navigate to that now.Let's name our project rafs_pizza. Navigate inside the directory we've just created, then create your virtual environment.This will give a clean environment to start with. Next, we're going to activate the environment. We do this by typingThe name of the environment now shows up next to our user information, which indicates that we're operating under that virtual environment.The creators of Django CMS provided an installation tool that greatly speeds up the process of getting all of Django CMS's dependencies installed. It also sets up our Django project for us. After the virtual environment is activated. Then type in PIP install djangocms-installer. Let's quickly make another directory for our actual Django project to go in. This way the virtual environment isn't inside our Django project.Once you're navigated inside, we can run the installer.The installer will run through a series of options for setting up and configuring our project. Once you've gone through all the installer options, it will create your project for you. It will ask you to enter a username, email, and password for your admin user. You can make these whatever you'd like. Once it's finished, you can run the Django server, open up a browser, and navigate to 127.0.0.1:8000 and the project will be up and running and now you're ready to start developing.2. Page Templates and Placeholders2.1 Login and Page CreationThe Django CMS installer created a starter page for us that we use to log in to the admin system. You can also log in from any page by putting a ?edit at the end of the URL. This will bring up the toolbar and you can log in from there. This toolbar is available on all pages when logged in and provides useful links to the various parts of the system.After logging in, you'll see an Installation successful message. Below that, you should see a box that says, “Add your first page”. So, we'll add a new page now. Here, you can enter a title and content for your page and now we've got a page with which we can start working with.2.2 Putting Content into PlaceholdersThere are two views in edit mode, Structure, and Content. Content will show you a preview of how the page will look like with your edits. The structure shows you all the places where you can edit the content on your page. These are the visual representation of the page's content placeholders. You can add, edit, and delete content plugins from here.2.3 Creating a Page Template with Content PlaceholdersLet’s start developing by opening up the Django Project Directory in any code editor.Locate the templates folder under rafaels_pizza. The base.html file is the master file for our project. It's the one that all the other templates are going to be extending. The other three templates are generic layout templates that Django CMS provided by default.Now we're going to create a template for our homepage i.e. home.html. This file must be in this same template folder. Now, we replicate one of the templates files into our home.html. There would be two main pieces of content that we would like to have i.e. daily specials and menus. We're going to create content placeholders for those two pieces of content. We then need to register our template in the settings.py file for our Django Project. Then, when we go to create a new page, our template should show up under the Templates drop-down. Open the Django Projects Settings file and search for cms_templates. Here, we see the entries for the default layout templates. On the left is the template file name, and on the right, is the template name that should appear in the template drop-down. Similarly, we type in home.html, Home. After saving this file, switch over to the admin system in our browser.base.htmlhome.html settings.py2.4 Assign a Template to a PageAfter refreshing, navigate to Page->Templates, and you should see your new homepage template in the list. Click on it to assign this template to this page.Clicking on Structure in the right-hand of the toolbar will show us those two new placeholders that we created, our Daily_Specials, and Menus. We may add any content to these placeholders using the plugin. To add some text, use the Text Plugin, for pictures the Picture Plugin and so on. Then after publishing these changes we see our content on the page. These two pieces of content are able to be edited individually from one another, editing the Daily Specials will have no bearing on the Menus.2.5 Static PlaceholderWe might want some content that is going to be consistent throughout the entire site, say the logo at the header and some information in the footer and also want them to be editable in future. For these pieces of content, Django CMS provides what's called a static placeholder. These placeholders are visible and edited from every page of the website and we add them in base.html. Making a static placeholder is really similar to making a regular placeholder, we just simply put static_placeholder instead of just placeholder.Let's add the static_placeholder for logo and footer. The footer placeholder goes right underneath the content block.base.htmlSave the file and then head back over to the browser. Refresh the browser and we should now see those placeholders showing up in the structure view of the page. The little pin icon next to it indicates that it's a static placeholder and is available on every page of the site.We can put a logo inside the logo static placeholder using a Picture Plugin and then add some information in the footer. So, it doesn't take much to get your content into the Django CMS system. Just a matter of a couple of lines of code for your placeholders and you're up and running.3. Create Content Plugins3.1 Building a PluginNow we'll be designing and building content plugins to go inside the placeholders like we created earlier.By better defining the type of content that can go in each placeholder, and designing that content's look and feel, we'll be able to further enhance our website.Let's create a content plugin that's optimized for displaying this kind of content. Open up a terminal window and start a new Django app. Type in-Now, under setting.py find INSTALLED_APPS and add rafs_pizza_plugins to the bottom of the list.settings.pyOpen up the models.py file inside the rafs_pizza_plugins app. Here, we'll define the data structure for our plugin the same way we would any other model in Django. The difference here, however, it won't be extending the models.model class like we normally would. Instead, we'll need to import the CMS plugin class. Then, we will configure the plugin's metadata by the class Meta and we'll want to define a verbose name of DailySpecial, and then verbose_name_plural of Daily Specials. Lastly, we'll add the Unicode function so our daily special instances are more user-friendly when displayed in a list and save the file.models.pyThen we'll head back to the terminal and type in This will create a migration file for our app. Once it's done creating the file, type in This will go ahead and create our tables in the database.In a normal Django app, we have a views file for processing our web requests. In our content plugins app, the views file is called CMS plugins.py. Rename the views.py to cms_plugins.py, and open the file. Remove the default code and import CMSPluginBase and plugin_pool. Then, import the models we just created. Now, we'll create a class for our plugin, class Daily_Specials_Plugin, and this will be extending from CMSPluginBase.We also define a render function that will build our context dictionary with the data we want to pass to the template. With this, we're going to be updating our context object with the user inputted data. So we'll do context.update.Then the last thing we need to do in this file is registering the plugin. So, we're going to do plugin_pool.register_plugin, and then the name of our class, which is Daily_Specials_Plugin and save the file, Now we're going to create that template file that we referenced. So, let's create a templates folder inside rafs_pizza_plugins, and inside that, we're going to create a file called daily_special.html. Similarly, we can do for our plugin i.e. menu_items.cms_plugins.pydaily_special.htmlFinally, if we want to limit the list of plugins available in the placeholder drop-down we can do that using the CMS_PLACEHOLDER_CONF in the settings.py. It can be also used to restrict the number of times a plugin can be used with the help of global key in the limits dictionary.3.2 Styling Page and PluginsSome CSS styling will spruce up our website. Inside the static directory lets create a CSS directory and under this create a main.css file.  Now, let's link up this CSS file with our base.html template like the way that the Django system requires us to by using the static template tag i.e. by adding the link tag in the base.htmlmain.cssConclusionHopefully, from this Django CMS documentation, you have experienced that it's an easy framework to jump into especially if you already know how Django works. It has the ability to create fully functional websites and get up and running quickly. Now, you should be able to take what you've learned and used it to explore Django CMS further.
Rated 4.0/5 based on 23 customer reviews
Unleash the Power of Django CMS

Unleash the Power of Django CMS

Blog
IntroductionDjango is a powerful Python-based web framework, that allows developers to get a web application up and running quickly. It's amazing for building web applications, with large amounts of d...
Continue reading

Understanding The Essential Blocks of Artificial Intelligence - Neural Networks

Artificial Intelligence, Machine Learning,  Deep Learning, and Neural Networks are all buzzwords right now. This article is the first among the series of planned articles which focuses on understanding and implementing the concepts of deep learning and artificial intelligence.“Artificial Intelligence is the new electricity. Similar to how electricity revolutionized a number of industries about hundred years ago, Artificial Intelligence will transform and revolutionize different industries” - Andrew NGArtificial Intelligence and Deep Learning are becoming one of the most important components of modern day businesses. A large number of smart and intelligent systems are regularly built to solve the use cases that were earlier thought to be complex to solve. Some examples include automatic speech recognition in smartphones, conversation chatbots, image classification and clustering in search engines, natural language generation, and understanding.At a broader level, Artificial Intelligence and Deep Learning aim to create intelligent machines. However, at a much deeper level, it comprises of Mathematical relationships, sophisticated optimization algorithms, and models that generate intelligence in the form of predictions, segmentation, clustering, forecasting, classifications etc.Neural Networks are the building blocks of every Artificial Intelligence and Deep Learning models. In this artificial intelligence neural network tutorial, we will understand everything about the neural networks and the science behind them.Contents:Introduction of Neural NetworksSingle Processing Units - NeuronsActivation FunctionsForward PropagationBackward PropagationIntroduction of Artificial Neural Network terminologyA neural network is a mathematical model that is designed to function similar to biological neurons and nervous system. These models are used to recognize complex patterns and relationships that exist within a labeled data. A labeled dataset contains two types of variables - predictors (some features which are used as independent variables in the model) and target (some features which are treated as dependent variables of the model).  Employees data containing their information such as age, gender, experience, skills and labeled with their salary amount (an example of numerical data)Tweets labeled as positive, negative or neutral (an example of text data)Images of Animals labeled with Name of the Animal (an example of image data)Audios of Music labeled with the Genere of the Music (an example of audio data)The core structure of a Neural Network model is comprised of a large number of simple processing nodes which are interconnected and organized in different layers. An individual node in a layer is connected to several other nodes in the previous and the next layer. The inputs from one layer are received and processed to generate the output which is passed on to the next layer.The first layer of this architecture is often named as input layer which accepts the inputs, the last layer is named as the output layer which produces the output and every other layer between input and output layer is named as hidden layers. Let us now understand how to apply artificial neural network and how it works.Single Processing Units - NeuronA neuron is the smallest unit in a neural network. It accepts various inputs, applies a function and computes the output.Each incoming connection corresponds to different inputs to a node, To each of its connections, the node assigns a number known as a “weight”. The weight of every input variable signifies the importance or the priority of that variable among all other variables. For different values of input variables, the node multiplies the value with its associated weight and adds the resulting products together. It also adds another term called “bias” which helps the learning function to adjust to left or right. This summed number is then passed through an activation function (described in next section) which maps the inputs to the target output values.Let’s understand this through an example. Take a situation in which you want to purchase a new house and you will make the decision based on the following factors in order of priority.Cost of the PropertySquare Feet AreaConstruction YearAvailability of Security SystemsNearby AmenitiesClimate Factors in the LocalityCrime Rate in the LocalityThe best way to formalize the decision making based on this situation is to formulate a mathematical equation with:Every factor is represented as x1, x2, x3, …Every factor’s priority is represented as the weight: w1, w2, w3Node Input is represented as the weighted sum of factors and their weights (Z)Node Output is represented as the value of mapping function g (Z)Inputs: x1,x2, x3, …Weights: w1, w2, w3, …Node Bias term: “b”Node Input (Z) = w1*x1 + w2*x2 + w3*x3 + w4*x4 + … + w7*x7 + bNode Output (A) = g (Z)Here the function “g” is known as Activation Function. Let’s understand how this activation function works.Activation Functions - Applying Non-Linear TransformationsThe main goal of an activation function is to apply a non-linear transformation on the input to map it to the output. For example, a linear combination of 7 variables related to the house is mapped into two target output classes: “Buy the Property” and “Do not buy the Property”.The decision boundary of the output can be given by a threshold value. If the generated value is below a threshold value, the node outputs 0, otherwise 1. The generated outputs (0, 1) belongs to the decision. In our example, A is the generated output (Activation), and “b” is the threshold. If weighted sum of inputs and their weights comes out to be greater than “b” then you will buy the property otherwise not. i.e.If A > 0, then interpretation = “buy house”else A < 0, then interpretation =  “do not buy”WX + b > 0, 1(“buy house”)WX + b < 0, 0(“do not buy”)This is the basic equation of the activation function which is applied to each neuron. In the above example, we have applied Step Function as the activation function.There are other choices of nonlinear activation functions such as relu, sigmoid, tanh: Forward PropagationNeural Network model goes through the process called forward propagation in which it forward propagates the inputs and their activations through the layers to get the final output. The computation steps involved are:Z = W*X + bA = g(Z)g is the activation functionA is the predicted output using the input variables X.W is the weight matrixB is the bias matrixTo optimize the weights and bias matrices used in the neural network, the model computes the error in the output and makes small changes in the values. These changes are made so that the overall error can be reduced. This process of error computation and weights optimization is represented in functions called loss function and cost function.Error Computation: Loss Function and Cost FunctionLoss function measures the error in the prediction and the actual value. One simplistic loss function is the difference between actual value and the predicted value.Loss = Y - AY = Actual Value, A = Predicted ValueCost function computes the summation of loss function values for every training data example.Cost Function =  Summation (Loss Function)Minimizing the Cost Function - Backpropagation using Gradient DescentThe final step is to minimize the error term (cost function) and obtain the optimal values of weights and bias terms. A neural net model does this through a process called backpropagation using gradient descent.In this process, it computes the error of the final layer and back passes it to the previous layer. The previous layer associated weights and biases are adjusted to tackle the error. The values of weights and bias are updated using the process called gradient descent. In this algorithm, the derivative of error in the final layer is computed with respect to each weight. This error derivative is then used to find the derivative of weights and bias which are then subtracted from the original values to get the updated new values.Then, the model again forward propagate to compute the new error with new weights and then will backward propagate to update the weights again. The process is repeated several times to achieve the minimum error term. This process is also termed as training. During training, the weights and thresholds are continually adjusted until training data with the same labels consistently yield similar outputs.RecapTo train a neural network model, following steps are implemented.Design the architecture of a neural network model with a number of layers, number of neurons in each layer, activation functions etc.For every training example in the input data, compute the activations using activation functions.Forward propagate the activations through the input layers to hidden layers to the output layerCompute the error of the final layer using Loss FunctionCompute the sum of errors for every training example in the input data using Cost FunctionBackpropagate the error to the previous layers and compute the derivative of error with respect to weights and bias parameters.Using Gradient Descent algorithm subtracts the weight and bias derivative terms from the original values.Perform this operation for a large number of iterations (epochs) to obtain a stable neural network model.In this article, we discussed the overall working of an AI neural network model. In the next article, we will discuss how to implement a neural network in Python. Feel free to share your comments.
Rated 4.0/5 based on 24 customer reviews
Understanding The Essential Blocks of Artificial Intelligence - Neural Networks

Understanding The Essential Blocks of Artificial Intelligence - Neural Networks

Blog
Artificial Intelligence, Machine Learning,  Deep Learning, and Neural Networks are all buzzwords right now. This article is the first among the series of planned articles which focuses on understandi...
Continue reading

10 steps for Getting started GUI`s with python

IntroductionPython programming language is used as a basis for a lot of tools and packages for creating a flexible, usable and reliable desktop application with Graphical User Interface - GUI. These applications can be used for the comfortable launching of Your personal code, functionality, visualizing, management and monitoring of Your tasks.Check out this step by step guide to learn how to build a GUI app in Python from scratch.Step # 1Defining the current task. The very first step in creating a GUI app in Python is what will be solved and estimated as result of the application? The answer to this question will define your next step. Field of the usage is very wide. Data visualizations of the scientific valuations and results, personal applications performance to working with texts, images, additional functions, business automation GUI`s for developing systems and managing tasks, and monitoring. Home use is also fun. Preliminary estimation of the size and functionality of the application is necessary because it is important for choosing GUI toolkit which will fit for these tasks. Gaining a basic understanding of the Python programming language is crucial for any developer to build a desktop application with GUI.Step # 2Second step —Choose a right package for GUI creation and play with GUIs using Python. There exist a number of Python-based packages to do this. As a part of Python standard library -  Tkinter gives you a possibility to create small, simple GUI applications. Most popular Third party related packages: PyQt, PySide, WxPython, Kivy.PyQt - most useful for desktop creation python bindings for the Qt (C++ based) application development framework.PySide - python binding of the cross-platform GUI toolkit Qt. WxPython - the cross-platform GUI toolkit for the Python.Kivy - open source Python library for developing mobile apps and other multitouch application software with a natural user interface.Step # 3We chose a PyQt5 as GUI toolkit for our desktop application. The next step - download and install this package. The best and recommended way is - install Anaconda, powerful and wide functional toolset for using a lot of python related packages.Download link: https://www.anaconda.com/download/and install with instructions. Another way to install PyQt5 - with pip(You must have python 2.x or 3.x installed).Python link: https://www.python.org/downloads/pip install pyqt5Step # 4If installed — we create a file pyqt_app1.py, opening this file with the favorite editor. Importing PyQt5 modules. Creating PyqtApp class. In the _init_function of our class setting window Icon and window title. At the bottom of this file, we create and import instruction of the file with if _name+ == “_main”: and typing lines with importing sys module, calling pyqt base app, calling show() to starting our GUI application.from PyQt5 import QtWidgets, QtGui, QtCore class PyQtApp(QtWidgets.QWidget):         def __init__(self, parent=None):         QtWidgets.QWidget.__init__(self, parent)         self.setWindowTitle("PyQt Application")         self.setWindowIcon(QtGui.QIcon("Your/image/file.png")) if __name__ == "__main__":     import sys     app = QtWidgets.QApplication(sys.argv)     myapp = PyQtApp()     myapp.show()     sys.exit(app.exec_())Step # 5We need to add some style, set size, and position to our application. Let's do this by adding some code. At first set stylesheet in the _init_ function of the main class. Next, set window opacity in the starting file “if” instruction and adding size in both parts of our code. You can change the color of the text and background-color of the window and elements with Your favorite rgba, hex or named color. Just do this with changing the line: self.element.setStyleSheet(“background-color: #hex number or rgba();color: #hex number or rgba(); ”) or self.setStyleSheet(“background-color: #hex number or rgba(); color: #hex number or rgba(); ”) if window styling. setStyleSheet() supporting many styling tags such as width, height, color, background-color, borders, padding, margin and other. All available tags in the Qt documentation. For positioning of the window, we need to get a desktop resolution. Desktop variable describes our desktop parameters. In the main class, we are setting size related to desktop resolution. And we add “else” to the bottom of the file in starting instruction. It makes our code more flexible, for example, if we use this code with other GUI application.from PyQt5 import QtWidgets, QtGui, QtCore class PyQtApp(QtWidgets.QWidget):         def __init__(self, parent=None):         QtWidgets.QWidget.__init__(self, parent)         self.setWindowTitle("PyQt Application")         self.setWindowIcon(QtGui.QIcon("Your/image/file.png"))         self.setMinimumWidth(resolution.width() / 3)         self.setMinimumHeight(resolution.height() / 1.5)         self.setStyleSheet("QWidget {background-color:                            rgba(0,41,59,255);}                            QScrollBar:horizontal {                            width: 1px; height: 1px;                            background-color: rgba(0,41,59,255);}                              QScrollBar:vertical {width: 1px;                            height: 1px;                            background-color: rgba(0,41,59,255);}") if __name__ == "__main__":     import sys     app = QtWidgets.QApplication(sys.argv)     desktop = QtWidgets.QApplication.desktop()     resolution = desktop.availableGeometry()     myapp = PyQtApp()     myapp.setWindowOpacity(0.95)     myapp.show()     myapp.move(resolution.center() - myapp.rect().center())     sys.exit(app.exec_()) else:     desktop = QtWidgets.QApplication.desktop()     resolution = desktop.availableGeometry() Step # 6Let’s add some functionality to our GUI application. The main goal of creating applications with a graphical interface is to build some comfortable interface for using and solving some tasks. We can add buttons, fields, frames, graphics and other things which we want to see on the couch. Buttons and text field would be good for getting effective results. For the best view of the buttons, we create a new class that will describe styling and behavior and set Font.from PyQt5 import QtWidgets, QtGui, QtCore font_but = QtGui.QFont() font_but.setFamily("Segoe UI Symbol") font_but.setPointSize(10) font_but.setWeight(95) class PushBut1(QtWidgets.QPushButton):         def __init__(self, parent=None):         super(PushBut1, self).__init__(parent)         self.setMouseTracking(True)         self.setStyleSheet("margin: 1px; padding: 7px;                            background-color: rgba(1,255,0,100);                            color: rgba(1,140,0,100);                            border-style: solid;                            border-radius: 3px; border-width: 0.5px;                            border-color: rgba(1,140,0,100);")         def enterEvent(self, event):         self.setStyleSheet("margin: 1px; padding: 7px;                            background- color: rgba(1,140,040,100);                            color: rgba(1,140,255,100);                            border-style: solid; border-radius: 3px;                            border-width: 0.5px;                            border-color: rgba(1,140,140,100);")         def leaveEvent(self, event):         self.setStyleSheet("margin: 1px; padding: 7px;                            background-color: rgba(1,255,0,100);                            color: rgba(1,140,0,100);                            border-style: solid;                            border-radius: 3px; border-width: 0.5px;                            border-color: rgba(1,140,0,100);") class PyQtApp(QtWidgets.QWidget):         def __init__(self, parent=None):         QtWidgets.QWidget.__init__(self, parent)         self.setWindowTitle("PyQt Application")         self.setWindowIcon(QtGui.QIcon("Your/image/file.png"))         self.setMinimumWidth(resolution.width() / 3)         self.setMinimumHeight(resolution.height() / 1.5)         self.setStyleSheet("QWidget                            {background-color: rgba(1,255,0,100);}                            QScrollBar:horizontal                            {width: 1px; height: 1px;                            background-color: rgba(0,140,0,255);}                            QScrollBar:vertical                            {width: 1px; height: 1px;                            background-color: rgba(0,140,0,255);}")         self.textf = QtWidgets.QTextEdit(self)         self.textf.setPlaceholderText("Results...")         self.textf.setStyleSheet("margin: 1px; padding: 7px;                                  background-color:                                        rgba(1,255,0,100);                                  color: rgba(1,140,0,100);                                  border-style: solid;                                  border-radius: 3px;                                  border-width: 0.5px;                                  border-color: rgba(1,140,0,100);")         self.but1 = PushBut1(self)         self.but1.setText("")         self.but1.setFixedWidth(72)         self.but1.setFont(font_but)         self.but2 = PushBut1(self)         self.but2.setText("")         self.but2.setFixedWidth(72)         self.but2.setFont(font_but)         self.but3 = PushBut1(self)         self.but3.setText("")         self.but3.setFixedWidth(72)         self.but3.setFont(font_but)         self.but4 = PushBut1(self)         self.but4.setText("")         self.but4.setFixedWidth(72)         self.but4.setFont(font_but)         self.but5 = PushBut1(self)         self.but5.setText("")         self.but5.setFixedWidth(72)         self.but5.setFont(font_but)         self.but6 = PushBut1(self)         self.but6.setText("")         self.but6.setFixedWidth(72)         self.but6.setFont(font_but)         self.but7 = PushBut1(self)         self.but7.setText("")         self.but7.setFixedWidth(72)         self.but7.setFont(font_but)         self.grid1 = QtWidgets.QGridLayout()         self.grid1.addWidget(self.textf, 0, 0, 14, 13)         self.grid1.addWidget(self.but1, 0, 14, 1, 1)         self.grid1.addWidget(self.but2, 1, 14, 1, 1)         self.grid1.addWidget(self.but3, 2, 14, 1, 1)         self.grid1.addWidget(self.but4, 3, 14, 1, 1)         self.grid1.addWidget(self.but5, 4, 14, 1, 1)         self.grid1.addWidget(self.but6, 5, 14, 1, 1)         self.grid1.addWidget(self.but7, 6, 14, 1, 1)         self.grid1.setContentsMargins(7, 7, 7, 7)         self.setLayout(self.grid1) if __name__ == "__main__":     import sys     app = QtWidgets.QApplication(sys.argv)     desktop = QtWidgets.QApplication.desktop()     resolution = desktop.availableGeometry()     myapp = PyQtApp()     myapp.setWindowOpacity(0.95)     myapp.show()     myapp.move(resolution.center() - myapp.rect().center())     sys.exit(app.exec_()) else:     desktop = QtWidgets.QApplication.desktop()     resolution = desktop.availableGeometry()Step # 7As you can see, adding these buttons and text field resized our window and this gives us a flexible view of the application depending on the device and other parameters. Let's write some functions for our app to demonstrate possibilities of the PyQt. The first button on the top will change the background-color and color of our text field.def on_but1(self):     self.textf.setStyleSheet("margin: 1px; padding: 7px;                              background-color: rgba(1,140,0,100);                              color: rgba(1,255,255,100);                              border-style: solid;                              border-radius: 3px;                              border-width: 0.5px;                              border-color: rgba(1,140,0,100);")Step # 8We must connect our button and function for calling event. For this, we add next line to the bottom of the _init_ function of the main class.self.but1.clicked.connect(self.on_but1)Step # 9This step will add images to the application. Second button will be calling a function to copy the path of the image from the text field and put this image to the right bottom corner.Adding QLabel:self.lb1 = QtWidgets.QLabel(self) self.lb1.setFixedWidth(72) self.lb1.setFixedHeight(72)and function:def on_but2(self):     txt = self.textf.toPlainText()     try:         img = QtGui.QPixmap(txt)         self.lb1.setPixmap(img.scaledToWidth(72,                            QtCore.Qt.SmoothTransformation))     except:         passWe are using try/except construction because we are not sure that text in the text field will be a path to the image.Connecting second button and function:self.but2.clicked.connect(self.on_but2)Step # 10Completing and running application. PyQt has a various functionality and gives possibilities to create completely desktop applications. Also provides threading support.Full pyqt_app1.py file looks like this:ConclusionWe described here a very simple example of creating GUI with the Python-based framework — PyQt for developers. This platform provides a lot of possibilities to build fast, functional and flexible applications with a graphical interface, supporting threading with QThread class that solves problems with large functionality and some Python related problems with implementation of the real-time processes. PyQt supporting all available Python standard library gives full toolset for programming to Python-based developers.I created this tutorial to give beginner’s ample insights required to setup Python GUI programming skills in them that helps to get started with building GUI’s using Python. 
Rated 4.0/5 based on 29 customer reviews
10 steps for Getting started GUI`s with python

10 steps for Getting started GUI`s with python

Blog
IntroductionPython programming language is used as a basis for a lot of tools and packages for creating a flexible, usable and reliable desktop application with Graphical User Interface - GUI. These...
Continue reading

Future of Machine Learning with AR and VR

How it'll feel when we watching the movie we could enjoy the smell of the environment of the scene along with images and sound? Imagine, in a scene of a movie a hero commit an accident, if we could taste the smell of that spot, the smell of burning tire, the smell of broken trees?Nowadays with virtual reality, one can enjoy the feeling of beach area with cold weather and beautiful waves. Modern technologies are moving towards the next big thing i.e Machine learning, augmented reality, and virtual reality world. Tech world now adopting the ability to personalize a customers experience based on their individual behavior and bring that to life virtually.The motivation of machine learning is continually engaging with users and build a smarter solution as it aims taking data continuously and make customized experience based on user demand. Now, the trick is how to have the most user friendly interface that can seamlessly use that data and apply it with a realistic and useful way. In this case, augmented reality (AR) and virtual reality (VR) uses machine learning to develop such featured machine. Let’s see the upcoming trends in machine learning and how it boosts AR and VR technology along with some examples.Generally, by balancing the air of sound system and device, we can make sight experience almost same as real experience. Now the things left is the smell of salty water of sea and smell of sea animals.Probably we can get the smell of smell device. That device will be similar to printer device wherein spite of ink there will be various smell and combination of a different smell. From which according to the video scene, the smell will be sprayed.How it'll feel if a robot smell a pizza can say the weather is musty or not? In order to do this, we can train robot's intelligence with thousand of smell. ie burn chicken smell, human's smell etc.Then the machine will learn from training and can perform automated tasks.In the most case of crime investigation and customs, dog squad is used to identify criminal or find out harmful drugs, explosives. Dog squad is trained with the smell of criminal's dress piece, hair or anything before the operation. Similarly, dog got trained with negative drugs and explosive before the operation. After they can determine and able to find out criminal’s location or harmful drugs, explosives. If we could train robot with these things, we can easily give rest to dog squad. The plus point is dogs can do mistakes but the machine won't. Is this really possible?Maybe, Yes. This type machine can be made but it is a long process. Because research going on the smell. Biologists are working hard, To understand how smell works In 1928, in order to explain smell Malcolm Dyson told "Smell generated due to vibration of infrared range" .Next, In 1949 RW Moncrieff told "Smell is a combination of 7 primary odor like the visible color is a combination of three basic colors (Red, Blue, Green)''. Primary Odors are including Sweaty, Spermous, Fishy, Malty, Urinous, Musky etc. By mixing all these odar all the smells are generated. To detect these smell, there are seven receptors in humans brain. We can feel the actual smell with the combination of these receptors.It was proved in research, a human can detect almost different 10000 smell.Can you imagine?10 thousand smells !!!Now if we want machine will understand smell, setup seven receptors on the machine are not sufficient, first, we have to know which primary odor present in which smell. The problem, we can't do this work. A composite smell is a combination of which primary smells, biologists didn't identify yet.Nowadays new theory has been introduced named "Odotope theory". It seems scientist are confused how smells work. We hope, we'll able to define smell soon. Then we’ll able to develop our automated machine soon that can test food smell and direct us whether that food is musty or not, can warn us about our sickness by taking our body smell. Where crime will happen we'll set our machine and by the internet, it will detect location wise every human smell and able to find criminal in very short time. Our main challenge is to find how smell properly works then we’ll move to a new era of technology where general life will become more user-friendly and based on Augmented reality (AR), Virtual Reality (VR).A human can hear due to olfactory nerve. But without that nerve, we can introduce machine with sound from analog to binary and binary to the analog conversation. In Chemistry, we can identify ion with Line spectrum. Like in bunsen burner flame, we can find golden-yellow flame from sodium (Na) ion. All these are some of the predictions of machine learning with AR and VR defining future.Conclusion Part.Like these, if we able to make relation of smell with light or something that is familiar to the machine, then IT will enter a new world of smell technology of Artificial Intelligence. We’ll able to ensure more user-friendly virtual reality and augmented reality combined with artificial intelligence. Best of luck for that times Artificial intelligence and Technology.
Rated 4.0/5 based on 23 customer reviews
Future of Machine Learning with AR and VR

Future of Machine Learning with AR and VR

Blog
How it'll feel when we watching the movie we could enjoy the smell of the environment of the scene along with images and sound? Imagine, in a scene of a movie a hero commit an accident, if we could ta...
Continue reading

How To Run Background Process In Node.js Using RabbitMQ?

What are background processes? When somebody tells me about background process, the picture that comes to my mind is something that is not seen by anybody, but that does some kind of work behind the scenes. In reality, it is pretty close to this definition. Background process in a web application scenario is some process which does some work away from the normal request-response loop. Some good examples of this are sending emails, generating reports, scheduling tasks, system monitoring or even sending out notifications to users as an effect of something that happened in the system. The need for background processing Typically in web applications, great efforts are made to maximize the responsiveness by getting only the critical data required to render the current page to the screen as fast as possible, thereby completing the user request and handing back the control to the user. Ideally, requests taking more than a second or few seconds should be cached or split and moved to a background process. Typically when a request comes in, web-server spawns a thread to handle it. As long as the request is not served, the thread remains alive and will not be returned to the thread pool. If web-server takes too long to respond, other requests will be queued, response time gradually degrades, thread pool gets exhausted and your web-server crashes. Every web application, medium to large, requires some kind of background processing mechanisms to work smoothly. Usually, this is done via some kind of queueing mechanism. There are many message brokers available with popular ones being RabbitMQ, Kafka, ActiveMQ to name a few. Sidekiq (which is based on Redis) is used for job processing in Rails Applications. AWS also provides a message-queue service called SQS, which hardly requires any setup and is fully managed by AWS. Options for background processing in NodeJS Node.js is basically a good fit for background processing and there are many ways of doing it. For the people who came from Rails world, there is Node-resque. Then there is Bull and Kue, for handling jobs and messages, which makes use of Redis for queuing. We can also use RabbitMQ for message-queues, which has a wide set of configurations to match your requirements. You can read more about the options for running background tasks in NodeJS in the StackOverflow thread here. In this article, we are going to discuss using RabbitMQ for background processing in NodeJS. Setting up RabbitMQ Installing RabbitMQ is the very first step to get started with background process in Node.js. In Linux Systems, you can directly use the following command to install `rabbitmq-server`: sudo apt-get install rabbitmq-server In MacOS the best way is to install it via Homebrew. You can do it with the following command. brew install rabbitmq A comprehensive download FAQ and options are given here in the RabbitMQ official download page. You can find the instructions to download the RabbitMQ installer for Windows systems also there. After you install the rabbitmq-server, make you sure you did it correctly by typing rabbitmq-server in your terminal. In MacOS, this is what it shows in the terminal.                                                                                             Rabbitmq-server Basics of RabbitMQ RabbitMQ is one the most popular open-source message broker. It supports multiple messaging protocols, has distributed deployment configuration support for high scalability and availability along with message queuing, delivery acknowledgment, flexible routing to queues and multiple exchange types. The two main entities in RabbitMQ are exchanges and queues. Queues are the place where the messages are actually stored. All the messages are published on to an exchange. The queues are connected to exchanges through a binding. Bindings contain the info on how the queue is connected to the exchange. It has something called routing rule, by which the exchange decide whether to forward the message to a queue or not.                                                                                       RabbitMQ flow A queue can be bound to many exchanges with different routing rules. The exchanges can be of different types namely direct, topic,headers, and fanout. Fanout exchanges are typically used when the messages on the exchange need to be forwarded to all the queues registered in the exchange. Fanout exchange is like mindless broadcasting. In a direct exchange, a message goes to the queues whose binding key exactly matches the routing key of the message which gives a level of filtering of messages forwarded to a queue. The topic exchange is like a direct exchange, with advancements in routing, used for multicast routing of messages. You can have routing rules with partial matching of the topic name. A headers exchange is designed for routing on multiple attributes that are more easily expressed as message headers than a routing key. Setting up a web server First let’s quickly set up an expressjs project. $ mkdir myapp $ cd myapp $ npm init With the above commands, we created a directory called myapp, made that the current working directory and initialized an npm package. Now we will install expressjs by the following command. npm install express --save Now we will create a file named index.js touch index.js From the express tutorial here, we will make a simple web app which shows a “Hello World” message. Edit the index.js to have the following content. Now run this web app with the following command : node index.js Now hit the URL http://localhost:3000 in your browser. If everything was done right, you should be seeing a Hello World! message. Sending messages to exchange There are a number of clients for RabbitMQ in many different languages. In this guide we use aqmp.node client in order to run node.js with express in background process. There are mainly two types of APIs that this client provides. One is callback based and the other is promise based. You can choose whichever fits your style or requirement. And in this tutorial, we will also use callback-based API.  The important step to make the most out of the tips to node.js task scheduling in background is an understanding of how to send messages to exchange. For this, we will install aqmp.node client first. npm install amqplib Now in the same directory, we will create a send.js file and add the following content. Here in the above code, we load the aqmp callback_api, open a connection to RabbitMQ, create a channel to communicate with RabbitMQ, assert a default queue named hello and send a message to that queue. While asserting the queue, we pass an option { durable: true } which makes the queue to persist even after the connection is closed. While sending a message to the queue, we pass an option { persistent: true } which makes the message persist in the queue. Here we do not see the exchanges because we are making use of default exchange. You can even give the queue name as blank, in which case the RabbitMQ server will generate a random name for it. Now run the following command to send a message to rabbitmq server. node send.js There is a rabbitmq-management plugin that comes with rabbitmq package. The rabbitmq-management plugin provides an HTTP-based API for management and monitoring of RabbitMQ server, along with a browser-based UI and a command line tool, rabbitmqadmin. You can access the browser-based UI by typing in http://localhost:15672/ in your browser. After running your send.js, you can go to http://localhost:15672/#/queues . There you can see a queue named hello and total message count as one.                                                                          RabbitMQ Management UI — Queues We have successfully sent a message to a default exchange onto a queue in RabbitMQ server. You can read more on this here. Receiving messages from queue We will first create a worker.js file in the current working directory with the following command. touch worker.js In this script, we open a connection to rabbitmq, then create a channel, then assert the queue and then consume the queue through the channel. Modify worker.js to have the following content. While consuming messages in a queue, in the real world we might need to have some kind of acknowledgment after processing the message so that even if the processing fails the message is not lost. In the code above we pass an option { noAck: true } to consume method, which means that the rabbitmq server won’t wait for the acknowledgment from the client side after processing. The server will delete the message from the queue as soon as it is delivered to the client. If you don’t want the server to delete message once it’s delivered, but rather you want the server to delete message from queue once you successfully processed it, you can pass the option { noAck: false } and in the code, inside consume, after you process the message, you can acknowledge it by channel.ack(msg) . Now let’s run our worker to consume the queue by the following command. node worker.js You will see the following output in the terminal. Now if you go to http://localhost:15672/#/queues you can see that the messages count in the queue is zero. Putting it Altogether In the above sections, we have seen how to send messages to rabbitmq and consume it. Now let’s make use of it in our web app.  Let’s chart out a basic workflow of our example website. Consider our website having an email subscription form. Users will give their email and click submit button. This should send an email to the user and display a thank you message. Since the flow is set, let’s think of how we can do this. Sending an email is a time-consuming operation compared to other web requests. So let’s take that email sending-part out of the main request-response loop. We will send the email to the exchange and our worker will take that from the queue, process it, send the email and send an acknowledgment to the server so that server can delete the message from the queue. We will use ejs for rendering HTML pages and nodemailer for sending emails. So let’s install those packages. npm install ejs npm install nodemailer Then let’s create an HTML form to get the emails from the user which will be rendered on the landing page. Create a directory called views and file called index.html inside views directory. The content of index.html is given below. Now we need to display the form on the landing page. For that, we modify our index.js to have the following content. This will render the HTML form with a single input field for email and a submit button. Form submit is handled by /subscribe . On subscribe, we will send the email details to the rabbitmq queue.  Now run node index.js for the server. Make sure your rabbitmq-server is running. Go to http://localhost:3000 . You will see the following page.                                                               Subscription form Now type in an email and click submit. You will see a page with the message Thank you. You are successfully subscribed. . Now if you go to rabbit-mq browser UI at http://localhost:15672/#/queues you can see a new queue named email with a total message count of 1.                                                                                  Email queue Now let’s go back to our worker and make the worker consume the email queue and send an email. We will use nodemailer to send emails. Nodemailer supports ethereal.mail accounts for testing. We will leverage that for sending emails in this tutorial. Edit worker.js to have the following content. Now run the worker in another tab of the terminal with node worker.js . You can see the following message.                                                                                       worker log So in the above example, we have successfully isolated the email sending from the normal request-response loop. This can be extended to many other use-cases like report generation, fetching data from third party APIs, complex calculations, scheduling jobs …etc.  You can view all the files used in the project here. Congrats — you have successfully published messages to rabbitmq-server and wrote code to implement the consumption and processing of that messages. Feel free to try the same exercise for other use-cases also. Don’t hesitate to reach out with general feedback or with questions.
Rated 4.0/5 based on 12 customer reviews
How To Run Background Process In Node.js Using RabbitMQ?

How To Run Background Process In Node.js Using RabbitMQ?

Blog
What are background processes? When somebody tells me about background process, the picture that comes to my mind is something that is not seen by anybody, but that does some kind of work behind th...
Continue reading

A Step-By-Step Guide To Decoding Browser

A browser is used to access the worldwide web fastly and securely. Every time, When URL (Uniform Resource Locator) is typed in the Browser’s address bar, we are all fascinated by the Web page appearance. Everyone is not intended but Web developers, mainly to know what exactly happening inside to get you the nice attractive web page. This beginner’s guide will give you a detailed guideline on the complete process of decoding browser.                                                              Address Bar Let’s imagine that you want to access Stack Overflow Website and typed “https://stackoverflow.com/" in the Address bar which is part of browser’s User Interface will talk to Network layer.                                                            User Interface (Address Bar) to Network Layer   By considering the above example, here, we will clearly demonstrate the continuous steps to decoding browser. The Network layer checks the Cache for a DNS record to find the corresponding IP address of domain “StackOverflow.com”. What is Cache? A collection of data duplicating original values stored elsewhere on a computer, usually for easier access. By Cache, we meant Web Cache. What is Web Cache? A web cache (or HTTP cache) is an information technology for the temporary storage (caching) of web documents, such as HTML pages and images, to reduce server lag. For example: ETag, CDN). Four Level Cache There will be four levels of Cache available that help to check for DNS Records to find Domain IP Address. What is DNS? The Domain Name System (DNS) is a hierarchical decentralized naming system for computers, services, or other resources connected to the Internet or a private network. The very first step to make the most out of the tips to decoding browser is an understanding of the different caches used. Let’s have a look at the below diagram for browser request and check for the four levels of Caches, if one does not exist. 1. Browser Cache—Check whether you visited earlier for DNS cache where browser maintains own cache of DNS. 2. OS Cache—When Browser Cache is not available, browser would make System call for the DNS Cache information maintained by Operating System. 3. Router Cache — When Browser and OS Cache are not available, it will look for Router Cache. 4. ISP Cache — When above all three Caches are not available, ISP’s DNS server initiates a DNS query to find the IP address of the server that hosts Stack Overflow.com. Assume, Network layer worked closely with the DNS Record Search to find the IP Address using the corresponding domain (StackOverflow.com). Now, we found the IP Address from DNS Record to proceed further. Cool, our next step is sending request and receiving the Response. In the following section, we shall explore the ways how to manage HTTP Request and Response for an effective browser decoding. HTTP Request & Response Browser’s Network Layer manages HTTP Request and Response. Below diagram helps to understand how HTTP Request and Response are being Sent and Received. Browser send TCP Connection with Stack Overflow Server through IP Address. TCP Connection is established for data transmission. Browser sends an HTTP Request to the Web Server. StackOverflow Server send out an HTTP Response to Browser. Shush, Be quiet. It’s not over. We just got the HTTP Response. Now you see the below Network information where you can see IP Address (151.101.65.69) which is helping to make HTTP Request.                                                                           Stack Overflow IP Address with HTTPS PORT (:443) Responses Content Types: HTML, CSS, JavaScript So always the Life Cycle of sending a request and receiving the response is same for all the various responses but every response we get has a different Content Type. Below are the series of various responses. Imagine the phase that we got the response from network layer and yet to get passed to browser engine to process. 1. HTML Response Header Below is the Response Header of every web page which is actually HTML of Content-Type is “text/HTML”.                                                                                     Browser Response Header for HTML 2. Image Response Header Below is the Response Header of PNG Image and Content-Type is “image/png”. There are different images available like JPEG which is “image/jpg”, GIF which is “image/gif” and SVG which is “image/svg+xml”.                                                                                                 Response Header of HTML 3. JavaScript Response Header Below is the Response Header of JavaScript File and Content-Type is “text/javascript”.                                                                                               Response Header of JavaScript 4. CSS Response Header Below is the Response Header of Stylesheet File and Content-Type is “text/css”.                                                                                                      Response Header of CSS Rendering Engine Browser User agent receives the Response and passes it to Rendering Engine which is also called as Layout Engine. Lets know about the rendering engine and how it works in order to understand the decoding browser bit-by-bit. What is Rendering Engine ? A rendering engine is a software that draws text and images on the screen. The engine draws structured text from a document (often HTML), and formats it properly based on the given style declarations (often given in CSS). Examples of layout engines: Blink, Gecko, Edge, WebKit.                                                                Critical Rendering Path Let’s take example response of HTML Content which is the text document in nature. Rendering engine parses the markup document and creates the Well Formed document and constructs the Object called DOM and for CSS file its creates CSSOM The CSSOM and DOM trees are combined into Tree is known as “Render Tree”.                                                                  Render Tree of DOM and CSSOM —  Image Credit: https://developers.google.com Render tree has DOM and CSSOM nodes which are needed to render the Web page. Layout that finds the accurate position and size of each object to draw the web page according to the Viewport of the device is called “Layout” Stage, also known as “Reflow”.                                                                                                    Render Tree Formation Painting (Stage Show) This is the final act which is known as Painting / Rasterizing where the visible nodes from Render Tree are converted to actual pixels on browser Screen.  The Output of the Layout Process is called Box Model where it adds padding, border, and margin. Handling JavaScript There is one exceptional Response, It is our Super Hero JavaScript who can do things dynamically. No matter whether it is HTML or CSS or both. But this guy handles nicely who is our Browser’s Scripting Language. When there is a JavaScript Content-Type of Response, it will be communicated to the Scripting Engine of the Browser (Which is an Interpreter also JIT Compiler who does things in Run-Time) will execute the javascript code and through JavaScript, you can access DOM and CSSOM API to make it interactive. It will throw a series of errors, if there are any errors found in that particular JavaScript file.   Hope this tutorial offered ample insights to understand the essentials of browser decoding bit-by-bit. Cool isn’t it! Happy Browsing!
Rated 4.0/5 based on 12 customer reviews
A Step-By-Step Guide To Decoding Browser

A Step-By-Step Guide To Decoding Browser

Blog
A browser is used to access the worldwide web fastly and securely. Every time, When URL (Uniform Resource Locator) is typed in the Browser’s address bar, we are all fascinated by the Web page appear...
Continue reading

Understanding how to write a custom middleware in Django

This a very basic tutorial on understanding Django-middlewares. We’ll try to understand middlewares and write one of our own. The prerequisite for this tutorial is nothing but knowing the Django application structure and understanding the requests/responses. What are Middlewares? The name sounds all fancy and stuff but it’s quite straightforward if you think about it. We’ll take an analogy and compare it with what it actually means and try to know it better. According to Docs: Middleware is a framework of hooks into Django’s request/response processing. It’s a light, low-level “plugin” system for globally altering Django’s input or output. Let’s consider a situation where you use 3 different kinds of software. For example, you use X for builds, Y for documentation and Z for development purposes. But now you want to integrate all of this into one big system to keep your software updated. If you go ahead and start doing it manually, it may take a long period of time. But somehow you come up with a framework that reads the input/output of your resources and gives an overall correct output for an input. In other words, the output generated by X can be used by Y and Z. So basically, Middleware passes data between 2 or more applications. Examples of operations performed by Middlewares: HTTP authentication Session management Encryption Validation How does Django Middleware work? It is essential to know how Django middleware process the request before going to create a custom Django middleware. The following picture shows the working of Django middlewares.                           Example of how Django middleware are processed The following block of code shows the middlewares that Django provides us. You can find these in the settings.py file of your Django application.   Django applies the above middleware order in a top-down manner. Each Middleware class is a layer that wraps the view function. When the request passes through all the layers one by one, a response is generated and it again goes through all the layers on its way back out. Writing a custom Middleware Now that you know what is a middleware and how actually it works, you can write your own Django custom middleware. For this, I’ll be showing you a very simple example of a custom middleware. Remind you that the practice followed is as per the Django documentation. In very simple terms, it is much like a view function. The only thing we need to care about is that there are 2 hooks available for request and 3 for response. Before calling the view, in the request phase, the available hooks are: process_request() process_view() After calling the view, in the response phase, the available hooks are: process_exception() process_template_response() process_response() While writing a middleware, all we need to do is define one or more of the above functions as per our need. Let’s suppose that we have a Django app that has the following models.py file. And now create a file where you define your custom middleware that uses the above model. Let’s call this file as moviemiddleware.py Now in your settings.py file, Note that the order of middlewares is very important. When you run your application, it should output the following: This was a very trivial example to let you know how exactly middleware can be used and further manipulated for our own purposes. Now let’s take a little more complicated example. In the following example, we will write a middleware that can handle redirects in the URL along with parameters. The default Django project doesn’t allow it beforehand. The logic of our middleware will be very simple: We’ll separate URL parameters from the absolute_path Next get exact `redirect_path` of separated absolute_path Then append the URL parameters which separated from absolute_path to redirect_path Since this is a response that we get in our HTTP cycle, we need to make a process_response() function in our middleware class. The layout of our CustomRedirectMiddleware.py will be - Let’s get into it and write a middleware for this. As you can see, the above code handles the three logic statements and can be used in your application by adding it to MIDDLEWARE in your settings.py file. There are a lot more cases where middlewares can be used as per the need of the application. The things to keep in mind from this tutorial are: Ordering in middlewares is very important as they are processed while an HTTP cycle. At least one of the requests/responses is necessary in order for it to be a Django middleware. Hope this article helped you to clearly understand how to set up a custom middleware in Django.  
Rated 4.0/5 based on 23 customer reviews
Understanding how to write a custom middleware in Django

Understanding how to write a custom middleware in Django

Blog
This a very basic tutorial on understanding Django-middlewares. We’ll try to understand middlewares and write one of our own. The prerequisite for this tutorial is nothing but knowing the Django ap...
Continue reading

Different Types Of Observers Supported By Modern Browsers

Before diving deeper into the observers family supported by modern browsers, let’s try to understand what is observer in general? What is Observer? Observer is a programme which watches or notices something. Observers can be helpful to watch certain activities happening in the browser and respond accordingly. Observers are similar to dogs, watching certain activities and alerting us if something unusual happens. And it is our responsibility to take an action once we are alerted by dogs for certain activities. Using observers we are able to watch different types of activities that occur in the browser, and take necessary actions. For eg. we can observe if a video is displayed within the view port and enable autoplay, if child element has been added or removed from the parent DOM element, if the size/dimensions of a box element has changed and so on. Following are four different types of observers supported by modern browsers. Intersection Observer Mutation Observer Resize Observer Performance Observer Intersection Observer :-  It is used to observe intersection between two HTML DOM elements. It is useful to watch an element in your DOM when it enters or leaves the visible viewport. Some of the use cases of Intersection Observer are described below Lazy-loading images or other resources when an element is visible within the viewport. ​Identifying visibility of advertisements and calculating ad revenues. Implementing “Infinite Scrolling” websites (eg. Forbes) when user scrolls down the page so that they don’t have to navigate through different pages. Loading and autoplaying, videos or animations when an element is within the viewport. Browser support :- Currently, firefox and chrome supports Intersection observer but we can find polyfills for older browsers. Using IntersectionObserver api mainly requires three steps a) Creating Observer b) Defining target object to observe c) Defining callback handler a) Creating Observer:- Threshold of [0.3] means, when target element is 30% visible within the element specified by root element, invoke the handler function. It means handler/callback function is invoked whenever element is visible by 30%, 50%, 80% and 100%. b) Defining target object to observe :- We can define multiple target objects to observe. As mentioned in the example before, a dog should know what to observe before alerting everyone. Any target element can be observed, simply by calling .observe(target) method of the respective observer. c) Defining callback handler :- This is the response to make when one notices an alert, for something unusual has happened. Callback handler is fired whenever, target element is intersected with the root element by threshold values. I have prepared a demo which uses Intersection Observer to change the color of the target box when it is seen within the visible area as user scrolls up.   See the Pen intersectionObserverExample by jitendra kasaudhan (@jitubutwal144) on CodePen. References: Demo , Intersection Observer API Mutation Observer :- Mutation observers are used to observe changes in the DOM elements. It is useful to observe changes such as addition or removal of child nodes in a parent node, changes in the value of attributes or data content etc. Before MutationObserver, DOM change events were handled by Mutation Events such as DOMAttrModified, DOMAttributeNameChanged, DOMNodeInserted. Mutation observer is designed as a replacement for Mutation Events defined in the DOM3 events specification. The practical reasons to avoid the mutation events are performance issues and cross-browser support. Probably the biggest audience for this new API is the people building JS frameworks, mainly to solve problems and create interactions. Another use case would be a situation where you are using frameworks that manipulate the DOM and need to react to these modifications efficiently ( and without setTimeout hacks). Browser Support :- It has pretty good support across different browsers. In general, implementation of MutationObserver requires three steps- a) Creating Observer :- It can be created, simply by calling its constructor and passing handler function and configuration options. We have an option to specify, what kind of changes do we want to track or observe. childList: true, means observe changes related to child nodes, attributes: true means observe attribute change, characterData: true means observe changes in target element’s data content. b) Defining target object to observe :- observer.observe(…) method accepts target element which should be observed. c) Defining callback handler :- Depending upon the configuration used during observer creation process, callback function is executed whenever changes occur in the target element. Callback function is fired with mutation records object which contains the type of mutation occurred in the target element. I have prepared a demo that fires mutation observer’s callback handler, whenever new child node is added or removed along with attribute change.   See the Pen mutationObserverExample by jitendra kasaudhan (@jitubutwal144) on CodePen.                                                                                           Mutation Observer Demo References: Demo, Mutation Observer API Resize Observer :- Resize Observer allows us to watch changes in content rectangle size (width, height) of a DOM elements and react accordingly. It is like document.onresize() or window.resize() events for elements. It is useful when the elements change their size without resizing the main window. For example, appending new children, setting element’s display property to none or similar actions can change the size of an element, its siblings or ancestors. It only watches the content box. Some of the behaviours of resize observers are described below observation will fire when watched Element is inserted/removed from DOM. observation will fire when watched Element display gets set to none. observations do not fire for non-replaced inline Elements. observations will not be triggered by CSS transforms. observation will fire when observation starts if Element has display, and Element’s size is not 0,0. Resize observer notifies about the dimensions of content box as shown in the figure below Browser Support :- Currently, it is not supported by most of the browsers except chrome ≥ 64.   Observer’s api can be used in three steps as other observers. a) Creating Observer :- It can be created, simply by calling its constructor and passing handler function. b) Defining target object to observe :- Define target object whose change in size should be observed. c) Defining callback handler :- See the Pen resizeObserverExample by jitendra kasaudhan (@jitubutwal144) on CodePen. Risize Observer Demo References: Resize Observer Usage Performance Observer :- It is used to observe the Performance Timeline and be notified of new performance entries as they are recorded by the browser. It can be used to measure certain performance metrics both in browser and node.js applications. In the browser we can access observer using Window object as window.PerformanceObserver and in node.js application we have to require perf_hooks to get performance object. Eg.const { performance } = require(‘perf_hooks’); It can be useful in following cases Measure processing time between request and response. (In browser) Calculate duration while retrieving data from database. (In nodejs app) Abstract precise timing information using Paint Timing API such as time for first paint or first contentful paint. Access performance metrics using User Timing API, Navigation Timing API, Network Information API, Resource Timing API, Paint Timing API Implementation of PerformanceObserver requires three steps a) Creating Observer :- It can be created, simply by calling its constructor and passing handler function. b) Defining target object to observe :- observer.observe(..) method accepts valid set of entry types that can be observed. These entry types may belong to various performance API such as user timing or navigation timing API. Valid entryType values are: "mark”[USER-TIMING], "measure" [USER-TIMING], "navigation" [NAVIGATION-TIMING-2], "resource" [RESOURCE-TIMING]. c) Defining callback handler :- Callback handler is fired whenever the observed event is used in the application. For eg. it can be used to mark the instant and measure the duration from start to end instant.   See the Pen performanceObserverExample by jitendra kasaudhan (@jitubutwal144) on CodePen. Performance Observer Demo Feel free to play around with a simple demo which uses PerformanceObserver API. When a button is clicked by a user, it will mark the instant as start and end and measure the duration the after 3000ms. Output can be viewed in console. References: Demo , Irina Shestak: Storytelling With Performance Observers -JSConf.Asia 2018, Performance Observer API , NodeJS perf_hooks API Please let me know if you have any thoughts/suggestions/improvements regarding these observers.
Rated 4.0/5 based on 10 customer reviews
Different Types Of Observers Supported By Modern Browsers

Different Types Of Observers Supported By Modern Browsers

Blog
Before diving deeper into the observers family supported by modern browsers, let’s try to understand what is observer in general? What is Observer? Observer is a programme which watches or not...
Continue reading

Displaying Application Breadcrumbs In Vue.js

Breadcrumbs are essential if your application has many nested screens and allows users to keep track of their locations when they are navigating the app. Let’s see the approach to implement it in Vue.js. Source code for this post is here. In many frameworks, a naive way of implementing would be by adding breadcrumb metadata in the route definition in an array and then iterating it in every component which needs to show the crumbs. The problem with this approach is we are hardcoding the path and we can’t show crumbs for dynamic params. If we are to do what I said just now in Vue.js, we could do it this way.                                                                        Route definition with static breadcrumbs In the snippet above :moon param belongs to the first child of the third route /saturn. This param is determined at runtime and I can’t specify it’s crumb in the breadCrumb property of its route definition. The static crumbs defined above can be displayed by iterating $route.meta.breadCrumbs array in any component.   Dynamic Breadcrumb component vue-router provides a $route object which represents the state of the currently active route. I feel this object provides us enough information to display the crumbs at runtime. The properties I’m interested in are $route.path and $route.matched. $route.path — A string that equals the path of the current route, always resolved as an absolute path. e.g. "/foo/bar". $route.matched — An Array containing route records for all nested path segments of the current route. Route records are the copies of the objects in the routes configuration Array (and in children Arrays). The Route Object — vue-router Alright, let’s get started. Say I have loaded a component with path /earth/moon, I have added the text for the crumbs in the meta property of my route definitions like below and I want the bread crumbs as Earth > Moon. The current absolute path for the component can be accessed through $route.path and inside a component, I can access it through this.$route.path. // this.$route.path will be equal to '/earth/moon' let pathArray = this.$route.path.split("/") // pathArray is now ["", "earth", "moon"] // we don't want the first item as it's an empty pathArray.shift() // pathArray is now ["earth", "moon"] Now I need to access the breadCrumb property from the meta property of the component’s route definition, for this I will be using $route.matched. For the component /earth/moon the $route.matched would contain two objects as it has matched two routes i.e. /earth and /earth/moon. 0: {path: "/earth", regex: /^\/earth(?:\/(?=$))?$/i, components: {…}, instances: {…}, …} 1: {path: "/earth/moon", regex: /^\/earth\/moon(?:\/(?=$))?$/i, components: {…}, instances: {…}, …} These two objects are the copies of the objects in the routes configuration Array of the /earth and /earth/moon routes. I can assess breadCrumb property for the /earth component like this this.$route.matched[0].meta.breadCrumb and for /earth/moon like this this.$route.matched[1].meta.breadCrumb Next, I will create a new Array of Objects where each Object will have property to and text for each item in the pathArray . Each Object in the Array will represent a crumb. I will name this Array as breadCrumbArray. Lately, I’m liking to write in Functional Programming style 🤓, so I will use reduce method to simplify this Array creation process. In the above snippet path property stores the string of the item in pathArray as is, to property will store path for the :to property for the router-link which will be triggered when clicking the crumb. In the to property we prefix the previous item’s path if it exists, else we assign the current item’s path to it. The resulting array of the reducer is assigned to breadcrumbs variable and it will look like this. [    { "path": "earth", "to": "/earth", "text": "Earth" },    { "path": "moon", "to": "/earth/moon", "text": "Moon" } ] The last step would be to wrap the above logic in a vue component. I will use the breadcrumb component of bootstrap-vue to display the bread crumbs. The final breadcrumb component will look like this. The above logic will also handle dynamic parms as we are picking up the constructed path from $route.path. The crumb text for dynamic params will be the path itself. You can see this at line 23 in the above snippet, I’m assigning the path if the breadCrumb property doesn’t exist.  Live demo demonstrating everything explained above is here.
Rated 4.0/5 based on 12 customer reviews
Displaying Application Breadcrumbs In Vue.js

Displaying Application Breadcrumbs In Vue.js

Blog
Breadcrumbs are essential if your application has many nested screens and allows users to keep track of their locations when they are navigating the app. Let’s see the approach to implement it in Vu...
Continue reading