<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gautham Sreeram Dasu on Medium]]></title>
        <description><![CDATA[Stories by Gautham Sreeram Dasu on Medium]]></description>
        <link>https://medium.com/@gauthamdasu?source=rss-52cb448a557d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*kou2JX9FfUgmkUOJcUUaow.jpeg</url>
            <title>Stories by Gautham Sreeram Dasu on Medium</title>
            <link>https://medium.com/@gauthamdasu?source=rss-52cb448a557d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 05:15:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gauthamdasu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[5 best practices to follow while developing code]]></title>
            <link>https://gauthamdasu.medium.com/5-best-practices-to-follow-while-developing-code-f59888a3b31e?source=rss-52cb448a557d------2</link>
            <guid isPermaLink="false">https://medium.com/p/f59888a3b31e</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[practice]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Gautham Sreeram Dasu]]></dc:creator>
            <pubDate>Fri, 13 Jan 2023 15:17:43 GMT</pubDate>
            <atom:updated>2023-01-13T15:35:41.978Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi there, in this short blog, lets see 5 best practices to be followed when developing code/ software.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mLoqmUULIj92TNGAwRekgQ.jpeg" /><figcaption>Source: My camera gallery</figcaption></figure><p>TL/DR;</p><p>Here are the five practices and lets see each one of the in detail:</p><ol><li>Remote commits with apt commit messages</li><li>Code linting, commenting</li><li>Branches for new features</li><li>Use an editor to code</li><li>README/ Project detail file</li></ol><h3><strong>Remote commits with apt commit messages</strong></h3><ol><li>The need for version control system cannot be stressed when developing a huge project. In those terms, short and meaningful commits make a huge difference. By this what we mean is that after successfully completing code development and testing of a piece of code, it is always a good practice to commit the changes made and push to remote repository. Commits need not be full project development, it could be as simple a single file change, or a days work or even multiple files modification. Regular commit to the remote repository helps in handling any inevitable hard disk crashes or working on different machines etc. Also when we commit, we do add commit message, it is equally important to have meaningful commit messages when committing the code. This practice is important because, in later point in times, when any need arises to check the changes made to the files, the commit messages would be of help which summarizes the changes made in the code. Let’s take an example, suppose say, I have made changes in a python file which connects to a rest api endpoint and then returns if the connection is getting established to the endpoint or not (basically checking the live state of the rest endpoint server). Suppose we make changes to the same file and add functionality to read the data and print on the screen, then my commit message could be like “Modify and add screen flush functionality ”, which shortly and crisply tells about the modification made to the code. In this way for later reference, commit messages and code commits are helpful.</li></ol><h3><strong>Code linting, commenting:</strong></h3><ol><li>Lets see each one of them in detail, by code linting, we mean the coding standards that are generally to be followed by the developer when writing the code. Lets take python for example, pep8 guidelines/standards are some guidelines put forth to guide python developers to practice them when developing the code. pylint, pep8, flake, bandit, pylama etc are some of the linters available for python. Commenting is another key aspect of code development, by commenting, what we mean is to write the synopsis of the code, i.e; what are the input parameters (if any) to the method, what are return values (if any), logic of the code. Commenting helps not only the person who had developed the code but also to the person who would be extending or adding new features to the code (developing the code) in future. Code commenting is useful for both the developer and future developer who would refer the comments written by them in the future.</li></ol><h3><strong>Branches for new features:</strong></h3><ol><li>It is always important that we maintain a master copy which works well and can be cloned and can possibly deployed. By branch we mean, git branch, which is like a copy of all the files, and we get to work on copy of the code that is there in the master branch. But when we want to develop any new features to the existing code, it is always a good idea to checkout (make a new branch) from the master(main, working) branch, develop the code, make pull request to merge the changes made for the feature, get it reviewed by other developers and then merge the changes made to the master (working) branch. To checkout to a new branch in git we can use the following command:</li></ol><pre>git checkout -b “name_of_the_branch”</pre><p>Also it is important from which branch, we are checking out a new branch. Checking out means, creating a new branch from a source branch.</p><h3><strong>Use an editor to code:</strong></h3><ol><li>Yes you heard it right, we were not aware about different editors available when developing code, which would help us in many different ways in developing the code. The extensions, packages, and other features like live server page, git, gitlens, etc, helps developer in improving the productivity of the developer. Eclipse, Visual Studio, Visual Studio Code, PyCharm, etc are some of the editors which helps the developer in improving their productivity.</li></ol><h3><strong>README/ Project detail file</strong></h3><ol><li>Readme or project page, as the name suggests, should basically have all the details related to the code developed for that project, where in on-boarding of new developer will be hassle free job for other co-developers. Typical readme can have details about the scope of the project, few background details about the project, tech stack used in the project, details on how to clone the repository and run the project. Markdown based readme files is a good option for maintaining the readme files.</li></ol><p>That’s it for now, hope these practices will be of use to you. Thanks for your time. If you feel you have gained some thing of the blog, kindly follow me which motivates me to learn and write more blogs. Thanks! once again.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f59888a3b31e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Few best settings and practices when working with Django projects.]]></title>
            <link>https://awstip.com/few-best-settings-and-practices-when-working-with-django-projects-af207cf55fb4?source=rss-52cb448a557d------2</link>
            <guid isPermaLink="false">https://medium.com/p/af207cf55fb4</guid>
            <category><![CDATA[webdev]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[tips]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Gautham Sreeram Dasu]]></dc:creator>
            <pubDate>Mon, 17 Oct 2022 18:54:47 GMT</pubDate>
            <atom:updated>2022-10-24T12:09:10.869Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yvz0_c-NFOeKRxi23hGpOw.jpeg" /><figcaption>Source: My image gallery.</figcaption></figure><p>In this blog lets see few settings and practices which I felt useful and applied when working with Django applications. I hope they will be of use to you as well and do let me know in comments if they are helpful. And without any further adieu lets see them.</p><ol><li><strong>Custom authentication back-end.</strong></li><li><strong>Authentication with email or username and password.</strong></li><li><strong>Decoupling sensitive information.</strong></li><li><strong>DB and Static file Settings.</strong></li></ol><p>I would recommend you also to practice these settings and practices for your existing and upcoming projects.</p><p>Let’s dive into the details of each of them.</p><ol><li><strong>Additional authentication backend</strong>:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/0*yOsd5b12Wn_FAkPL.png" /><figcaption>source: <a href="https://uxwing.com/authentication-icon/">https://uxwing.com/authentication-icon/</a></figcaption></figure><p>Django is often refereed as “batteries included” framework which consists of all the stater material to kickoff our ideas as an web-application. As it is well know that authentication to any app is an important aspect be it in web or desktop applications, Django comes with an inbuilt authentication system(backend), which is basic, can be used for most of the use cases and authenticate user to access the applications. Hence secure access and authentication are hence taken care off. But when we have scenarios where our application needs to communicate with different applications we would be requiring a different authentication backend. Django by default has model backend to authenticate the users. When a need arises for having an LDAP (Microsoft user management system) based authentication or any social media based authentication or token based authentication or to include rest framework based authentication, we would be requiring to modify or add the corresponding back-ends so that any of the above scenarios work. Lets consider example of adding rest framework based authentication system. Let’s consider adding DRF(Django Rest Framework) to our django application, firstly we set the default authentication classes and put the appropriate classes as shown:</p><pre>REST_FRAMEWORK = {<br>&quot;DEFAULT_AUTHENTICATION_CLASSES&quot;: (<br>&quot;rest_framework_simplejwt.authentication.JWTAuthentication&quot;,),<br>&quot;DEFAULT_PERMISSION_CLASSES&quot;: (&quot;rest_framework.permissions.IsAuthenticated&quot;,),<br>}</pre><p>Here we are using jwt (json web token) based authentication system, where in jwt tokens are issued and verified by the system. We have set the default auth class as jwt, and permission class as isauthenticated. This means that the user have to be authorized first to view the details, send or recieve data from the application. More details can be found <a href="https://django-rest-framework-simplejwt.readthedocs.io/en/latest/">here</a>.</p><p>2. <strong>Authentication with email or username and password</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2v47nGNOAIEV_5CD.jpg" /><figcaption>source: <a href="https://colorlib.com/wp/html5-and-css3-login-forms/">https://colorlib.com/wp/html5-and-css3-login-forms/</a></figcaption></figure><p>Often username and passwords are paired and sent together for authentication, but there could be a need where in the input could be either user name or email and password. In this scenario either username or password is sent as pair and the user needs to be authenticated. Django by default provides authentication when username and password are provided. To handle the above case when either of the username or email are provided, a custom authentication backend can be created. The logic related to authentication can be put in the .py file and the written custom backend can be added to the list of <strong>AUTHENTICATION_BACKENDS</strong> variable present in the settings.</p><p>The following code is a custom backend written for the email based authentication.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68afd404dda3cea056a916110f628e98/href">https://medium.com/media/68afd404dda3cea056a916110f628e98/href</a></iframe><p>As we see from above section, we have a class written for email based backend, where in firstly we try to get the user model and then use it to get the user based on the email. If user is already existing then, the user is retrieved and user is checked if the user is active and the password provided is correct. Once, all conditions are satisfied, then the authorized user is returned. This custom email backend needs to be added in the authentication backend variable(list) in settings.py. By default in our app, ModelBackend is present as one of the authentication backends. We add our custom written email based backend to the authentication backends list.</p><p>We can add our new email based backend to the AUTHENTICATION_BACKENDS list as shown below:</p><pre>Before writing custom backend.<br>AUTHENTICATION_BACKENDS = [“django.contrib.auth.backends.ModelBackend”,]</pre><pre>Including the custom backend.</pre><pre>AUTHENTICATION_BACKENDS = [“django.contrib.auth.backends.ModelBackend”,<br>“app.auth.EmailBasedBackend”,]</pre><p>Here an assumption was made that under app, there is a file auth.py which has EmailBasedBackend class in it as shown above.</p><blockquote>It is to be noted that, we haven’t coded the case when username is provided as input because, Authentication backend, checks each of the backend providedi in it and the first element of <strong>ModelBackend</strong> handles the case when username is provided. In the case when email is provided, <strong>ModelBackend </strong>cannot process the authentication and next present auth backend is considered, i.e. the second in the AUTHENTICATION_BACKENDS list, <strong>EmailBasedBackend</strong> is considered and it processes the authentication request.</blockquote><p>3. <strong>Decoupling sensitive information</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*VX0b7ro73D6k3ttS.png" /><figcaption>source: <a href="https://icon-icons.com/icon/unlink/112462">https://icon-icons.com/icon/unlink/112462</a></figcaption></figure><p>During application deployment, decoupling of sensitive data(information) is an important step. By decoupling we mean, sensitive data like that of database passwords or django app key or any other api related keys, other passwords, etc are not hard coded into the application code as it is not a good practice. It is always good practice to have a environment variable file and load the environment variables from the environment file. There are two benefits of doing this, this first is that any change of variables in the code can be changed just by changing it in the environment file. The second advantage is that multiple environments can be loaded from the same file, making it easy for loading on different machines. And an added advantage is that when there is a necessity in review of the code or by chance the code gets visible to the public, misuse of the passwords and other sensitive information can be minimized. For publicly deployed projects, shared projects, opensource projects, the production deployments or any other deployments can be easily done using their respective env files and having the code visible to the public. And <a href="https://pypi.org/project/python-dotenv/">python-dotenv</a> package comes handy to load environment variables for any application deployments.</p><p>4. <strong>DB and Static file Settings</strong>:</p><p>Database are backbone of any application and it correct configuration is an important step in any application development. Django also called as batteries included frameworks, by default comes with sqlite database when the app is created. But for the real world applications, sqlite might not be ideal chose of database. In this scenario we need to configure database for our application, where postgres or any other sql database can be used by the application. In settings.py file, DATABASES is the key word to be filled with a dict of different settings of the database to be used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Fqc8DA8T6BMvtoFd" /><figcaption>source: <a href="https://www.freepik.com/free-photos-vectors/database-icon">https://www.freepik.com/free-photos-vectors/database-icon</a></figcaption></figure><ul><li>django.db.backends.postgresql&#39;</li><li>&#39;django.db.backends.mysql&#39;</li><li>&#39;django.db.backends.sqlite3&#39;</li><li>&#39;django.db.backends.oracle&#39;</li></ul><p>Are the four backends that are built-in backends from django. Other custom backends can also be provided by providing an full path to the backend. By default, default is the key inside the dict which has all the details for the database, where different details of the DB are provided. But when unit testing or while running the test suite, the default db cannot be be used and if the db is hosted remotely then it is a overhead in terms of communication and time. Here come <strong>TEST </strong>key where in the<strong> </strong>db for tests can be configured and then this db gets loaded when running the test suite. Adding a TEST key with the database details will be very helpful and helps in testing the code developed in less time. Coming to the media settings, generally we have a media server in place which renders in the css, js or static files, but in dev environment, we do need the css, js, media and static files to be tested. In doing so we can configure static files using the static_url key word in settings.py, and having the template tag { <strong>% load static </strong>%} in the django template. <a href="http://whitenoise.evans.io/en/stable/">Whitenoise</a> is another package that can be used to serve static files.</p><p>I hope the above details helps in your projects. And in this blog we have seen how to have a custom authentication backend, email or username based authentication, importance of variables decoupling, and loading the static and media files. Any comments, suggestions or feedback, pls feel free to reach out to me. Thanks for reading, happy day(/night).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af207cf55fb4" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/few-best-settings-and-practices-when-working-with-django-projects-af207cf55fb4">Few best settings and practices when working with Django projects.</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GraphQL in LokiBots:]]></title>
            <link>https://medium.com/lokibots/graphql-in-lokibots-f74120664579?source=rss-52cb448a557d------2</link>
            <guid isPermaLink="false">https://medium.com/p/f74120664579</guid>
            <category><![CDATA[lokibot]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[roboticprocessautomation]]></category>
            <category><![CDATA[graphene]]></category>
            <dc:creator><![CDATA[Gautham Sreeram Dasu]]></dc:creator>
            <pubDate>Mon, 27 Sep 2021 16:44:49 GMT</pubDate>
            <atom:updated>2021-09-27T16:44:49.710Z</atom:updated>
            <content:encoded><![CDATA[<h4>GraphQL has been into the API industry for quiet some time!, We at <strong>LokiBots </strong>also use GraphQL for making our web apps more responsive. Let us, in this blog see how GraphQL is being used at LokiBots and how we can set up for a django based web app with a sample code snippets.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BLTWT-uNXkdVNzG7SnoqGw.jpeg" /></figure><p>GraphQL as the name itself suggests, is a query language open sourced by Facebook has been getting traction in recent times. It gives clients the power to ask for exactly what they need, nothing less or more, which helps in evolving the API’s over the time and enabling powerful developer tools.</p><p>In contrast to the REST API’s, in a single request on a single endpoint many resources can be obtained(queried). With <strong>types </strong>and <strong>fields </strong>defined, graphql ensures that the apps consuming the API’s ask for what is present and possible from the server and related helpful errors. Overhead related to the parsing of the data received can also be reduced on the API consuming apps end. GitHub, Facebook, Intuit, Coursera, Shopify, Pinterest, etc. are some of the companies who use GraphQL in their tech stack. We at LokiBots also use GraphQL in our tech stack to expose a core Django based web application as API’s to be consumed to a React based frontend application.</p><h3>GraphQL in LokiBots:</h3><p>A core component in LokiBots (web app) consisting of the frontend and backend have been decoupled and <strong>ReactJS </strong>is being used in the frontend and graphene-django (django-graphql client for the backend).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kcMp08naqOoZawJ_pHhoiQ.png" /><figcaption>LokiBots web app overview.</figcaption></figure><p>In LokiBots, prior to the current tech stack, Django’s default template engine was used for rendering the frontend (templates). As the platform evolved, we have decided to decouple the FE and BE and have a single endpoint for the communication from the FE to the BE.</p><h3>Let’s go into some practicals:</h3><p>So let’s take a simple use case and see how we can use django-graphene to expose the app as graphql API’s. Consider a situation where we need to get a list of all active users using our web application. Let us see how we can handle this situation in a standalone django way, and using the graphene-django way.</p><h4>The code snippet which will be common for all the three situations is as follows:</h4><pre># Assuming that django inbuilt user model is being used in our application.</pre><pre>from django.contrib.auth.models import User</pre><pre># list all the active users using our web app:</pre><pre>active_users = User.objects.filter(active=True).all()</pre><p>Let us see how the view for the request of <strong>get all users </strong>can be written:</p><pre>def get_all_users(request):<br> active_users = User.objects.filter(active=True).all()</pre><pre>return render(request, ‘name_of_template’, {‘users’: active_users})</pre><p>Here we have filtered the active users and returned the filtered users along with the template as a response so that the returned values are rendered as required in the template. Here the FE and the BE are coupled so the data along with the template are returned as response to the request made.</p><p>But when we are exposing our django app using graphene-django for a third party decoupled FE app, we send the response (data) that is asked for (requested) by the FE app. Considering the above case, and assuming there could be a requirement of having all the active users of our django web app, then let’s see how we can proceed to solve this problem.</p><p>As mentioned earlier, there are types and fields that needs to be defined in the back end so that the FE app asks for what is existing and possible. In our scenario we are dealing with the active users and in particular with the User model. So for the FE client to request for active users, we first need to define a type for the user. So we can call them as UserType. By defining the type, we make sure that the type is available and queries or mutations on this type are possible. Before we go into the implementation details, let’s go brief about the packages used. Since we want to expose a django application, we can use the graphene-django package, which will be very helpful for exposing our django application. Details on how to add graphene-django are given in this <a href="https://docs.graphene-python.org/projects/django/en/latest/installation/">link</a>. The idea is simple, install the package, add the installed package to the installed apps list in the settings, add the graphql <strong>URL </strong>(end point) to the urls of the django app, and add the location of a schema file, which contains all queries and the mutations. After following these steps, we see that on running the server and visiting the <strong>IP</strong> of the development server and visiting graphiql, we see an interactive version of the graphql, where the documentation and all the related information about the graphql schema is available on that page.</p><p>A <strong><em>schema.py</em></strong> file needs to be created and the location of the file needs to be specified in the settings of the django application. For example if we have created a schema file in the an app in the project. Suppose say there is an app with name test_graphql and it contains the schema file, with the following queries and mutations,</p><pre>#project/test_graphql/schema.py<br>from graphene import Schema</pre><pre>class Query(ObjectType):<br>    pass</pre><pre>class Mutation(ObjectType):<br>    pass</pre><pre>schema = Schema(query=Query, mutation=Mutation)</pre><p>then in our settings.py file or the file containing all the settings, we need to add the location for graphene as follows:</p><pre>#project/project/settings.py<br>GRAPHENE = {‘SCHEMA’: ‘test_graphql.schema.schema’,}</pre><p>Where inside the dict we specified the schema instance instantiated with the query and mutation.</p><p>For each of the django models we can subclass the <strong>DjangoObjectType </strong>class, which automatically defines GraphQL fields corresponding to the fields of the django model. For the user model the <strong><em>schema.py</em></strong> file should look something like this</p><pre>import graphene<br>from graphene_django import DjangoObjectType<br>from django.contrib.auth.models import User</pre><pre>class UserType(DjangoObjectType):<br>    class Meta:<br>    model = User<br>    fields = “__all__” </pre><pre># we can specify tuple consisting the #fields of our interest according to the model or we can specify all the #fields of the model.</pre><pre>class Query(graphene.ObjectType):<br>    all_users = graphene.List(UserType)</pre><pre>    def resolve_all_users(root, info):<br>         return User.objects.filter(active=True).all()</pre><p>As we see from the above query, we are first defining the type of the field respective query returns, all_users is a list of UserType objects type. And we define a resolver to resolve this query as and when made by the client. So for each of the query, there will be a resolver function with name resolver appended in the front of the query to respond to the request made by the client.</p><p>The scenario is very simple and small compared to the scenario and the requirement we handle in LokiBots. At LokiBots graphql is being used for different components of the react based Front End app, which make queries and mutations for managing the users in the organization, automation of workflows and bots, subscription to the platform, publishing of bots to different bot stores, sharing bots, client support and many more.</p><p>Want to know more about <strong>LokiBots, </strong>and interested to start your <strong>RPA </strong>journey? Reach out to us at Sales@LokiBots.com.</p><p>For more details, visit <a href="https://www.lokibots.ai/"><strong>https://www.lokibots.ai/</strong></a><strong>.</strong></p><p>Few open GraphQL API endpoints for testing purposes and getting ourselves comfortable with queries and mutations:</p><ol><li><a href="https://api.spacex.land/graphql/">https://api.spacex.land/graphql/</a></li><li><a href="https://graphql.org/swapi-graphql">https://graphql.org/swapi-graphql</a></li><li><a href="https://developer.github.com/v4/explorer/">https://developer.github.com/v4/explorer/</a></li><li><a href="https://rickandmortyapi.com/graphql">https://rickandmortyapi.com/graphql</a></li><li><a href="https://www.onegraph.com/graphiql">https://www.onegraph.com/graphiql</a></li><li>And a comprehensive list <a href="https://github.com/APIs-guru/graphql-apis">here</a></li></ol><h3>About the author</h3><p>Gautham Sreeram Dasu is currently working as a Software Engineer at LokiBots. He predominantly works in the backend of LokiBots with tech stacks which include python and django.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f74120664579" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lokibots/graphql-in-lokibots-f74120664579">GraphQL in LokiBots:</a> was originally published in <a href="https://medium.com/lokibots">lokibots</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Let’s add QR feature for our Django application!]]></title>
            <link>https://medium.com/codex/lets-add-qr-feature-for-our-django-application-2cee136503b8?source=rss-52cb448a557d------2</link>
            <guid isPermaLink="false">https://medium.com/p/2cee136503b8</guid>
            <category><![CDATA[django]]></category>
            <category><![CDATA[features]]></category>
            <category><![CDATA[django-app]]></category>
            <category><![CDATA[qr]]></category>
            <category><![CDATA[qr-code]]></category>
            <dc:creator><![CDATA[Gautham Sreeram Dasu]]></dc:creator>
            <pubDate>Fri, 27 Aug 2021 09:46:47 GMT</pubDate>
            <atom:updated>2021-08-29T03:28:41.279Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OODSUKQPx4B1hax1" /></figure><p>Recently I was in a restaurant where I was told to scan a qr code and get the menu on my mobile. When I came back home I just wondered how we can add it as a feature or build one which replicates the same in any of the existing projects or new projects. I thought of giving it a try and just made a simple qr code generator and how to add the qr feature in your Django-powered application!</p><p>The mobile phone has become a must in our day-to-day life and is quite useful as it is the simplest means for communication, sharing, and creation of data. One simple way of sharing data is by using QR. We often do see in restaurants, on the dining tables, a small board with QR code embedded and asking us to scan the code for getting the menu. There could be multiple reasons for this setup and what reasons I have found is like following the norms of COVID, which includes no contact of the menu card as it will be touched by many people in the restaurant, also once the QR code is scanned redirects to a webpage, which possibly can give some details about the user using, and also there could be possible chances of the user reopening the menu and a possibility of ordering from the same restaurant.</p><h3>Let’s now dive into the details of the blog:</h3><p>The idea of the blog is simple, to have a django app, an html page with the details of our restaurant, and a corresponding view for the same in place. By making a QR code for that URL and render in our app, we are done. We share can the embedded qr and try using a QR reader and test it. I’ve used Django, we can use <strong>flask</strong> or any other framework of our choice, but the idea will be the same across the frameworks. The overview of the idea can be seen in the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*U74rkT28JOBNPLFr" /></figure><p>The following packages are used for building the application:</p><pre># django <br>pip install django <br># pyqrcode <br>pip install pyqrcode <br># pypng <br>pip install pypng</pre><p>First let’s see the logic to use pyqrcode, the logic is simple, we provide the URL to be embedded as an input to the method <strong>create</strong> in pyqrcode and use the png package to save the embedded QR as png.</p><pre>from pyqrcode import create import png<br>def embed_QR(url_input, location_name): <br>    embedded_qr = create(url_input)<br>    embedded_qr.png(location_name, scale=7)</pre><p>We have a method in place for generating the qr code, let’s now see how we can integrate to our Django app. There are multiple tutorials on how we can <strong>setup</strong> our Django app, but I am not going to touch on the setup, assuming we have django app in place and let’s see how we can use the qr code for the same.</p><p>We can simplify our use case to only render an already existing view and embed the code in the django project. Suppose say, <a href="https://myblog.com/menu">myblog.com/menu</a> is already an existing view of our django app, we embed this URL as QR code and return to the landing page of our app or save and distribute. Since we are developing the app locally, we can embed the url as something as following <a href="http://ip_of_your_computer:8000/menu">ip_of_your_computer:8000/menu</a> in the qr. The saved image can be put as an image embedded in our landing page.</p><p>We should make sure that the device which we are using to test the QR, the server are on the same subnetwork and the server is listening to all the ip’s of the subnet.</p><p>Okay so let’s go into some coding now!!!.</p><p>We have already the method for generating QR in place, also assuming we have django app in place which renders menu for the path <strong>menu/</strong>, if not then we can add, a URL in urls.py which actually does it. So in our django app(assuming an app existing in our project), open urls.py(if not existing create one and add the urls to urls.py in the project) and add the following line</p><p>this line communicates to the django server that if any request related to the menu is received then redirect to the view related to the menu present in the views.py.</p><p>In views.py we can have a simple view something as following:</p><pre>menu_items = some_menu_model.objects.get() return render(request, &#39;menu.html&#39;, {&#39;menu&#39;: menu_items})</pre><p>We see that in the above view, for the request obtained, we get all the items of the menu, assuming a model for the menu exists, and then render a menu.html with menu items into it.</p><p>So when a user(to whom we have shared the qr code, scans the qr code gets redirected to the URL of the menu and this view is rendered which has all the menu items.</p><p>That’s it, it is very simple! Give it a try and let me know if it is working in the comments below :).</p><p>Few checkpoints to be made before we running the app:</p><ol><li>Make sure all the required packages are installed.</li><li>Make sure the server and the testing devices are in the same subnetwork so that the testing device browser on opening the URL redirects our request to the server running locally.</li><li>Also make sure that you the url should have the ip address of the computer used as server rather than local host. Say 192.178.0.12 is ip of your computer then redirect url for the view of menu can be something like <a href="http://192.178.0.12/menu">192.178.0.12/menu</a>.</li></ol><p>That’s it !!! we have embedded a simple qr feature into our django app. Cheers!!!!</p><p>We can improve upon, by having qr for the features of inviting new users to your web app, or some personalized things for the user in the web app or any other feature!</p><p>I’ve uploaded the code on my github repo. <a href="https://github.com/gauthamdasu/Mini-project-blog-codes/tree/master/QR_App">This</a> is the link for the repo.</p><p>We can make our project more generic and rename it to qr generating app. Let us see how we can make it happen. We take input of the url and then embedded the url and return the qr code to the view and boom that’s it we made our own QR generator app!!!!. Few screenshots for the same:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/857/0*8IqsgM1cCXBwti1y" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/0*5NunWUj9Gf3rvh9W" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OnjzEJP8CZq9RDdg" /></figure><p>I have included the same in the github project, feel free to go through it and give it a try. Cheers.</p><p>I have made a small video of the demo for the same, hope you find it useful:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FFB_Fbd_CfF8%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DFB_Fbd_CfF8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FFB_Fbd_CfF8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/29377a6fffa8ce159310689944f4477b/href">https://medium.com/media/29377a6fffa8ce159310689944f4477b/href</a></iframe><p>I hope this article could be useful to someone who is reading the article, I am open to any comments, suggestions, feedback and clarifications. You can encourage me by <a href="https://www.buymeacoffee.com/gautham.dasu">buying me a coffee</a> for writing me more such content. Thanks and have a nice day!!!</p><p><em>Originally published at </em><a href="https://gauthamdasu.hashnode.dev/lets-add-qr-feature-for-our-django-applications"><em>https://gauthamdasu.hashnode.dev</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2cee136503b8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/lets-add-qr-feature-for-our-django-application-2cee136503b8">Let’s add QR feature for our Django application!</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[7 quick useful techniques in Django]]></title>
            <link>https://medium.com/codex/7-quick-useful-techniques-in-django-29fc201aea78?source=rss-52cb448a557d------2</link>
            <guid isPermaLink="false">https://medium.com/p/29fc201aea78</guid>
            <category><![CDATA[techniques]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[django-filter]]></category>
            <category><![CDATA[filtering]]></category>
            <dc:creator><![CDATA[Gautham Sreeram Dasu]]></dc:creator>
            <pubDate>Thu, 08 Jul 2021 16:08:47 GMT</pubDate>
            <atom:updated>2021-07-11T16:02:14.621Z</atom:updated>
            <content:encoded><![CDATA[<h4>Lets see 7 quick useful techniques in django which can be helpful and save our time!!!!</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3jx8Dnv6uOumJB7f" /><figcaption>Photo by <a href="https://unsplash.com/@tata186?utm_source=medium&amp;utm_medium=referral">Tatiana Rodriguez</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>As it is in the official website, Django is an web framework for <strong>perfectionists with deadlines</strong>, not going deep into the statement, django is a powerful web frame work for rapid web development. Coming to the point of the blog, lets see 7 useful techniques which can help us in solving some of our problems related to django and some times could be time saving as well.</p><p>The following are the categories which the techniques will be addressed:</p><ol><li>Filters on Query sets</li><li>User related object creation</li><li>Different ways of objects reference</li></ol><h4>Okay so let do a dive into the techniques now 😃,</h4><h3>1. Filter from a input list of values:</h3><ol><li>We see how we can filter objects when an list of input parameters given:</li></ol><p>Lets say we have User model and the respective user objects in our database. We can import the User model from the inbuilt django auth model as following:</p><pre>from django.contrib.auth.models import User</pre><p>Now lets say we are given an <strong>input list of values</strong>, based on which the users needs to be filtered, for example we might want to filter the <strong>users </strong>based on a list of <strong>user ids </strong>or username or first_name or anything else, for simplicity lets suppose that we have a list of <strong>active users </strong>as following:</p><pre>#list of active users.<br>active_users_list = [1,2,55,21,63,76,88]</pre><p>we want only the user objects with these user ids(which are active user objects), this can be achieved using the following approach:</p><pre>#list of active users.<br><strong>active_users_list </strong>= [1,2,55,21,63,76,88]<br># we are interested in filtering only the active users from all the users, so we can do the following:<br><strong>active_users</strong> = User.objects.filter(id__in=active_users_list)</pre><p>So <strong>active_users</strong> gives a query set of active users based on the input filter. So for getting an generic way of filter for a list of values can be as following</p><pre>#Model Posts be the a dummy model with name, user etc<br>value_list = [val1, val2, val3, val4]    #input list<br>filtered_value = Posts.objects.filter(Field__in=value_list)</pre><h3>2. FAQ — F And Q expressions, filter on query sets:</h3><p>F expressions, suppose we have a situation where we need to compare the value of one field on a model with another field in the same model, then F expressions come into play.</p><pre># We can import F expressions as following<br>from django.db.models import <strong>F</strong></pre><p>Suppose there is social media application and there are posts, lets define model for post as following:</p><pre>class post(models.Model):<br>    image = imageField() ##dummy one<br>    likes = int field<br>    shares = int field</pre><p>We can may be define a simple condition like make a post <strong>trending </strong>if number of <strong>likes </strong>are more than <strong>shares </strong>or something like that.<strong> </strong>To achieve that we can use the F expression.</p><pre># we have already imported F expression, to get the posts which are trending, we have the following filter:<br><strong>trending_post </strong>= posts.objects.filter(likes__gt=F(&#39;shares&#39;)))</pre><h3>3. Foreign key (other table) object(s) reference from single object(one object) (Many to one relation):</h3><p>For defining <strong>many to one relation</strong>, we use <strong>foreign key </strong>in django models, which means an entry in a table is related to more than one tables in the database. Accessing of the related elements by foreign key can be done in many ways, one of the ways is to call the model it self and filter the required objects with different parameters. Another simple method can be like direct calling from the model which is related as foreign key.</p><p>Lets see it though an example, say we have model <strong>blog, </strong>with fields of description, title and author. In general we see that there are more than one blog per user, so we can define <strong>User </strong>as foreign key to blog model. Assuming we have imported User model as before, we can define the blog model as following:</p><pre>from django.contrib.auth.models import User<br>from django.db import models</pre><pre>class blog(models.Model):<br>    title = models.CharField(max_length=100)<br>    description = models.TextField(null=True, blank=True)<br>    user = models.ForeignKey(User, related_name=&#39;blogs&#39;,        on_delete=models.CASCADE)</pre><p>User is foreign key to the blog model, because a user can write more than one blog. So lets see how to reference of blogs of a user can be done. We can do it in two ways, one way is to filter all the blogs with user as shown below:</p><pre>from .models import blog</pre><pre>def user_blogs(user):<br>    user_blogs = blog.objects.filter(user=user)<br>    return user_blogs</pre><p>The second way to reference the user blogs directly from user itself using the related name provided in the blog model.</p><pre>#assuming user is user object of some user<br>.... code before<br>user = User.objects.get(pk=1)   # assuming user object with 1 exits.<br>user_blogs = user.blogs.all()   #gives all the blog object</pre><p>The reference of foreign key objects will be useful in cases where we can fine grain the access of the objects related to the user. For example as shown above, we can try removing a check whether the objects belong to the user or not, like the user’s (with user_id = 1) blogs, are accessed only by that user and not others directly.</p><h3>4. Or operation on query sets:</h3><p>We often have the requirement of filtering the query sets based on different criteria and then have a logical operations upon the query sets. For example lets take posts model as we have defined previously and has foreign key to another model say likes, suppose say we have a requirement where we require all the posts with a particular “title” or has a count of likes &gt; some number, then in a simple way we can first filter our model objects based on the title and again have another model objects filtered based on the count and have an or operation on it.</p><pre>from django.contrib.auth.models import User<br>from django.db import models</pre><pre>class blog(models.Model):<br>    title = models.CharField(max_length=100)<br>    description = models.TextField(null=True, blank=True)<br>    user = models.ForeignKey(User, related_name=&#39;blogs&#39;,        on_delete=models.CASCADE)<br>    likes = models.ForeignKey(likes, related_name=&#39;blogs&#39;,                 on_delete=models.CASCADE)</pre><pre>class likes(models.Model):<br>    count = models.IntegerField()<br>    <br>#Suppose blog has title as filed, and likes as foreign key then,<br>Total_blogs_with_title=&quot;title&quot;_or_has_likes&gt;500 can be done as following:</pre><pre>from django.db.models import Max, Q<br>blogs_with_title = blog.objects.filter(title=&quot;title&quot;)<br>blogs_with_count = blog.objects.filter(Q(likes_count&gt;500))<br>Total_blogs = (blogs_with_title | blogs_with_count).distinct()</pre><p>We can have the “<strong>and ” </strong>operations included in the filter itself as following:</p><pre>Total_blogs= blog.objects.filter(title=&quot;title&quot;, Q(likes_count&gt;500))</pre><h3>5. User creation in django:</h3><p>The best part about django is it that user management is already inbuilt in its system, we can either extend it or use the existing ones. Often when model object are created we tend to use nameofmodel.object.create(parameters). But coming to the user model it is little different, we need to user create_user method rather than create( ) method. For more details we can refer to <a href="https://stackoverflow.com/questions/11544398/user-manager-methods-create-and-create-user">this </a>answer on stackoverflow.</p><p>So to create new user we can do it the following way:</p><pre>from django.contrib.auth.models import User</pre><pre>credentials = {&#39;usernam&#39;: &#39;name_of_the_user&#39;,<br>               &#39;password&#39;: &#39;password_provided&#39;,<br>               &#39;email&#39;: &#39;mailid.provided&#39;.}<br>User.objects.create_user(**credentials)</pre><h3>6. Using filter and first( ) instead of get:</h3><p>Often we tend to use .get( ) method on the model objects to fetch an object, what I could observer is that we get a particular object using model.object.get(id=id) or with some other parameters inside. But in some cases or while writing unit tests, when we tend to access the elements by get method or by index. If there are elements present then we wont have any problem, but incase if the elements are deleted by chance or when in a later stage of when we try to access an element by either their ids or any other fields, our program may crash. To avoid such situations and have a failsafe mechanism we can use filter and first methods. We can do it as following:</p><pre>#using get method. assuming user with id-6 exists<br>user = User.objects.get(pk=6)</pre><pre>#using filter and first methods<br>user = User.objects.filter(pk=6).first()</pre><p>In the first case if by chance the user object with id 6 is not present we get an error saying there are no objects with the id 6, but in the second case the filter will give an empty query set and first method on it will give None value and does not crash our program.</p><h3>7. Fake Migrations:</h3><p>Fake migrations comes into play when data already exits in the table but the it appears like migration is not applied. We can see the migrations applied using the following command</p><pre>python manage.py showmigrations</pre><p>This could possibly happen when the databases are shared among the developers or when database dumps are present and want to populate our db, when we fake migrations, no change in database schema is done but an entry in the database is created.</p><p>We give <strong>— fake </strong>flag to the migrate command.</p><pre>python .\manage.py migrate --fake #fake all the migrations.</pre><p>Fake initial is another flag used when we are not able apply initial migration because table already exits in the database. In this case we can fake the initial migration using <strong>— fake-initial </strong>flag in the migrate command.</p><p>There are many other useful commands present in django which saves time and provides a way to solve the problems, like get_object_or_404, transaction.atomic etc, which will be very useful. Thank you for your patient reading. Hope it might be useful. Thanks again. 😄</p><p>Thanks once again for reading the article and if you feel like supporting me you can do by buying <a href="https://www.buymeacoffee.com/gautham.dasu">me a coffee</a>, it keeps me motivated to improve upon my skills and provide the best possible content.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=29fc201aea78" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/7-quick-useful-techniques-in-django-29fc201aea78">7 quick useful techniques in Django</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to make a To Do app in Python Django]]></title>
            <link>https://python.plainenglish.io/how-to-make-a-simple-to-do-app-in-python-django-ead5b35b9d98?source=rss-52cb448a557d------2</link>
            <guid isPermaLink="false">https://medium.com/p/ead5b35b9d98</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[django]]></category>
            <dc:creator><![CDATA[Gautham Sreeram Dasu]]></dc:creator>
            <pubDate>Tue, 26 Jan 2021 13:30:42 GMT</pubDate>
            <atom:updated>2021-08-30T10:48:08.813Z</atom:updated>
            <content:encoded><![CDATA[<h4>A simple guide that will get you up and running with Django</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YSisfB1Sdt_69M55" /><figcaption>Photo by <a href="https://unsplash.com/@gautham_dasu?utm_source=medium&amp;utm_medium=referral">Gautham Sreeram Dasu</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>To give a brief intro about <strong>Django</strong>, it’s a web framework for perfectionists with deadlines and makes it easy for developers for making web applications. It is ridiculously fast, secure and scalable. More information can be found <a href="https://www.djangoproject.com/">here</a>. The framework is based on MVT (Model, Views and Templates) software principle, meaning we have a database model, the respective views for the application, and templates to render or intake the data ( over simplified description), more information can be found <a href="https://www.javatpoint.com/django-mvt">here</a>.</p><p>Coming to the to do app, the idea is simple, we have a database that stores the things (to be done), we tell it to keep and then we filter the list of undone things from it and then display only the list of them onto the template of the app.</p><p>So firstly we need to design of our database. Since we are dealing with a sample app, let’s keep the db also simple. Let’s say we are good to go if we have two fields, one for storing the thing which we are going to do and then the status of the thing we stored. So having a text field and boolean field in a model should suffice for our problem. Okay since our model is done, let’s start to work on the project setup and other things. We have good number of tutorial for setting up the Django tutorials so I am not going to in details bout it, but will give an outline. Say the name of the app we are working on be ‘todolist’. So to make this first we need to give the following commands:</p><pre>django-admin startproject todolist   # setup the django project<br>cd todolist                          # go to that directory</pre><p>Once we are ready with the project, we need to have a core application which does all the things required for our application. So let’s create a core application in the todolist app.</p><pre>python manage.py startapp core  # create core application</pre><p>Now we add the app “core” to the list of installed app in the <strong>settings.py</strong> file in the tololist folder. Next we run the following commands about the migration for starting our local db.</p><pre>python manage.py makemigrations       # make the migration files<br>python manage.py migrate              # create tables in the db </pre><p>So now we have setup our project. So we need to test the project till now. Run the command python manage.py runserver<strong> </strong>and if Django and everything is properly set, we get the default Django screen in the browser on visiting <a href="http://127.0.0.1:8000/">http://127.0.0.1:8000</a>.</p><p>Okay going forward about the application, the outline of the project should look similar to that as following:</p><pre>│   manage.py<br>│   <br>├───Core                        # Core App<br>│   │   admin.py<br>│   │   apps.py<br>│   │   models.py<br>│   │   tests.py<br>│   │   views.py<br>│   │   __init__.py<br>│   ├───migrations<br>├───static                     # Static sources for js and css<br>│   ├───css<br>│   └───js<br>├───templates                  # Templates for rendering data<br>│       base.html<br>└───TodoList                   # Default app created by django<br>    │   asgi.py<br>    │   settings.py<br>    │   urls.py<br>    │   wsgi.py<br>    │   __init__.py<br>    └───__pycache__</pre><p>We have static and template directories made, where the css and the html templates put for rendering for different views in our application.</p><p>Since we have modeled our data base, let’s code it, so we have only two fields, one is boolean field and another is text field.</p><p>So in <strong>models.py</strong> in core application, let’s write the following code for the model and we name it as todo.</p><pre>from django.db import models       # Core/models.py</pre><pre>class todo(models.Model):</pre><pre>    name = models.TextField(max_length=255)<br>    status = models.BooleanField(default=False)</pre><p>Since we have made our model, lets make db know that we have added an table. So running the commands for migration,</p><pre>python manage.py makemigrations       # make the migration files<br>python manage.py migrate              # create tables in the db</pre><p>We see that the migration file would have been created and then changes would have been made in the database.</p><p>We can also create a super user and add the model in the admin page as well. Running the command of</p><pre>python manage.py createsuperuser</pre><p>And providing the required details we get the super user created. To register the model we created in the admin page, lets add the following lines in admin.py in the Core app.</p><pre>from django.contrib import admin</pre><pre>from .models import todo</pre><pre>admin.site.register(todo)</pre><p>We have registered our model in the admin page. To test, let’s visit “<a href="http://127.0.0.1:8000/admin">http:127.0.0.1:8000/admin</a>”. We enter the credentials used when we created a super user command line and then login to the admin console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/1*pSKUuT5WG7yJbMgue95naQ.png" /><figcaption>Django Admin Console</figcaption></figure><p>Now we will see the screen with the model of todo under Core app and on clicking we can create new todo objects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*kQWvWrueXqyXga8L2TFzhA.png" /><figcaption>We can see the todo model under the CORE app.</figcaption></figure><p>We can click on the Todos and add a new object as shown below by providing the details for the name of the todo and by clicking save button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RSvkOaopaNQI2rv7fCN_Gw.png" /></figure><p>Okay till now we have created a model and then migrated our model to create fields in the database and then populated the database with some sample data. Next we give the views for different things that happen on our application.</p><p>The idea is that the user should be able to see the to do things, add new todo things and mark already done todo as read. For these three operations we have three different views in our application. The first one is <strong>index </strong>which renders all the undone tasks by us. The next view is <strong>new_to_do </strong>view, which actually allows the user to add a new one to the list and finally we have <strong>mark_as_done </strong>which makes a to do object as done.</p><p>The idea of having a boolean field in the model for todo is simple. We consider a todo task as done if the status of the task which is a boolean field is true. And by default the task status is false. So for the views of index and new_to_do, we filter the objects based on the status and then render them on the template. So the person who is using the app will have only the list of to do tasks which are not done. Coming to the view related to mark_as_done, we send the the id of the task that is done from the template and then retrieve the object and make that task status field as True.</p><p>Okay now coming to the template, we have only one template named base.html, which is rendered and it would look something like this at the end of the app. It also has dark mode enabled 😅 and a couple of emojis 😃 😄 👊.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wkd54cP_jU8IoOLHnGhsYA.png" /><figcaption>Screen showing the app in one place.</figcaption></figure><p>We can see that there are empty list under the to do list initially and there is a provision for making a new ones.</p><p>The flow of the app is something like this, first we enter the task to be done and click enter or press on the button, here a post request is made to the server and then the server receives the request, takes the data embedded in the request and then makes a new object and again renders the template back.</p><p>A sample of the app is shown as following, where we display the list of to do tasks made and a button to mark them as read. On click of the button, the view mark_as_done is called and then the task is marked as read.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VY1CDMVq6vP4HZznCTwHaQ.png" /><figcaption>Scree of our app in working.</figcaption></figure><p>Okay let’s see the code related to the views. As we discussed we have three views, index, mark_as_done and new_todo.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c9b46ea04fec05ffe6f32a7f3b80230/href">https://medium.com/media/9c9b46ea04fec05ffe6f32a7f3b80230/href</a></iframe><p>So in views of index we are filtering the all the object which are not read(status=False) and rendering to the template of base.html. For the view of new_todo, we have the a post request coming for creation of new object and we create a new one with the details obtained from the request and then again filter and render back to base.html. Finally mark_as_done is simple in the sense, where the id of the todo object is sent and is retrieved and its status is made as true making it as done.</p><p>Next lets see the template we are rendering:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17bf6592e2f580493b52382abf2e3f18/href">https://medium.com/media/17bf6592e2f580493b52382abf2e3f18/href</a></iframe><p>We have two important places from lines 35–50 where the list of undone tasks are displayed and each task having an id, which is used in making the task as read. Line 44 we send the id of the task sent along to the url and on the view the task is marked as read. And lines 51 to 64 we have another important place, where a post request is made and is sent. Line 58–62 covers this aspect of sending the data using the forms and the request is handled by the view of new_todo. Forgot to mention, a button for dark mode was also enabled, and on click of the button, each elements which are white will be turned dark and vice versa.</p><p>I am attaching a screen recording for the same here. We can see the working of the app end to end.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FoSHDhhNu6WI%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DoSHDhhNu6WI&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FoSHDhhNu6WI%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d71fe4945bbfceab4f9b49567eddda02/href">https://medium.com/media/d71fe4945bbfceab4f9b49567eddda02/href</a></iframe><p>So that’s it we have a made a to-do app for our selves. Hurray 🙌 .</p><p>All the code is uploaded in GitHub and this is the <a href="https://github.com/gauthamdasu/Mini-project-blog-codes/tree/master/TodoList">link</a> for the code.</p><p>Any comments, suggestions, clarifications, feedback or anything else I am ready to receive and try my best to help.</p><p>Thanks for reading.</p><p>I have written another <a href="https://gauthamdasu.medium.com/how-to-add-notifications-to-django-app-74df1dac984e">blog</a>, regarding adding notifications like that of social media application to Django application. If you are interested please go through it as well. Thank you for your time.</p><p>Thanks once again for reading the article and if you feel like supporting me you can do by buying <a href="https://www.buymeacoffee.com/gautham.dasu">me a coffee</a>, it keeps me motivated to improve upon my skills and provide the best possible content.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ead5b35b9d98" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/how-to-make-a-simple-to-do-app-in-python-django-ead5b35b9d98">How to make a To Do app in Python Django</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to add notifications to Django app.]]></title>
            <link>https://medium.com/star-gazers/how-to-add-notifications-to-django-app-74df1dac984e?source=rss-52cb448a557d------2</link>
            <guid isPermaLink="false">https://medium.com/p/74df1dac984e</guid>
            <category><![CDATA[notifications]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[social-media]]></category>
            <dc:creator><![CDATA[Gautham Sreeram Dasu]]></dc:creator>
            <pubDate>Fri, 15 Jan 2021 18:02:28 GMT</pubDate>
            <atom:updated>2021-07-11T16:00:26.621Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to add social media like notifications to a Django app.</h3><p>Hi there, in this blog lets see how we can try adding notifications, like that of any social media applications to a django powered app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_kjkG-DV4sv0MFN0" /><figcaption>Photo by <a href="https://unsplash.com/@jamie452?utm_source=medium&amp;utm_medium=referral">Jamie Street</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>So not taking much of your time lets dive into the idea of the app. The idea is to have list of <strong>users</strong> displayed on the screen and only logged-in users can send a message to other users visible on the screen. Now we try adding notifications feature to this app. Per say when a message is sent from a user to another user then the user must be notified about the same that an message has arrived.</p><p>Going into the details of the app, we use <a href="https://pypi.org/project/django-notifications-hq/">django-notifications-hq</a> package for notifications. You can go through different tutorials on how to setup the virtual environment and setting django project without creating any problem with the global python present in the system. I suggest <a href="https://simpleisbetterthancomplex.com/series/beginners-guide/1.11/">this </a>blog which has series of blogs which gives an hands on experience and a better overview about django.</p><p>The final screen of the app is shown as below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YbCAnFTOsmzbegGWQvlnrQ.png" /><figcaption>Final screen of the app</figcaption></figure><p>We can see that there is a bell icon present in the navbar and also if the user is not authenticated(logged in) then name is changed to guest user. Also the list of all users are shown along with a text box for filling the message and sending the message to the user.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SWAA8ZgZzQySTa0WkfzceA.png" /></figure><p>Three dummy users have been created and all the users were given <strong>staff</strong> <strong>permissions </strong>so that they can login from the admin page itself of our simple app, and we need not write an authentication app again for the <strong>login </strong>and <strong>logout </strong>sessions of the user.</p><p>Say we are logged in with the credentials of user1 the screen would look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E656d6EF12-E460Yz2havw.png" /></figure><p>We can see only the users other than the user logged in are present and the list of unread notifications is seen on top of bell icon and also the name of the user logged in.</p><p>Note: We use the django <strong>admin </strong>site for logging in to the app and then again redirecting to the index site.</p><p>A sample demo video of our app can be seen as following:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FTkfSYEHaahc%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DTkfSYEHaahc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FTkfSYEHaahc%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/837834737b91fb471081c876adf2e945/href">https://medium.com/media/837834737b91fb471081c876adf2e945/href</a></iframe><p>The idea is simple we have the notification tags enable for in the templates of our django app and there is a <strong>{% live_notify_badge %}</strong> which gives count of unread notifications present for a user in an app and also it comes inbuilt with the <strong>django-notification-hq</strong> package and it pings the server for every fixed amount of time(say 5 seconds) and it gets updated on the template rendered. So in our case we have the bell icon on the navigation bar and the live notification badge is embedded into the template and is placed in the circle above the bell button. So when any new notification arrives the live notify badge pings the server and updates the count of the unread notifications.</p><p>For our simple app there are only two methods which is an index method which renders list of all the users and the logged in user for sending the message. Another method deals with the sending of notification from one user to another with the message and the type of the message.</p><pre>from notifications.signals import notify</pre><pre>sender = User.objects.get(username=request.user)<br>recipient = User.objects.get(id=user_id)<br>message = &quot;This is an simple message&quot;<br>notify.send(actor=sender, recipient=recipient, verb=&#39;Message&#39;,<br>description=message)</pre><p>This is how we send a message or notification from one user to another. So in the view we have the list of sender name, and the we get the recipient id and the message to be sent and using the above snippet of code we can send messages from one user to one another user.</p><p>On click of the bell icon, the list of notifications of the user appears as a drop-down menu. Further we can make improvements like on click notifications vanish and marking notification as read on click etc, but since our app is a basic version, we display the notifications for each of the user.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10e0e10c8ff0bd2746a3e987237a68fc/href">https://medium.com/media/10e0e10c8ff0bd2746a3e987237a68fc/href</a></iframe><p>We see from the code shown in the snippet, that we can access the list of notifications of an user, directly from the user in the template. And for each of the notification in the list of notifications, we are displaying the information of the sender, the recipient and the message.</p><p>We can see an sample drop down messages, which appear on click of the bell icon.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/275/1*Ax1t_mHUkL6dhqGuPEychg.png" /><figcaption>Drop down notifications</figcaption></figure><p>Now coming to the part of the display of the user and sending the message from one user to another, we will take the help of the following code snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42c819c0f19272d6f831eff3fc8e0b2f/href">https://medium.com/media/42c819c0f19272d6f831eff3fc8e0b2f/href</a></iframe><p>We display the list of users in an table. When this page is rendered, the values of the user and list of users are also sent. In the line 10 we use if condition to check if the logged in user is same as user in the list so that the user cannot send the message to himself. Next we display the user id, user name and then there is the form field which has a text box and an hidden input field for the user_id along with a submit button for each user as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/701/1*WX8zAqx1vyWkjO2RYmQjrw.png" /><figcaption>List of users, text field for message and send message button.</figcaption></figure><p>Finally we see about the two views that are written for our simple app. We again use the code snippet for the same and is shown as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b72eeef1d377e881f32e8e6060dbc93b/href">https://medium.com/media/b72eeef1d377e881f32e8e6060dbc93b/href</a></iframe><p>One is the <strong>index </strong>view, another is <strong>message </strong>view, in the index view we query the list of users present in our database and the request user and is rendered to the template as shown in the line 12 in above code snippet. Since we are not using any other app for authentication, we use try except blocks so that our app breaks gracefully if it has to. And also the user will be notified to login from the admin site to our app if he tries to send the message to another user.</p><p>Coming to the message view, the data that is required for sending the notification taken from the request.POST method and we notify the user as shown in the code and in lines 21 to 23. Once the user is notified, then the user is redirected to the index page again for further communication.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47c0d0dbceadc308af4d1006cd71496a/href">https://medium.com/media/47c0d0dbceadc308af4d1006cd71496a/href</a></iframe><p>We see that lines 9,10 corresponds to the views of index and message, and line 11 is related to the notifications reading. More info can be found in the guide of <a href="https://pypi.org/project/django-notifications-hq/">django-notifications-hq</a>.</p><p>To exit from the application, the use again needs to enter the admin url and then logout from the admin url.</p><p>Yes, so that’s it what i got to say, any suggestions, queries, comments, improvements etc., please feel free to put them in the comments or approach me directly. Thanks for your patience and time for reading the article.</p><p>The codes for the project will be pushed to the git repository and this is the <a href="https://github.com/gauthamdasu/Mini-Projects/tree/master/django%20stuff">link</a> for the repository. Thank you for reading, hope it would help you some time 😃. Thanks once again for reading the article and if you feel like supporting me you can do by buying <a href="https://www.buymeacoffee.com/gautham.dasu">me a coffee</a>, it keeps me motivated to improve upon my skills and provide the best possible content.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74df1dac984e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/star-gazers/how-to-add-notifications-to-django-app-74df1dac984e">How to add notifications to Django app.</a> was originally published in <a href="https://medium.com/star-gazers">Star Gazers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A simple python based reminder app]]></title>
            <link>https://gauthamdasu.medium.com/a-simple-python-based-reminder-app-63ba818c1f33?source=rss-52cb448a557d------2</link>
            <guid isPermaLink="false">https://medium.com/p/63ba818c1f33</guid>
            <category><![CDATA[simple-reminders]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Gautham Sreeram Dasu]]></dc:creator>
            <pubDate>Tue, 04 Aug 2020 14:45:17 GMT</pubDate>
            <atom:updated>2020-08-04T14:45:17.750Z</atom:updated>
            <content:encoded><![CDATA[<p>In this story we shall see a python based app which gives voice and notification reminders to <strong>take</strong> <strong>a break</strong> for relaxing, <strong>drink</strong> <strong>water</strong> and greet the user based on time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*dLqJfMkKF_QEP8YMVGgfyw.jpeg" /><figcaption>Source: My phone gallery</figcaption></figure><p>We see that when we are involved in some work or other we loose track of time and forget to even have the food for both mind and body. Unless someone reminds us to have food or water we forget and neglect to drink water.</p><p>Okay coming to the point, we are going to see a simple python based app to remind us to drink water and take break from our routine for every 35 minutes. Idea is very simple, we use the schedule package available for python and define methods to be called as and when required. The packages required to be installed are notify2, Schedule, espeak.</p><p>The commands for installing the required package are given as following:</p><pre>To install the required packages use :<br><strong><em>1.</em></strong> Notify2    <strong><em>pip3 install notify2<br>2.</em></strong> Schedule   <strong><em>pip3 install schedule<br>3. </em></strong>Espeak     <strong><em>sudo apt-get install espeak</em></strong></pre><p>Let me explain the code embedded below, firstly we import the required modules for our program. We import notify2 package to notify users through a bubble notification, getpass to get the user details, os to execute the espeak commands, schedule to run required methods at required time and the time module.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b8a0290eed5a47692d087f6902a6a99/href">https://medium.com/media/1b8a0290eed5a47692d087f6902a6a99/href</a></iframe><p>We have defined three methods for our simple program, firstly we have defined method related to reminder related to drink water. In the code we can see that based on the user name the message is generated and is executed as a voice command and a notification. Similarly we also have the third method related to taking <strong>break</strong>. We use the schedule package for scheduling the method <strong>take_a_break</strong> to be executed every 35 minutes which in fact reminds us about take a break. Lastly we also have a <strong>wish</strong> method which wishes the user every 8 hours based on the current time and is first executed in our program. This method takes the local time and gets the time in 24 hour format and accordingly prepares the message for the greetings. We can also see that in the code, the main has a while loop which actually does the scheduled pending jobs run.</p><p>More information refer following links :</p><pre><a href="https://pypi.org/project/schedule/">https://pypi.org/project/schedule/</a><br><a href="https://docs.python.org/3/library/sched.html">https://docs.python.org/3/library/sched.html</a><br><a href="https://schedule.readthedocs.io/en/stable/">https://schedule.readthedocs.io/en/stable/</a><br><a href="https://notify2.readthedocs.io/en/latest/_modules/notify2.html">https://notify2.readthedocs.io/en/latest/_modules/notify2.html</a><br><a href="https://notify2.readthedocs.io/en/latest/">https://notify2.readthedocs.io/en/latest/</a></pre><h4>Limitations:</h4><p>This is a simple python based app suitable for ubuntu. Many other additional features and other methods can be incorporated as required and this can be integrated with other modules and a simple <a href="https://en.wikipedia.org/wiki/J.A.R.V.I.S."><strong>jarvis</strong></a>, kind of personal assistant can be made. This project is on the way and will be updated soon.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=63ba818c1f33" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create simple News reader app using pythons Espeak and beautiful soup(bs4).]]></title>
            <link>https://medium.com/swlh/create-simple-news-reader-app-using-pythons-espeak-and-beautiful-soup-bs4-436c2c674b4c?source=rss-52cb448a557d------2</link>
            <guid isPermaLink="false">https://medium.com/p/436c2c674b4c</guid>
            <category><![CDATA[new-reads]]></category>
            <category><![CDATA[web-scraping]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Gautham Sreeram Dasu]]></dc:creator>
            <pubDate>Tue, 10 Sep 2019 05:59:46 GMT</pubDate>
            <atom:updated>2019-09-18T15:39:41.000Z</atom:updated>
            <content:encoded><![CDATA[<h3>Create simple <strong>python based news</strong> reader app(new on voice) using Espeak and beautiful soup(bs4).</h3><h4>In this post we shall see how to create a simple python based news reader application which uses beautiful soup for web scrapping a news page(fixed as of now) and will read the new out using espeak (text to speech) module.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4B9lJuIh1VrBbXbDWz-Qvw.jpeg" /><figcaption>Image Source : My phone gallery</figcaption></figure><p>Web scrapping is the process of extraction of information from the webpages, after reading the <strong>HTML</strong> page and parsing it and getting the information required from the page as structured Data. In this post we used <strong>Beautiful</strong> <strong>Soup</strong> python package for scrapping the web for getting the news. And used <strong>espeak</strong> module for converting the text scrapped to speech and giving it as output. From the below picture we get the outline of web-scraping process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/929/1*W6zjflPwKv9ZMT5gCKnCcw.png" /><figcaption>Model showing how the work flow of web scraping.</figcaption></figure><p>First we should install Beautiful soup and espeak for our program. To install</p><ol><li><strong><em>Beautiful Soup</em></strong> use the following command(s):</li></ol><pre>sudo apt-get <strong>install</strong> python-bs4 (for Python 2)<br>sudo apt-get <strong>install</strong> python3-bs4 (for Python 3)</pre><p>2.<strong> Espeak</strong> use the following command(s)</p><pre>sudo apt-get install espeak python-espeak</pre><p>After installation of the above packages we first import the required packages for our application to run, as we can see below we are importing os, re, sleep from time and urlopen from urllib along with <strong>beautiful soup</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6fdab0aca924df3ccada7f394337d0c/href">https://medium.com/media/e6fdab0aca924df3ccada7f394337d0c/href</a></iframe><p>We want to give notification before reading the text as speech about the news, so we defined a function called as sendmessage which takes input as string ie a message and gives as notification on the screen as below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/967/1*COHbRTrhuUf1PhjIEu2svg.png" /><figcaption>Using sendmessage function to notify the user.</figcaption></figure><p>The sendmessage sends the input message string by spawning a sub-process for <a href="https://www.thegeekstuff.com/2010/12/ubuntu-notify-send/"><strong>notify-send</strong></a><strong> </strong>which takes message as<strong> </strong>input. We can add news icon also if we want.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78a54f6a590a24bf262ca6fb8c993269/href">https://medium.com/media/78a54f6a590a24bf262ca6fb8c993269/href</a></iframe><p>Till now we imported the required packages and defined our send message function. Now the main part of getting the web page and the scrapping of information i.e. news is started. For our case let us consider the news providing site inshorts which provides news in and around 60 words.</p><p>The steps of scrapping is as following:</p><ol><li>Get the html page of news-webpage using urlopen we imported from urllib.</li><li>We create a beautiful soup object for parsing the read html page.</li><li>When we see the html page we see that in the page the div tag with attributes having articleBody as the property of the item has the data we are interested.</li><li>bsObj has the parsed html file we got so we use bsObj method find_all with the item property as articleBody.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2cd57d76a97dc2fc21fcb97e2422c39c/href">https://medium.com/media/2cd57d76a97dc2fc21fcb97e2422c39c/href</a></iframe><p>Next we have the following steps:</p><ol><li>We next go through the found <strong>news</strong> and <strong>encode</strong> that to utf8 format and extract only text and remove double quotes exclamations and other characters other than <strong>“,” </strong>and <strong>“ . ”</strong> using regular expression as shown below.</li><li>We now create the command that gets executed as a system command.</li><li>We have espeak module and the input text news to be read and the speed at which the words are to be read out for the</li></ol><pre>news= &quot;espeak &quot;+ &quot;&#39;&quot;+news+&quot;&#39;&quot;+&quot;  -s 100&quot; </pre><p>4. We execute the command using os.system and the news is read from the inshorts.com.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/942074b6ac8ad9139b0637e394904548/href">https://medium.com/media/942074b6ac8ad9139b0637e394904548/href</a></iframe><p>Hurray we made a simple python based news read using Beautiful Soup and espeak modules. This code is around 35 lines and can be found <a href="https://github.com/gauthamdasu/simple_python_programs/blob/master/web_scrape.py"><strong>here</strong></a>.</p><p>Many modifications can be done to this program such as using other voices available for espeak module, we can put the program in a while loop which after a set time say 60 minutes does again the web scraping process and gives latest news. We can explore other text to speech modules other than espeak and play around with them. We can also get news from other websites like google-news or yahoo or any other news website. Once the html page is read and parsed we can find out the html element which gives the news required and get the regular expression which extracts the text from the scrapped data.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=436c2c674b4c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/create-simple-news-reader-app-using-pythons-espeak-and-beautiful-soup-bs4-436c2c674b4c">Create simple News reader app using pythons Espeak and beautiful soup(bs4).</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Installing Hadoop in  Pseudo Distributed Mode (single node cluster) on Ubuntu system.]]></title>
            <link>https://gauthamdasu.medium.com/installing-hadoop-in-pseudo-distributed-mode-single-node-cluster-on-ubuntu-system-563dbdbc3d24?source=rss-52cb448a557d------2</link>
            <guid isPermaLink="false">https://medium.com/p/563dbdbc3d24</guid>
            <category><![CDATA[hadoop]]></category>
            <dc:creator><![CDATA[Gautham Sreeram Dasu]]></dc:creator>
            <pubDate>Sat, 24 Aug 2019 14:39:17 GMT</pubDate>
            <atom:updated>2019-08-24T14:39:17.877Z</atom:updated>
            <content:encoded><![CDATA[<h3>Installing <strong>Hadoop</strong> in Pseudo Distributed Mode (Single Node Cluster) on Ubuntu 16.4 system.</h3><h4>In this post we shall see how to install hadoop on a single node cluster (pseudo distributed mode).</h4><p><a href="https://hadoop.apache.org/">Hadoop</a> software library framework allows distributed processing of large data sets across clusters of computers(can be commodity computers as well) using <a href="https://hadoop.apache.org/docs/r1.2.1/mapred_tutorial.html">simple programming models</a>. It is designed to scale up from single server to thousands of machines, each offering local computation and storage. “Rather than rely on hardware to deliver high-availability, the library itself is designed to detect and handle failures at the application layer, so delivering a highly-available service on top of a cluster of computers, each of which may be prone to failures.” For more information look the following link.</p><p><a href="https://hadoop.apache.org/">Apache Hadoop</a></p><p>Hadoop frame work contains the following modules:(top to bottom as shown in the below figure)</p><ol><li>Hadoop Mapreduce.</li><li>Hadoop YARN.</li><li>Hadoop Distributed File System(HDFS).</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-VVguR_SuIxkNz-ncWxqdA.jpeg" /><figcaption>Architecture of Hadoop</figcaption></figure><p>We shall now see how to install hadoop single node cluster, which is also known as Pseudo distributed mode where 2 nodes — a master and a slave process are simulated by running JVM’s . A fully-fledged test cluster environment view is given in this mode.In this mode HDFS(<strong>Hadoop Distributed File System</strong>) for storage (uses some portion of our hard disk) and YARN(<strong>Yet Another Resource Negotiator</strong>) for managing the resources are used.</p><h3>The following is the step by step procedure for installation of <strong>hadoop</strong> single node cluster.</h3><p><strong>Requirements:</strong><br>Ubuntu system.<br>Hadoop tar(stable version : 2.9.1) can downloaded from the following <a href="http://mirrors.estointernet.in/apache/hadoop/common/hadoop-2.9.1/">link</a><br>which is about 345 MB.</p><p><strong>Installation Procedure:</strong><br>Before starting installation of hadoop we check for the installation of java.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/778/1*ETfOZqfVP8du3dTvjKPCcg.png" /><figcaption>java -version (If java is installed the we get the version of installed java in our system).</figcaption></figure><p>If java is not installed use <strong>sudo apt-get install default-jre</strong></p><figure><img alt="Command for installing java using apt-get" src="https://cdn-images-1.medium.com/max/511/1*gb_f0bFiusX-_YTZ3Vbmlg.png" /><figcaption>Install java using apt-get</figcaption></figure><p>or install java from source.</p><p>Or use</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*f5GhOsySaWAcpD5CniohGQ.png" /><figcaption>sudo apt-get install openjdk-8-jre</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*h9RjwvKcYjqvHZmW0jCX9w.png" /><figcaption>sudo apt-get install openjdk-8-jre</figcaption></figure><blockquote>Preferably install <strong>java</strong> <strong>8</strong> because <strong>java 9</strong> gave some errors in <strong>multi-node</strong> cluster setup.</blockquote><h3><strong>HADOOP INSTALLATION:</strong></h3><p><strong>Step 1</strong><em> : Add new user group. (We add new user/users to this group.)</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*r_U9W2pc3hZQinvr2zGeAQ.png" /><figcaption><strong>sudo addgroup hadoop // new group hadoop is added .</strong></figcaption></figure><p><strong>Step</strong><em> </em><strong>2</strong><em> : Add new user to this group.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*A4sqkqmboeaXd9_pR4pE_Q.png" /><figcaption><strong><em>sudo adduser — ingroup hadoop hduser</em></strong></figcaption></figure><p><strong><em>We get prompt to password, please give password and confirm it as shown:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*-lr9UzN57XEjnW0JFxeh6w.png" /><figcaption>Adding a new user in hadoop group.</figcaption></figure><p><strong>Step 3<em> </em></strong><em>: Login as </em><strong><em>hduser</em></strong><em> using </em><strong><em>su hduser </em></strong><em>and produce</em><strong><em> ssh-keys for password less </em></strong><em>login to localhost</em><strong><em> </em></strong><em>so that hadoop which is going to be installed in pseudo distributed mode does password less login to </em><strong><em>localhost</em></strong><em> and dose the tasks assigned.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/295/1*9A0vDftN2rZtaIKbIBap2Q.png" /><figcaption>To login as hduser in hadoop group.</figcaption></figure><p><em>Using</em></p><h4><strong><em>ssh-keygen -t rsa -p “ ”</em></strong></h4><p>we get the following prompt:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*QYtof2Q63qNqkVJl1Sx6Cg.png" /><figcaption>using command ssh-keygen -t rsa -p “”</figcaption></figure><p><em>you will be getting a prompt to give path for generated key.<br></em><strong><em>press enter for default.</em></strong><em><br>we need to copy the key generated to the authorized_keys.</em></p><p><strong><em>cat ~/.ssh/id_rsa.pub &gt;&gt; ~/.ssh/authorized_keys</em></strong></p><p><strong><em>Step 4</em></strong><em>: We finally test whether the local machine is being connected through<br>hduser or not.<br></em><strong><em>ssh localhost </em></strong><em>//connect to </em><strong><em>localhost</em></strong><em> from </em><strong><em>hduser</em></strong><em> without password.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/843/1*5xRUe9PwF4em32ucw0OrGg.png" /><figcaption>using ssh localhost for password less login.</figcaption></figure><p>we get somthing like above and press yes and we login to the localhost.<br>Press exit and come out to the hduser.</p><p><strong>Till</strong> now we had set the system for installation of hadoop in it. We shall now see how to put <strong>HADOOP</strong> on top of this machine.<br>We install <strong>hadoop</strong> in <strong>/usr/local/</strong> so that it is in common place to all the users( we can install in any other location as well).<br>First extract the .tar file which is downloaded and rename it as hadoop.</p><p><strong><em>Step 5</em></strong> : Move the extracted file to <strong>/usr/local/</strong> and change the access permission to only hduser in the hadoop group.<br><strong>sudo mv hadoop /usr/local</strong><br>Next <strong>changeownership</strong> of hadoop folder <strong>to hduser</strong> in <strong>hadoop group.</strong></p><blockquote><strong>sudo chown -R hduser:hadoop hadoop</strong></blockquote><p><strong><em>Step 6 :</em></strong> We now make some changes in the .bashrc file (~/.bashrc). We add the following lines at the end of the bashrc file.</p><blockquote># $HOME/.bashrc<br># Set Hadoop-related environment variables<br>export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64<br>export HADOOP_HOME=/usr/local/hadoop/sbin/hadoop<br>export HADOOP_CONF_DIR=/usr/local/hadoop/<br>export HADOOP_MAPRED_HOME=/usr/local/hadoop/<br>export HADOOP_COMMONHOME=/usr/local/hadoop/<br>export HADOOP_HDFS_HOME=/usr/local/hadoop/<br>export YARN_HOME=/usr/local/hadoop/<br># Some convenient aliases and functions for running Hadoop-related commands<br>unalias fs &amp;&gt; /dev/null<br>alias fs=”hadoop fs”<br>unalias hls &amp;&gt; /dev/null<br>alias hls=”fs -ls”<br>lzohead () {<br>hadoop fs -cat $1 | lzop -dc | head -1000 | less}<br># Add Hadoop bin/ directory to PATH<br>export PATH=$PATH:$HADOOP_HOME/bin</blockquote><p><strong><em>Step 7:</em></strong><br>We now configure our hadoop file for it to work as single node cluster.<br>Open the <strong>hadoop-env.sh</strong> file which is present in /usr/local/hadoop/etc/hadoop/ and remove comment of the line export JAVA_HOME and add the path of the java directory as shown.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/1*DyuqRIpUXWpcnTw85xhKTQ.png" /><figcaption>hadoop-env,sh in /usr/local/hadoop/etc/hadoop/</figcaption></figure><p>Then create new folder <strong>tmp</strong> for which <strong>hadoop</strong> uses to store the files and run the tasks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*CouLcM-uCbO45dNIsRhlng.png" /><figcaption>Create tmp directory.</figcaption></figure><p>Change the ownership of this file to hduser under the user group hadoop.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*9_GF31Z_FZago4oopUVD8w.png" /><figcaption>change ownership</figcaption></figure><p>We next change the <strong>core-site.xml</strong><br>$ vi /usr/local/hadoop2/etc/hadoop/core-site.xml and put the following<br>code in the space between <strong>&lt;configuration&gt; . . . &lt;/configuration&gt;</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/689/1*TZJBKBWVDkYrzXNzJrtC0Q.png" /></figure><p>The following should be put:</p><blockquote>&lt;!- -conf/core-site.xml- -&gt;<br>&lt;property&gt;<br>&lt;name&gt;hadoop.tmp.dir&lt;/name&gt;<br>&lt;value&gt;/app/hadoop/tmp&lt;/value&gt;<br>&lt;description&gt;A base for other temporary directories.&lt;/description&gt;<br>&lt;/property&gt;<br>&lt;property&gt;<br>&lt;name&gt;fs.default.name&lt;/name&gt;<br>&lt;value&gt;hdfs://localhost:54310&lt;/value&gt;<br>&lt;description&gt;The name of the default file system. A URI whose<br>scheme and authority determine the FileSystem implementation. The<br>uri’s scheme determines the config property (fs.SCHEME.impl) naming<br>the FileSystem implementation class. The uri’s authority is used todetermine the host, port, etc. for a filesystem.&lt;/description&gt;<br>&lt;/property&gt;</blockquote><p>Similarly we change in the <strong>mapred-site.xml</strong> present in the same directory<br>in between &lt;configuration&gt; . . . &lt;/configuration&gt; as shown:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/763/1*9nqImNcXYq9Y52D9OJOVnQ.png" /></figure><blockquote>&lt;!- -conf/mapred-site.xml- -&gt;<br>&lt;property&gt;<br>&lt;name&gt;mapred.job.tracker&lt;/name&gt;<br>&lt;value&gt;localhost:54311&lt;/value&gt;<br>&lt;description&gt;The host and port that the MapReduce job tracker runs<br>at. If “local”, then jobs are run in-process as a single map<br>and reduce task.<br>&lt;/description&gt;<br>&lt;/property&gt;</blockquote><p>Similarly in the hdfs-site.xml also we put in the space between<br>&lt;configuration&gt; . . . &lt;/configuration&gt; the following code as shown below :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/912/1*HStKBHf4HGL4I5plS6Lk8w.png" /></figure><blockquote>&lt;! --conf/hdfs-site.xml- -&gt;<br>&lt;property&gt;<br>&lt;name&gt;dfs.replication&lt;/name&gt;<br>&lt;value&gt;1&lt;/value&gt;<br>&lt;description&gt;Default block replication.<br>The actual number of replications can be specified when the file is created.<br>The default is used if replication is not specified in create time.<br>&lt;/description&gt;<br>&lt;/property&gt;</blockquote><p>Once done we are ready to start our hadoop node and work on, but before<br>that the <strong>Namenode</strong> needs to be formatted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/1*yWvgVdi1q9eqWCHYGdlmcg.png" /><figcaption>format the name node before using hadoop</figcaption></figure><p>We get the message as the name-node is formatted if every thing as described<br>above is done properly.<br>To start the hadoop system we have shell script which does the starting of all<br>nodes, managers and other things.</p><p>To start hadoop file system we have the following command</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*rAZYSzMrXn6fBq_l0HnnZg.png" /></figure><p>We get the output as following</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*yGgomim6SP4Q2yAQJ-Oh5A.png" /></figure><p>To check whether all the nodes are running we can use jps for this. The output<br>is shown as in the picture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/218/1*Gly3aAY4o7poXyy3WuniyA.png" /></figure><p>To stop we also have stop all shell script which stops all the processes which are started. We have sbin/stop-all.sh</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/759/1*mkCw6NgF9haUcZGD1Xnobw.png" /><figcaption>Stopping all the started processes.</figcaption></figure><p>We may get that the scripts start-all.sh, stop-all.sh are deprecated so we can use start-dfs.sh, start-yarn.sh and stop-dfs.sh, stop-yarn.sh to strart and stop our single node cluster.<br><strong>HURRAY ! Hadoop is successfully installed in your system.</strong></p><p>The installation on Multi node hadoop cluster is explained in this article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=563dbdbc3d24" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>