<?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 Emile Gill on Medium]]></title>
        <description><![CDATA[Stories by Emile Gill on Medium]]></description>
        <link>https://medium.com/@emilegill743?source=rss-52b1a8846410------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*4zGE4EuPW6iQZLcjSwymmQ.png</url>
            <title>Stories by Emile Gill on Medium</title>
            <link>https://medium.com/@emilegill743?source=rss-52b1a8846410------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 23:56:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@emilegill743/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[Getting Started with Snowflake ❄️]]></title>
            <link>https://medium.com/data-science/getting-started-with-snowflake-%EF%B8%8F-35d395103034?source=rss-52b1a8846410------2</link>
            <guid isPermaLink="false">https://medium.com/p/35d395103034</guid>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[editors-pick]]></category>
            <dc:creator><![CDATA[Emile Gill]]></dc:creator>
            <pubDate>Wed, 09 Dec 2020 19:16:31 GMT</pubDate>
            <atom:updated>2020-12-10T08:41:04.014Z</atom:updated>
            <content:encoded><![CDATA[<h4>Exploring the hype and delving into the free trial of Snowflake’s Cloud Data Platform</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fxme-rt2YSnE4pt9GVFyUA.jpeg" /><figcaption>Image by <a href="https://pixabay.com/users/geralt-9301/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1065155">Gerd Altmann</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1065155">Pixabay</a></figcaption></figure><p><strong><em>Whether you’ve heard of Snowflake Inc. from its record-breaking IPO this September, or from its ever-growing presence in data job ads, there is definitely a lot of hype around this company at the moment. In this article, we explore why, and how we can get started with its free trial.</em></strong></p><h3>What is Snowflake?</h3><p>A data warehouse is essential for any company that wants to be data-driven, and all companies should aspire to be data-driven; having business data at the fingertips of analysts and decision-makers has never been more important. That said, it is no easy feat to architect an on-premise data warehouse.</p><p>Requiring heavy IT support to set up and maintain the necessary hardware and software, on-premise data-warehouses have traditionally only been viable for large companies, and have often proved a source of friction between IT and analytics teams. This is humorously demonstrated by Snowflake in their ad:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fyvr4yzs9Vio%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dyvr4yzs9Vio&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fyvr4yzs9Vio%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/2ee1e8ccf0be78d47d9a68f269914407/href">https://medium.com/media/2ee1e8ccf0be78d47d9a68f269914407/href</a></iframe><p>Snowflake elegantly solves the problem of data warehousing. A cloud-based solution, Snowflake requires none of the heavy infrastructure set-up or up-front costs that are associated with traditional on-premise data-warehouses. It also allows users to scale up and down easily, whilst only paying for the storage and compute that they use. This makes it a great option for startups, where it may not be feasible to raise the up-front costs needed for a traditional data warehouse. Or for companies moving up the data hierarchy of needs, that need to start small, but scale up as their company progresses towards a more data-driven culture.</p><h3>Why Snowflake?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uxlHcYCwt9Xu9bTx3Xf4gA.jpeg" /><figcaption>Image by <a href="https://pixabay.com/users/natwhitephotography-1575700/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1386553">Natalie White</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1386553">Pixabay</a></figcaption></figure><p>Its unique selling point comes in its layered architecture. At the base we have the “Database Storage” layer, this is where Snowflake stores and efficiently organises data loaded into the platform. Above this sits the “Compute” layer, where our virtual warehouses live. Each of these is an independent compute cluster that has access to the storage layer. Being independent, these virtual warehouses do not compete for any of the same resources; hence, we can scale the computational power to run queries on our storage layer indefinitely. On top of all of this sits Snowflake’s “Cloud Services” layer. This is the layer that we directly interact with, coordinating the way we interact with Snowflake’s underlying architecture, all through the universal language of SQL.</p><p>The separations of these layers and independent ability to scale each is what marks Snowflake out from the crowd. Giving the flexibility to organisations to pay only for the storage and compute they need, as well as scaling indefinitely to meet their needs.</p><h3>Getting Started — Signing up for a free trial</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*GPK2BKeGSQP1bbkT" /><figcaption>Photo by <a href="https://unsplash.com/@foxxmd?utm_source=medium&amp;utm_medium=referral">Matt Duncan</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>One of the great things about cloud tech, is that there’s almost always a free trial that you can try out, without having to pay anything upfront. This makes learning the platform accessible, not just to those privileged enough to work for a company willing to try out new tech, or invest in learning and development, but also to individuals who are curious and trying to upskill themselves for the job market.</p><p>In a <a href="https://towardsdatascience.com/setting-up-a-postgresql-instance-on-the-cloud-4ec4cf168239"><strong>recent article</strong></a>, I detailed how one could set up a cloud PostgreSQL instance on AWS, Google Cloud Platform, or Heroku. Similarly, here, we will see that Snowflake offers a generous free-trial option that will allow us to explore what the platform has to offer.</p><p>To get started, we simply need to register <a href="https://signup.snowflake.com/"><strong>here</strong></a>. Upon registration, an email will shortly arrive in our inbox with a link to our newly created Snowflake instance, for which we can set a username and password. From this point onwards we have <strong>$400 of Snowflake credits </strong>at our disposal to get a feel for the platform and explore the power of the features it has to offer.</p><h3>Exploring the Web UI</h3><p>Let’s begin by taking a look at the user interface of our newly set up instance. At the top of the console, we can see a taskbar comprising ‘<strong>Databases’</strong>, ‘<strong>Shares’</strong>, ‘<strong>Data Marketplace’</strong>, ‘<strong>Warehouses’</strong>, ‘<strong>Worksheets’</strong>, ‘<strong>History’</strong>. We’ll go through these one by one to get a feel for some of the fundamental concepts and features of Snowflake.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/678/1*hKH26xRlbYrP9UYBPH1X-A.png" /></figure><h4>Databases</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-ZSyP_C7dYUBJL2Dv8S_kg.png" /></figure><p>Here we can see a list of all the databases we have access to. We can also use the user interface to create, clone, drop or transfer ownership of a database. You will see that in our free trial we have already been provided with several databases. The database SNOWFLAKE_SAMPLE_DATA is of particular use, as this is pre-loaded with several datasets that we can immediately get started with.</p><p>Clicking on a database, we can see its comprising Tables, Views, Schemas, Stages, File Formats, Sequences and Pipes. The first three of these will be familiar to anyone with a working knowledge of SQL, the latter are powerful additions that help us with loading data into Snowflake, some of which we will explore later.</p><h4>Shares</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F3RX_8RZNPw5d14iomcmFQ.png" /></figure><p>The “Shares” tab demonstrates another feature unique to Snowflake, allowing us to easily share Tables and Views between different accounts. Owing to its layered architecture, data stored on a provider’s account may be queried by a consumer, without the need for any replication or transfer of data. The top-level “Services” layer simply manages access to the stored data, such that the consumer incurs charges only for the compute resources they use to query the data, but no additional storage resources.</p><p>This all sounds simple, but is quite revolutionary when it comes to sharing data between third-party vendors, or between independent business functions within an organisation. It eliminates the need for any archaic form of data transfer like SFTP, or any replication of the data. The services layer from the consumer account simply reaches over to the provider’s storage layer and can see the data as it is, using its own compute resources to perform any queries.</p><h4>Data Marketplace</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7-YW7tbIrfslZdTYzjjqqA.png" /></figure><p>As well as the direct sharing of data from providers to consumers we saw in the previous section, Snowflake also offers a “Data Marketplace” feature. This is an “app-store” of data, if you will. Various companies offer datasets, some available instantly for us to import and query, others being “Personalised” options which can be requested and purchased from the company under specific commercial terms agreed with the consumer. Again, something so simple, but entirely unique, that Snowflake has brought to the game, creating an entire industry in itself of companies providing and purchasing data in a seamless manner.</p><h4>Warehouses</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c5AzJM8TtQEETN6IfWUU7w.png" /></figure><p>As discussed previously, “virtual warehouses” provide the computational element of the Snowflake platform, powering the queries and data loading tasks we perform. In the “Warehouses” tab we see a list of the warehouses we have available, as well as- analogous to what we saw in the “Tables” tab- the ability to create, drop and transfer ownership of warehouses.</p><p>We also have the option to configure the properties of a warehouse and to suspend or resume a warehouse. Remember, each warehouse is an independent compute cluster with specified size. This size determines the computational power we have available to make a query, but also the number of credits that we will be billed. We can provision compute resources to meet our individual needs and suspend or resume these as required so that we are not billed while they are not in use. The “Auto Suspend” property of a warehouse means that we do not have to resume/suspend manually; after a specified period of inactivity, the warehouse will be suspended automatically, resuming once a new query is executed.</p><h4>Worksheets</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KKXHQX35q-sS4EnkfuUwKg.png" /></figure><p>The “Worksheets” tab provides a neat editor where we can write and execute SQL commands. The sidebar on the left visualises database objects: databases, schemas, tables, views. The tabbed layout at the top of the page allows us to create multiple “worksheets”, which are saved by Snowflake, so we don’t need to worry about losing our work. We can also load a SQL script in a .sql file, stored locally, to a worksheet. We see the result of any queries we execute at the bottom of the UI, and have the option of exporting this data to a CSV, or even copying it to our clipboard.</p><h4>History</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Id6J0h5RCXuMOEJ7j-2mSQ.png" /></figure><p>Here we can see all queries that have been executed on our Snowflake instance, going back 14 days. We can also see various metadata about these executions, including the status of the execution, warehouse used and duration the command took to run. We can even see the results of historic queries that have been run in the last 24 hours. This is useful if we want to retrieve information about queries that we have run or to check up on the status of any queries that might be currently executing.</p><h3>Fundamentals of Snowflake</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZFmvnkCpB2VRQT2T" /><figcaption>Photo by <a href="https://unsplash.com/@aaronburden?utm_source=medium&amp;utm_medium=referral">Aaron Burden</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Now that we have a feel for the concepts behind the Snowflake platform and the user interface that it provides, let’s consider a simple use case. Say we have a CSV dataset that we want to interrogate in Snowflake, how would one go about this task?</p><h4><strong>Connecting to Snowflake</strong></h4><p>When it comes to connecting to Snowflake, we have <a href="https://docs.snowflake.com/en/user-guide-connecting.html">several options</a> to choose from.</p><p>We have seen that the web UI provides us with a “Worksheets” tab, where we can execute SQL commands and that we can use the UI itself to create objects. In addition to this, we can also connect to Snowflake using the SnowSQL command-line interface, or through its <a href="https://docs.snowflake.com/en/user-guide/conns-drivers.html">various connectors to common languages/frameworks</a>. Here we will stay language agnostic by exploring the Snowflake CLI.</p><p>The installation process for SnowSQL CLI is covered <a href="https://docs.snowflake.com/en/user-guide/snowsql-install-config.html">here</a> in the official documentation. Once it has been installed, connecting is easy. We simply, provide our connection parameters (the full list of which may be found <a href="https://docs.snowflake.com/en/user-guide/snowsql-start.html#connection-syntax">here</a>) and we’re in:</p><pre>snowsql -a &lt;account-name&gt; -u &lt;username&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oLc7YjS_jXHpadsTYKuPgQ.gif" /></figure><h4>Creating Database Objects</h4><p>Before we load any data, we must first set up the required database objects in Snowflake to house our CSV data, as well as provisioning the resources required for the load process.</p><p>Let’s first provision the compute resources we will use for our loading of data. To do this we need to create a new “virtual warehouse”, specifying the properties we want it to have. In this case, since our task will require minimal compute power, we provision an “X-SMALL” warehouse, which will only consume 1 Snowflake Credit per hour. We will also set an autosuspend of 3 minutes, so that our warehouse will automatically shut down after 3 minutes of inactivity, conserving the credits we use.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c46bd7b5f4940933bcca127f2f9cab6f/href">https://medium.com/media/c46bd7b5f4940933bcca127f2f9cab6f/href</a></iframe><p>Next, we will need a database to house the tables and views for our project. We can do this simply by running:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/166a9af98fd6fc47ff5df624431afed1/href">https://medium.com/media/166a9af98fd6fc47ff5df624431afed1/href</a></iframe><p>Now that we have our warehouse and database set up, we can check the context we are working in, with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/07ffe4c5e0d541075634183427b8682f/href">https://medium.com/media/07ffe4c5e0d541075634183427b8682f/href</a></iframe><p>If we correctly executed the above commands, then this should return the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*MrY4EOU0zaymDzbneIw1rw.png" /><figcaption>Checking current context</figcaption></figure><p>All that remains is to create a table and then we can go ahead and start learning how to load in our data. For comparison, I’ll use the same data as my <a href="https://towardsdatascience.com/a-practical-guide-to-getting-set-up-with-postgresql-a1bf37a0cfd7">“<em>A Practical Guide to Getting Set-up with PostgreSQL</em>”</a> post, which used the <em>Covid-19 Geographic Case Distribution </em>dataset provided by the European Centre for Disease Prevention and Control. You can get the latest version of this dataset <a href="https://opendata.ecdc.europa.eu/covid19/casedistribution">here</a> as a CSV.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d6b4171faff3313ef6fd53e46a00f17/href">https://medium.com/media/6d6b4171faff3313ef6fd53e46a00f17/href</a></iframe><h4>Loading Data</h4><p>When it comes to loading data into Snowflake, we must first create a “<em>Stage”</em>. This is simply a reference to a cloud storage location, from which Snowflake can pull our data. There are two forms of data stage that we might use: <strong>external stages</strong>, which reference cloud storage outside of Snowflake (i.e. Amazon S3, Google Cloud Storage, or Microsoft Azure) and <strong>internal stages</strong>, cloud storage hosted within the Snowflake platform.</p><p>To create an external stage we would- in keeping with the above syntax for creating database objects- simply need to run:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19bec57a7746436aca74fdcb7ca7b46c/href">https://medium.com/media/19bec57a7746436aca74fdcb7ca7b46c/href</a></iframe><p>The parameters will vary slightly depending on the cloud service provider (AWS, GCP, Azure) we are connecting to. The example I have given here is for AWS, but the documentation <a href="https://docs.snowflake.com/en/sql-reference/sql/create-stage.html">here</a> provides examples for all three, as well as the full list of optional parameters that can be supplied when creating a stage.</p><p>For the purposes of this tutorial, though, I will assume that we don’t have access to cloud storage already and will leverage the internal data storage provided by Snowflake. To create an internal stage is even simpler, running the above without specifying a URL or credentials parameter will create our stage within Snowflake:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3080b10aaaf7cc0a5349346367b3fa5/href">https://medium.com/media/f3080b10aaaf7cc0a5349346367b3fa5/href</a></iframe><p>Once we have our internal Snowflake stage set up, we can load in our CSV using the SnowSQL CLI put command from our local machine.</p><pre>put file://C:\Users\emile\OneDrive\Documents\GitHub\snowflake-basics\ecdc_covid_cases.csv @test_db.public.test_stage;</pre><p>If all went well, our file will now be stored in our Snowflake cloud storage, ready to be loaded into our table. We can quickly check the contents of a Snowflake data stage by running the command:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a370eb7e210133baffc503a28c83ed45/href">https://medium.com/media/a370eb7e210133baffc503a28c83ed45/href</a></iframe><p>This will return a list of the files stored in our stage. In our case, we should see only one file, named ecdc_covid_cases.csv.gz . The .gz suffix shows that Snowflake has implicitly compressed our file, reducing the cloud storage required to host it.</p><p>Now that we have our data staged in the cloud, loading it into our target table couldn’t be easier. We simply run a COPY INTO command, specifying our table and the data stage we wish to load from.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4692ea5ac63f2a9cc8ed12559f4abe51/href">https://medium.com/media/4692ea5ac63f2a9cc8ed12559f4abe51/href</a></iframe><p>As well as defining the table we seek to populate, and stage to gather data from, we also specify the FILE_FORMAT parameter. In this case, letting Snowflake know that we are loading CSV data, we want to skip the first row which contains headers, ignore missing columns and ignore commas contained within strings enclosed by double-quotes. There are many other options we could have specified here, and Snowflake even allows us to execute the create file_format command to create a named format, should we want to persist this for later use. In this case, though, specifying it in our COPY INTO command will suffice.</p><p>Following the execution of this command, we will be greeted with a line confirming the success of our execution. If not we will see a red error message, suggesting we need to tweak our load command to ensure that the file format is correctly specified and the data within is corresponding to the table we configured previously. Assuming our command has indeed executed correctly, we can now test the data we have loaded by selecting the first few lines by query:</p><pre>select * from worldwide_cases<br>limit 5;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rTSvrGwHHHkBwlAqaSSTuw.png" /></figure><p>The results should return the first 5 lines of our dataset, indicating that our data is loaded correctly. We can now query away to our heart’s content!</p><h3>Conclusion</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DCrZr2uiWMSnX_E7" /><figcaption>Photo by <a href="https://unsplash.com/@alessandroerbetta?utm_source=medium&amp;utm_medium=referral">Alessandro Erbetta</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>We now have a basic feel for the fundamentals of the Snowflake Data Platform, can create database objects, stage and load data, and query data, both from the web-UI, and SnowSQL command-line interface. We have also explored why- in my opinion, at least- Snowflake is worthy of the hype that surrounds it.</p><p>In my next post on Snowflake, I hope to take this further and explore a more complex use case where we need to continuously load data into a Snowflake database from cloud storage. But for now…</p><p><strong>Thanks for reading!</strong></p><p>If you enjoyed this post, feel free to check out some of my other articles:</p><ul><li><a href="https://towardsdatascience.com/a-practical-guide-to-getting-set-up-with-postgresql-a1bf37a0cfd7">A Practical Guide to Getting Set-up with PostgreSQL 🐘</a></li><li><a href="https://towardsdatascience.com/setting-up-a-postgresql-instance-on-the-cloud-4ec4cf168239">Setting up a PostgreSQL instance on the cloud ⛅</a></li><li><a href="https://towardsdatascience.com/getting-started-with-bokeh-effortlessly-elegant-interactive-data-visualisations-in-python-703249565bb3">Getting started with Bokeh — Effortlessly elegant interactive data visualisations in Python</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=35d395103034" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/getting-started-with-snowflake-%EF%B8%8F-35d395103034">Getting Started with Snowflake ❄️</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting started with Bokeh — Effortlessly elegant interactive data visualisations in Python]]></title>
            <link>https://medium.com/data-science/getting-started-with-bokeh-effortlessly-elegant-interactive-data-visualisations-in-python-703249565bb3?source=rss-52b1a8846410------2</link>
            <guid isPermaLink="false">https://medium.com/p/703249565bb3</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[bokeh]]></category>
            <category><![CDATA[getting-started]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Emile Gill]]></dc:creator>
            <pubDate>Tue, 10 Nov 2020 16:24:34 GMT</pubDate>
            <atom:updated>2020-11-10T20:07:17.794Z</atom:updated>
            <content:encoded><![CDATA[<h4><a href="https://towardsdatascience.com/tagged/getting-started">Getting Started</a></h4><h3>Getting started with Bokeh</h3><h4>Effortlessly elegant interactive data visualisations in Python</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yBUpvLcIzvKJW1sw" /><figcaption>Photo by <a href="https://unsplash.com/@auntneecey?utm_source=medium&amp;utm_medium=referral">Denise Johnson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong><em>In this article I aim to give you an introduction to Bokeh, detailing what it is, why you should be using it and how you can easily get started!</em></strong></p><h3>What is Bokeh?</h3><p>Bokeh is a neat Python library that allows us to quickly and easily create high-performance, professional interactive data visualisations and web apps. You can check out some examples of the power and range of what Bokeh can do <a href="https://docs.bokeh.org/en/latest/docs/gallery.html"><strong>here</strong></a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*VAQPJXnpUzUgHQN4D2bH3A.gif" /><figcaption>An example of what can be done with Bokeh.</figcaption></figure><p>Bokeh’s most enticing quality is that it manages to balance both simplicity- with very few lines of code required to create complex interactive visualisations- and the flexibility for advanced customisation, should you require it. Whether you simply want to create prettier graphs in your Jupyter notebooks, or develop and deploy complete web applications, Bokeh is a great visualisation tool for any Python user to have.</p><h3>Fundamentals of Bokeh</h3><h4>Plots</h4><p>Bokeh’s <em>Plot </em>class, provides the canvas on which we can build our visualisations. To get started creating a visualisation in Bokeh we can simply instantiate a figure object:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4711977342d308ab0ca740b244d58fb7/href">https://medium.com/media/4711977342d308ab0ca740b244d58fb7/href</a></iframe><p>If we were to run the above code we would produce a blank figure object, which would then be output as an HTML file, saved to the path specified, and then displayed in a new tab of a web browser. We could also have used the output_notebook() function, if we were working in a Jupyter notebook and wanted to display our visualisation directly in an output cell.</p><p>This isn’t very inspiring so far, as we’ve yet to add any data- but we’ll see in the next section how we can start building on top of our figure to quickly add detail to our plot.</p><h4><strong>Glyphs</strong></h4><p><em>Glyphs</em> are the most fundamental plotting unit in Bokeh. They essentially consist of visual markers, with attributes that may be associated with our data. From simple line glyphs, to hexagonal grids of tiles (<a href="https://docs.bokeh.org/en/latest/docs/reference/models/glyphs/hex_tile.html#bokeh.models.glyphs.HexTile">HexTile glyph</a>), there are a multitude of different glyphs available to us, depending on our visualisation needs.</p><p>We can extend the above code, to add a glyph to our figure, as simply as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1d55392d62c4dff9e33d525df9ed03e/href">https://medium.com/media/b1d55392d62c4dff9e33d525df9ed03e/href</a></iframe><p>The power of Bokeh is visible immediately. With a single additional line of code, we have added some data to our plot which we can fiddle with and explore instantly with Bokeh’s out of the box interactivity. The plot tools visible on the right of the figure enable us to drag, zoom and reset our plot. We can even save our view to an image file, using the SaveTool feature.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*SsuU5p3TuXPJthPiKdFTOA.gif" /><figcaption>Exploring Bokeh’s inbuilt plot tools, with a simple line glyph.</figcaption></figure><p>You can explore the variety of glyphs we are able to choose from, and their associated attributes, <a href="https://docs.bokeh.org/en/latest/docs/reference/models/glyphs.html#bokeh-models-glyphs"><strong>here</strong></a>.</p><p>Adding multiple glyphs to a figure is no issue, we simply call all the glyph methods we require separately and Bokeh will layer them onto our figure in the order specified.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b1ad850dca88a458d4bbe52ae6d3397/href">https://medium.com/media/6b1ad850dca88a458d4bbe52ae6d3397/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*wghZrAcZxbxOAqKoBJkq8Q.png" /><figcaption>Multiple glyphs overlayed on a figure.</figcaption></figure><h4>Data Sources</h4><p>It couldn’t be easier to specify our data to Bokeh objects. Bokeh is compatible with the familiar data formats of lists, NumPy arrays and pandas <em>DataFrames</em>; all of which we can pass directly as arguments to glyph methods.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1bc9ce0af70abf3db7ef5fe61394e69/href">https://medium.com/media/f1bc9ce0af70abf3db7ef5fe61394e69/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*SoeaEr2bDxV0UtjmR9aPIA.png" /><figcaption>Line glyph from NumPy source data.</figcaption></figure><p>Bokeh also comes inbuilt with its own data format, the <strong>ColumnDataSource</strong>. This forms the fundamental underlying data structure used by Bokeh objects. In fact, when we supply lists, NumPy arrays, or pandas <em>DataFrames</em> to Bokeh, these are implicitly converted to <em>ColumnDataSource</em> objects behind the scenes. <em>ColumnDataSource</em>s make it easy to share data between glyphs and link the selected data, as well as allowing us to supply additional data, which, as we will see later, can be used to add annotations to our visualisation.</p><p>We can define a ColumnDataSource in the following manner:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03ab73c857706cb79db7913ce8c797f3/href">https://medium.com/media/03ab73c857706cb79db7913ce8c797f3/href</a></iframe><p>Once we have defined a <em>ColumnDataSource</em>, we can easily access data by simply specifying its column name, and passing our <em>CDS</em> as the source parameter of a plotting method. Below we create an example plot using one of Bokeh’s sample CSV datasets that we read into a pandas <em>DataFrame</em> and then convert to a<em> ColumnDataSource.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/163d858eadbfb6c27a898f9bd523bebd/href">https://medium.com/media/163d858eadbfb6c27a898f9bd523bebd/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*mASuOgxVrLN9_jjNT9Tmhw.png" /><figcaption>Apple stock opening price over time (dataset from Bokeh’s sample data).</figcaption></figure><h3>Customising Visualisations</h3><h4>Configuring Plot Tools</h4><p>As we have seen from instantiating a figure object in Bokeh, plots come with various inbuilt interactive features. If we do not articulate these explicitly, Bokeh will provide the default suite of tools. However, should we wish, we may also define these when creating our figure object:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/368f36c9b6365e6e8b145235af6d1958/href">https://medium.com/media/368f36c9b6365e6e8b145235af6d1958/href</a></iframe><p>You can explore all the plot tool options that are available in the official documentation <a href="https://docs.bokeh.org/en/latest/docs/user_guide/tools.html"><strong>here</strong></a>.</p><h4>HoverTools</h4><p>One of the plot tools we have available in Bokeh, deserving of its own section, is the HoverTool class. This is a neat feature that allows us to uncover additional details from our data, simply by hovering above an object.</p><p>We can specify a HoverTool by providing a list of (label, value) tuples for the data we wish to be included.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed62e6b163d77430f66bb00d94a937dc/href">https://medium.com/media/ed62e6b163d77430f66bb00d94a937dc/href</a></iframe><p>Field names prepended with @ are associated with ColumnDataSource column headers and names beginning with $ are “special fields” corresponding to plot metadata such as $index (index of data point), $x (x-coordinate of cursor).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*xWZCMRFwBJ_f7Tahz5Gseg.gif" /><figcaption>Adding a hover tool to our Apple stock plot.</figcaption></figure><h4>Colour Mapping</h4><p>Bokeh provides various inbuilt colour palettes for us to choose from, the full list of which can be found <a href="https://docs.bokeh.org/en/latest/docs/reference/palettes.html#bokeh-palettes"><strong>here</strong></a>. A useful feature for styling our visualised data with a specified palette is the “Color Mappers” available to us in Bokeh. These allow us to visually elucidate some property of the data, depending on its value.</p><p>For example, the linear_cmap function allows us to assign colours depending on a value’s position on a linear scale:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/847f062ae391455d2a1a956dbc859db8/href">https://medium.com/media/847f062ae391455d2a1a956dbc859db8/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*PecLecUMdi-3fihBMczYVg.png" /><figcaption>Example of a linear colour-mapping, applied to our previous sine plot.</figcaption></figure><p>Similarly a log_cmap function exists, working in exactly the same manner but with a logarithmic scale. We can even include a ColorBar alongside out plot to explicate the mapping of colours to values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*qct1grx1oNzyNQwLrXXb_A.png" /></figure><p>For categorical data, we can use the CategoricalColorMapper class to define a relationship between various categorical factors and a specified colour palette. Bokeh has a detailed guide for handling categorical data <a href="https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html"><strong>here</strong></a>, where you can read more about this should you wish.</p><h4>Annotations</h4><p>Annotations such as titles, legends, axis labels, data labels can also be added to plots. There are many different options to customise our plots here; I will not delve into too much detail on these as there is plenty of good documentation on what can be done <a href="https://docs.bokeh.org/en/latest/docs/user_guide/styling.html">here</a> and <a href="https://docs.bokeh.org/en/latest/docs/user_guide/annotations.html">here</a>.</p><p>The below example shows some of the most common properties we might want to define when creating a figure:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff8234b84e7ac221ed8d6f9eb2dd9dbe/href">https://medium.com/media/ff8234b84e7ac221ed8d6f9eb2dd9dbe/href</a></iframe><h3>Arranging Plots</h3><p>If we have multiple plots that we want to combine in the same output, we can use the bokeh.layouts module to define how these are arranged.</p><h4>Row and Columns</h4><p>The row and column functions allow us to organise plots into rows, columns, or a nested layout formed of some combination of the two.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b15ed1559e644667748edc12027ca0a/href">https://medium.com/media/7b15ed1559e644667748edc12027ca0a/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*m0yWRz7D3yAlUaSO8boD8Q.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed93d5cd394b7952fae8d2cf0baf378e/href">https://medium.com/media/ed93d5cd394b7952fae8d2cf0baf378e/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/231/1*i8iAcK75d8-3jEHnh4VmVQ.png" /></figure><h4>Grids</h4><p>A uniform grid of plots is easy to create using the gridplot function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e07614add227041ea9bc79b07bad6f32/href">https://medium.com/media/e07614add227041ea9bc79b07bad6f32/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/637/1*1InYwOmtvhlqDheCcOVJkw.png" /></figure><h4>Tabs</h4><p>We can even display our visualisations in a tabbed layout, with the ability to toggle between different plots.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2672306550bc1de0d08b6469d50f4fb/href">https://medium.com/media/f2672306550bc1de0d08b6469d50f4fb/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*PPtkhEnlTrEWhCCwWD44zw.gif" /></figure><h4>Linked Plots</h4><p>To link the axes of plots, we can simply define their axis range attributes to be equal:</p><pre>p3.x_range = p2.x_range = p1.x_range<br>p3.y_range = p2.y_range = p1.y_range</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*LOA-OvyrM1xmW16merW_zw.gif" /></figure><p>To link the selection of data in our visualisations, all that is required is for them to share the same data source:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*GUsQBgKslIGOt66OQ06DhA.gif" /></figure><h3>Conclusion</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*adopKBuUjsSN4FHu" /><figcaption>Photo by <a href="https://unsplash.com/@joshboot?utm_source=medium&amp;utm_medium=referral">Josh Boot</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>We are now well equipped with the fundamentals of Bokeh and should have everything we need to get started experimenting and creating our own custom visualisations. That said, this is only the tip of the iceberg when it comes to Bokeh’s full capabilities as a visualisation tool.</p><p>In my next post we will explore how to take what we have learned one stage further, and transition from creating beautiful visualisations to developing captivating web applications, with even more interactivity. I’ll see you back then, but for now:</p><p><strong><em>Thanks for reading!</em></strong></p><p>All of the source code for this post can be found here:</p><p><a href="https://github.com/emilegill743/bokeh-basics">emilegill743/bokeh-basics</a></p><p>If you enjoyed this post, feel free to check out some of my other articles:</p><ul><li><a href="https://towardsdatascience.com/launch-a-website-for-free-in-5-simple-steps-with-github-pages-e9680bcd94aa">Launch a Website for Free in 5 simple steps with GitHub Pages</a></li><li><a href="https://towardsdatascience.com/finding-a-grocery-delivery-slot-the-smart-way-f4f0800c4afe">Finding a grocery delivery slot, the smart way</a></li><li><a href="https://towardsdatascience.com/a-practical-guide-to-getting-set-up-with-postgresql-a1bf37a0cfd7">A Practical Guide to Getting Set-up with PostgreSQL 🐘</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=703249565bb3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/getting-started-with-bokeh-effortlessly-elegant-interactive-data-visualisations-in-python-703249565bb3">Getting started with Bokeh — Effortlessly elegant interactive data visualisations in Python</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting up a PostgreSQL instance on the cloud ⛅]]></title>
            <link>https://medium.com/data-science/setting-up-a-postgresql-instance-on-the-cloud-4ec4cf168239?source=rss-52b1a8846410------2</link>
            <guid isPermaLink="false">https://medium.com/p/4ec4cf168239</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Emile Gill]]></dc:creator>
            <pubDate>Tue, 27 Oct 2020 20:47:02 GMT</pubDate>
            <atom:updated>2020-10-27T20:47:02.799Z</atom:updated>
            <content:encoded><![CDATA[<h3>Setting up a PostgreSQL instance on the cloud, for Free ⛅</h3><h4>Comparing the “Free Tier” offerings of AWS, GCP and Heroku for setting up a cloud Postgres instance.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WLCRPV_6J7W3mmI_U7df_Q.jpeg" /><figcaption>Image by <a href="https://pixabay.com/users/mohamed_hassan-5229782/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=3458033">mohamed Hassan</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=3458033">Pixabay</a></figcaption></figure><p>In a <a href="https://towardsdatascience.com/a-practical-guide-to-getting-set-up-with-postgresql-a1bf37a0cfd7">recent article </a>I detailed how to get started with PostgreSQL, installed locally, using pgAdmin and SQLAlchemy to set up a database, load data and query it from within a Python script. However, sometimes, having a local installation to play around with isn’t enough. What if we want to connect a web app to our database? Or set up an ETL process that pulls data from an API to our database on a schedule?</p><p>This article sets out to show you how anyone can set up their own cloud-hosted Postgres instance, completely free. We will compare the offerings of three major cloud providers: AWS, GCP and Heroku.</p><p>If you want to check out my previous article before continuing, you can find it here:</p><p><a href="https://towardsdatascience.com/a-practical-guide-to-getting-set-up-with-postgresql-a1bf37a0cfd7">A Practical Guide to Getting Set-up with PostgreSQL 🐘</a></p><h3>AWS (Amazon Web Services)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*u46jUeEj-CxOMSQ8" /><figcaption>Photo by <a href="https://unsplash.com/@campoilucas?utm_source=medium&amp;utm_medium=referral">Lucas Campoi</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>AWS Free Tier</h4><p>Probably the biggest name in cloud computing right now, AWS offers a rich “<em>Free Tier</em>”, perfect for hobbyists that want to learn and experiment on a budget. For 12 months we have access to <a href="https://aws.amazon.com/free/">all this</a> for free. Of particular interest in this article, we are offered 750 hours of RDS, Amazon’s managed Relational Database Service, per month. To access the AWS Free Tier we just need to sign up for an account and verify with a credit card; our 12 months of “<em>Free Tier</em>” access will then begin.</p><h4>Setting up an RDS Postgres Instance</h4><p>Once you have an AWS account set up and verified with a credit card, to set up a Postgres instance on AWS RDS we simply navigate to the RDS page from the management console and fill out a simple configuration form.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O9NdPy0NBnBLbne2OYPOzg.gif" /></figure><p>We can select a Postgres instance and choose the “<em>Free Tier</em>” template to get started. Other important options to configure are ‘<strong><em>autoscaling </em></strong>’and ‘<strong><em>backups’</em></strong>,<strong><em> </em></strong>both of which should be disabled to prevent us from exceeding limits that may incur charges. We will also need to navigate to ‘<strong><em>Additional connectivity configuration</em>’</strong> and make sure that we set ‘<strong><em>Public Access</em></strong><em>’</em> as ‘<strong><em>Yes’</em></strong><em> </em>to allow us to access the database from outside of an EC2 instance, i.e on our local machine.</p><p>To connect to the database we have set up we first need to enable inbound traffic from our IP address. We can do this by editing the ‘<strong><em>Inbound Rules</em></strong><em>’</em> for the security group assigned to the RDS instance to allow traffic from ‘<strong><em>My IP</em></strong><em>’</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Lj9-IvcQX4sMVqPI1vNQOA.gif" /></figure><p>Once this step is complete we can connect to our database using pgAdmin, or Python as explained in my <a href="https://towardsdatascience.com/a-practical-guide-to-getting-set-up-with-postgresql-a1bf37a0cfd7?gi=dc41432e4ee7">previous post</a>. All we need is the endpoint and port for the instance, which is provided in the ‘<strong><em>Connectivity and Security</em></strong><em>’</em> section of our database’s page.</p><h4>Advantages</h4><ul><li>Free for a whole year (so long as we don’t go over the usage limits).</li><li>Access to a plethora of other “<em>Free Tier</em>” cloud solutions.</li></ul><h4>Disadvantages</h4><ul><li>Easy to go over usage limits if you’re not careful.</li><li>Requires credit card details to sign up.</li></ul><h3>GCP (Google Cloud Platform)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BnloT5ZeNUXv1dr5" /><figcaption>Photo by <a href="https://unsplash.com/@pericakalimerica?utm_source=medium&amp;utm_medium=referral">Pero Kalimero</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>GCP Free Trial</h4><p>Google Cloud Platform also has a <a href="https://cloud.google.com/free">“Free Trial” option</a>, equivalent to the AWS Free Tier. Although here we have a combination of two offerings:</p><p>Firstly upon signing up and verifying a credit card, we are eligible for a fixed budget of $300 to spend within our first 3 months. This has the advantage of giving us access to the full suite of Google Cloud products, as we are limited only by the credit remaining on our account, rather than to specific products. We also have the added security here, that we cannot unknowingly go over usage limits as we would have to explicitly upgrade our account to a “paid account” to be charged (<a href="https://cloud.google.com/free/docs/gcp-free-tier">see here</a>).</p><p>In addition to this, Google offers several “<em>Always Free</em>” products if we upgrade to a “<em>paid account</em>”. These operate in a similar fashion to the AWS Free Tier, with monthly allowances. So long as we remain within the usage limits we will not be charged and we have the added benefit here that there is no end-date to this offering; unlike the AWS “<em>Free Tier</em>”, our access will never expire and we can continue to make use of our allowance indefinitely. Unfortunately, though, Postgres isn’t included in the “<em>Always Free</em>” products that Google offers and so we will be limited to the 3-month “<em>Free Trial</em>” option for the purposes of this article.</p><h4>Setting up a Cloud SQL Postgres Instance</h4><p>To get up and running with Postgres, all we need is to create a new project, select “<strong><em>SQL</em></strong>” in the navigation menu and choose a Postgres instance. As previously, I have <strong>disabled automatic backups </strong>to keep costs down.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fy0QmUj29jALp6odxcEUTw.gif" /></figure><p>Again, we need to allow access to our IP address so that we can connect to this instance locally. To do this we can navigate to “<strong><em>Connections” </em></strong>and under “<strong><em>Public IP</em></strong>” we can add the IP address we want to allow access for.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-ucEHbnbsp8rqEltHxmiPg.gif" /></figure><p>We can find the public IP address for our instance in the “<strong><em>Overview” </em></strong>section of our instance page under “<strong><em>Connect to this instance”</em></strong><em>,</em><strong><em> </em></strong>which, along with the port, will allow us to connect to our database in the usual way.</p><h4>Advantages</h4><ul><li>No chance of unwanted charges (unless we explicitly upgrade to a “<em>paid account</em>”).</li><li>Access to the full suite of GCP’s products.</li></ul><h4>Disadvantages</h4><ul><li>Trial only lasts 3-months.</li><li>Once our credit is gone, it’s gone forever.</li></ul><h3>Heroku</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cbZPpmmmvmZOqPQ5TH0qLQ.jpeg" /><figcaption>Image by <a href="https://pixabay.com/users/bessi-909086/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=736887">Bessi</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=736887">Pixabay</a></figcaption></figure><h4>Heroku Free Tier</h4><p>Heroku is a little different from the previous two examples we’ve discussed, in that it is more of a PaaS (platform as a service) than a cloud service provider. It tends to offer more managed, low configuration services, which are great if you’re just starting out in the world of cloud technology, but a bit constrictive if you’re a seasoned developer. Heroku will also normally work out more expensive than AWS/GCP for a given task, though its simplicity makes it incredibly easy to get up and running- removing the paradox of choice that can sometimes come with the breadth of AWS and GCP services. Heroku also comes with the added benefit that its “<em>Free Tier</em>” does not require credit-card verification and hence offers a safe bet if you are concerned about incurring charges. That said, Heroku’s 10,000 row limit in its “<em>Free Tier</em>” Postgres instance puts a major constraint on what we can actually do with it, making it unlikely to be useful in projects, beyond simple experimentation.</p><h4>Setting up Heroku Postgres</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*srocJkOdmq5oXrbvK59NSw.gif" /><figcaption>Setting up a Heroku Postgres Database.</figcaption></figure><p>Setting up a Heroku Postgres database couldn’t be simpler. Firstly we need to set up an account with Heroku (no card verification necessary- but if we do we get additional benefits). We can create a new app to house our database and navigate to <a href="https://data.heroku.com/">data.heroku.com</a>, where we can specify that we would like to set up a Heroku Postgres instance. We can then install, assign our database to the app we created and check the “<strong><em>Settings</em></strong>” tab to reveal our database credentials.</p><p>From this point onwards the procedure of connecting with pgAdmin and querying our database is the same as with GCP and AWS (explained in my previous article <a href="https://towardsdatascience.com/a-practical-guide-to-getting-set-up-with-postgresql-a1bf37a0cfd7"><strong>here</strong></a>). The only caveat here being that we need to restrict database access to the particular database name in our credentials, otherwise we will see many other databases as well as our own.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jb2ZifHnjHzEDqZUaJxs-g.gif" /><figcaption>Connecting to Heroku Postgres and Restricting DB with pgAdmin.</figcaption></figure><h4>Advantages</h4><ul><li>No card verification necessary.</li><li>Impossible to be charged, unless we specifically choose a paid option.</li></ul><h4>Disadvantages</h4><ul><li>Limited to 10,000 rows</li><li>Heroku offers only a fraction of the services available on GCP or AWS.</li></ul><h3>Conclusion</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0pVuO6Ymaiwpdwl43YUDsA.jpeg" /><figcaption>Image by <a href="https://pixabay.com/users/kareni-5357143/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=3048299">RÜŞTÜ BOZKUŞ</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=3048299">Pixabay</a></figcaption></figure><p>We have now seen three methods by which we can set up a cloud-hosted Postgres instance, all with pros and cons depending on your requirements and comfort level.</p><p>Overall I would recommend AWS if you are comfortable ensuring that you stick to usage limits, as here we have the advantage of a whole year of access to RDS and a suite of other “<em>Free-Tier</em>” services. GCP may be a good midway option if you want a little more security over charges but with the equivalent flexibility and suite of services offered by AWS. And, finally, if you’re a complete beginner or want to play around with a cloud Postgres database without the commitment of credit-card verification, Heroku could be a good starting point for you.</p><p><strong><em>Thanks for reading!</em></strong></p><p>If you enjoyed this post, feel free to check out some of my other articles:</p><ul><li><a href="https://towardsdatascience.com/a-practical-guide-to-getting-set-up-with-postgresql-a1bf37a0cfd7">A Practical Guide to Getting Set-up with PostgreSQL 🐘</a></li><li><a href="https://towardsdatascience.com/finding-a-grocery-delivery-slot-the-smart-way-f4f0800c4afe">Finding a grocery delivery slot, the smart way</a></li><li><a href="https://towardsdatascience.com/launch-a-website-for-free-in-5-simple-steps-with-github-pages-e9680bcd94aa">Launch a Website for Free in 5 simple steps with GitHub Pages</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4ec4cf168239" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/setting-up-a-postgresql-instance-on-the-cloud-4ec4cf168239">Setting up a PostgreSQL instance on the cloud ⛅</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Launch a Website for Free in 5 simple steps with GitHub Pages]]></title>
            <link>https://medium.com/data-science/launch-a-website-for-free-in-5-simple-steps-with-github-pages-e9680bcd94aa?source=rss-52b1a8846410------2</link>
            <guid isPermaLink="false">https://medium.com/p/e9680bcd94aa</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[github-pages]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Emile Gill]]></dc:creator>
            <pubDate>Mon, 22 Jun 2020 11:38:03 GMT</pubDate>
            <atom:updated>2020-06-22T15:08:20.532Z</atom:updated>
            <content:encoded><![CDATA[<h4>Get your personal portfolio website on the web in less than 10 minutes.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Yc9uznWiRU47fF3M" /><figcaption>Photo by <a href="https://unsplash.com/@artnok?utm_source=medium&amp;utm_medium=referral">Nicolas Picard</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><blockquote><strong>Having your own website allows you to showcase to the world who you are, and what you’re passionate about.</strong></blockquote><p>Many people assume that to set up a website would be too expensive or technically challenging for the average person. Actually, it doesn’t have to cost anything at all, can be done by anyone- regardless of experience level- and can be up and running in under 10 minutes! (You can check out mine <a href="http://emilegill743.github.io/">here</a>)</p><p>GitHub Pages is a feature of GitHub which allows users to host static webpages straight from repositories. It’s ideal for hosting a personal portfolio website, showing off projects, or for a small organisation to host their page. In this post I’ll demonstrate just how simple it is for anyone to get a website onto the web, hosted through GitHub Pages.</p><h3>Step 1 — Create a GitHub Account</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*70aOJ1osE9C8cVZUkmH95g.png" /></figure><p>If you already have a GitHub account, feel free to skip this step. If not, you can <a href="https://github.com/"><strong>sign up here</strong></a>.</p><p>I’ll try not to assume prior knowledge throughout this article, but I’d recommend you familiarise yourself with Git concepts if you’ve never used it before. This article offers a great introduction to Git and GitHub from the complete basics:</p><p><a href="https://towardsdatascience.com/getting-started-with-git-and-github-6fcd0f2d4ac6">Getting started with Git and GitHub: the complete beginner’s guide</a></p><h3>Step 2 — Create a Repository for your Website</h3><p>A repository is where you store all the code for your project, or as GitHub’s help page explains:</p><blockquote>“A repository is like a folder for your project. Your project’s repository contains all of your project’s files and stores each file’s revision history. You can also discuss and manage your project’s work within the repository.” <a href="https://help.github.com/en/github/creating-cloning-and-archiving-repositories/about-repositories">GitHub Help</a></blockquote><p>Therefore, to get started with our project, the first thing we need to do is to create a repository for our website’s code to reside. Now that we have a GitHub account this is easy to do. Simply navigate to the top right of the GitHub homepage, and under the “+” menu, select “<em>New Repository</em>”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*yDMspYqRVSDu2G-J0Hqmgw.gif" /><figcaption>Adding a new repository</figcaption></figure><p>This will open the “<em>Create a New Repository</em>” page where we can choose a repository name and choose whether our repository is “<em>Public” </em>or “<em>Private”</em>. It’s important that we give this repository a specific name so that GitHub knows that this repository should be hosted through GitHub pages. This should be [your_username].github.io . So, for example, since my GitHub username is emilegill743, my repository name- and as we will see later also my website URL- will be <a href="http://emilegill743.github.io/">emilegill743.github.io</a>. We will also want to set our repository as “<em>Public”</em>, since GitHub Pages only allows hosting of “<em>Private</em>” repositories if we upgrade to GitHub Pro.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-G4Sa3SDJjd1MQYFNqMMUw.gif" /><figcaption>Creating a repository for our GitHub Pages website</figcaption></figure><p>Once we have completed this, GitHub will take us to our newly created repository and explain several ways we can get started working with a local copy of the repository. If you’re not familiar with working with Git on the command line, the easiest way to “<em>clone</em>” this empty repository to your local system is to use the “<em>Set up in Desktop</em>” option. This will open up GitHub Desktop- which can be installed <a href="https://desktop.github.com/">here</a>- creating a local copy of the repository at a location of your choice. You’ll now have an empty folder with the name of your repository on your local filesystem which will be tracking any changes we make. These changes can then be “<em>committed</em>” and “<em>pushed</em>” to our remote repository on GitHub.</p><h3>Step 3 — Choosing your Website Design</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q5985SAdlM2e2_j6aLOv3Q.gif" /><figcaption>Templates available on <a href="https://html5up.net/">https://html5up.net/</a></figcaption></figure><p>Now, if you’re hardcore you might want to design your website from scratch. Theoretically, you could do that; all we need is an index.html file in our remote repository and GitHub will go ahead and render our website. However, since the purpose of this post is to get our website up and running as quickly as possible, we’ll use a template to get us started. This will allow us to create a sleek, responsive, professional website with minimal work involved.</p><p>There are many sites offering templates for website designs, some can be purchased for a small cost, but many are available for free. A particular favourite of mine is <a href="https://html5up.net/">HTML5 UP</a>, which offers a selection of beautiful designs, perfect for a personal portfolio website. All designs are free under the <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 License</a>, meaning that we are free to use it as we wish, so long as we credit HTML5 UP for the design.</p><p>Feel free to do your own research and find a design that best suits you; for demonstration, here I will use HTML5 UP’s <a href="https://html5up.net/strata">Strata theme</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HgGagRTqY8uD955QLG509g.gif" /><figcaption>Strata by HTML5 UP!</figcaption></figure><h3>Step 4 — Push to GitHub</h3><p>Now that we have selected our design, we can download its associated files and transfer them into our local repository. We can then commit our changes and push this to our remote repository. Again, if you’re comfortable working with Git, go ahead and do this via the command line, but if you’re new to this you can equivalently use GitHub Desktop.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xqFaNAtnihVHChovfM7mKQ.gif" /><figcaption>Pushing to GitHub</figcaption></figure><p>This is where the magic happens. Open up a web browser and navigate to the URL [your_username].github.io .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7RHEZnMgOil1scWvJ4HdSA.gif" /></figure><p>Our website is live! Now let’s look at how we can add the final touches and personalise it, right now it’s just a template.</p><h3>Step 5— Customise your Website</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*CUjwtoeetme4uzrb" /></figure><p>For this final step, we’ll need a text editor. If you’re a coding pro, you’ll probably already have a text editor of choice. Any will do, but I’d personally recommend Visual Studio Code, which you can install <a href="https://code.visualstudio.com/">here</a>.</p><p>Opening the folder containing our repository in our text editor we will see all the files contributing towards the design of our website. The most important of these is our index.html file, which denotes the structure of the main page of our website. There will probably also be some .css files and maybe some .js too. Don’t worry too much if you’re not familiar with any of these, I wasn’t when I created my first website! A basic summary is that HTML (Hypertext Markup Language) forms the building blocks of the structure of our webpage, CSS (Cascading Style Sheets) describes how our webpage should be styled, and Javascript defines the interactive behaviour of our webpage.</p><p>The first thing I’d suggest is that you download the <a href="https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer">‘Live Server<em>’ </em>extension</a> for Visual Studio Code, which will enable us to preview our website as we edit it, automatically refreshing when we save changes. Alternatively, you can just open up the index.html page in your web browser and manually refresh to check changes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J00KE9XiRrGJzZCPPl-ueg.gif" /><figcaption>Live Server for VS Code</figcaption></figure><p>I won’t go into too much detail about how to write HTML, otherwise, this could quickly become a tutorial. The basic syntax is composed of ‘<em>tags</em>’ which form HTML elements, the building blocks of an HTML web page. These tags normally consist of a start tag, any attributes of the tag, some content and then an end tag.</p><pre>&lt;tag attribute=&quot;value&quot;&gt;Some content&lt;/tag&gt;</pre><p>It’s up to you how much you want to customise your website. At the most basic level, you’ll want to change the content of the HTML elements in your webpage to reflect your personal information. You’ll also probably want to change the images in the webpage. To do this all you need to do is copy the image that you want to use into your repository (if you’re using an HTML5 UP template it’ll probably already have a folder designated for images) and then adapt the src attribute of the image element to reflect the path to your new image, &lt;img src=&quot;path_to_image.jpg&quot;/&gt; . It’s worth noting here that image elements are composed of a single tag containing their attributes, rather than a start and end tag like many other HTML elements.</p><p>If you’re interested in learning more about HTML, CSS and Javascript so that you can take your website personalisation to the next level, I’d strongly recommend Harvard’s <a href="https://courses.edx.org/courses/course-v1:HarvardX+CS50W+Web/course/"><em>CS50W</em>: <em>Web Programming with Python and JavaScript</em></a>. This is a completely free course on <a href="https://www.edx.org/">edX,</a> the first couple of chapters of which focus on Git, HTML and CSS and later goes on to look at Javascript. It provides a great introduction to the skills we need to really make the website our own.</p><p>For further inspiration, feel free to check out my website at <a href="http://emilegill743.github.io/">emilegill743.github.io</a> and the corresponding GitHub repository linked below.</p><p><a href="https://github.com/emilegill743/emilegill743.github.io">emilegill743/emilegill743.github.io</a></p><p><strong><em>Thanks for reading!</em></strong></p><p>If you enjoyed this post, feel free to check out some of my other articles:</p><ul><li><a href="https://towardsdatascience.com/finding-a-grocery-delivery-slot-the-smart-way-f4f0800c4afe">Finding a grocery delivery slot, the smart way</a></li><li><a href="https://towardsdatascience.com/a-practical-guide-to-getting-set-up-with-postgresql-a1bf37a0cfd7">A Practical Guide to Getting Set-up with PostgreSQL 🐘</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e9680bcd94aa" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/launch-a-website-for-free-in-5-simple-steps-with-github-pages-e9680bcd94aa">Launch a Website for Free in 5 simple steps with GitHub Pages</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Practical Guide to Getting Set-up with PostgreSQL ]]></title>
            <link>https://medium.com/data-science/a-practical-guide-to-getting-set-up-with-postgresql-a1bf37a0cfd7?source=rss-52b1a8846410------2</link>
            <guid isPermaLink="false">https://medium.com/p/a1bf37a0cfd7</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Emile Gill]]></dc:creator>
            <pubDate>Tue, 16 Jun 2020 14:14:43 GMT</pubDate>
            <atom:updated>2020-06-16T14:14:43.123Z</atom:updated>
            <content:encoded><![CDATA[<h4>Getting started with PostgreSQL, pgAdmin and SQLAlchemy for Python. Incorporate SQL into the heart of your Python projects.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Lc1LSNIN1prMH0vS" /><figcaption>Photo by <a href="https://unsplash.com/@bepnamanh?utm_source=medium&amp;utm_medium=referral">Nam Anh</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>As current, or aspiring, data professionals, we are often reminded of the importance of having a solid grounding in SQL (if you’re not convinced, check out <a href="https://www.dataquest.io/blog/why-sql-is-the-most-important-language-to-learn/">this</a>, <a href="https://towardsdatascience.com/the-most-in-demand-skills-for-data-scientists-4a4a8db896db">this</a> and <a href="https://learnsql.com/blog/four-reasons-aspiring-data-scientists-must-learn-sql/">this</a>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/894/0*UTOe6uFJ07DTdEJa.png" /><figcaption>Source: “<a href="https://www.dataquest.io/blog/why-sql-is-the-most-important-language-to-learn/">Want a Job in Data? Learn SQL”, DataQuest Blog</a></figcaption></figure><p>Although, perhaps, less glamorous a skill to flaunt than Python, or machine learning, you’d be hard-pressed to find a Data Science job advert that didn’t reference SQL (see above chart). That said, with multiple flavours of the language to choose from, and many different tools through which we can interact with databases, it can be difficult to know where to get started when learning SQL.</p><p>While it may be tempting to attempt to learn SQL through online courses with embedded IDEs such as <a href="https://www.datacamp.com/">DataCamp</a> or <a href="https://www.dataquest.io/">DataQuest</a>, the real learning process comes when we are able to play and experiment with our own personal projects. To do this, we must be able to: set up our own database, load in relevant datasets, and query our data. The aim of this post is to demonstrate how, in a matter of minutes, we can get set-up to do all of these things.</p><h3>Installing PostgreSQL and pgAdmin</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qkG8HH1w-B44q7fiy52CYA.png" /><figcaption>PostgreSQL official logo, Source: <a href="https://wiki.postgresql.org/wiki/File:PostgreSQL_logo.3colors.svg">https://wiki.postgresql.org/wiki/File:PostgreSQL_logo.3colors.svg</a></figcaption></figure><p>There are an array of database management systems (DBMS) available to choose from, notably: PostgreSQL, MySQL, Oracle, SQL Server, SQLite. Here we will use <strong>PostgreSQL</strong>: a popular, open-source solution (<a href="https://www.compose.com/articles/what-postgresql-has-over-other-open-source-sql-databases/"><em>What PostgreSQL has over other open source SQL databases</em></a>).</p><p>We can install PostgreSQL <a href="https://www.postgresql.org/"><strong>here</strong></a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*34yPckwRVLlg-5CLLLaCxQ.png" /></figure><p>During the installation, make sure to keep the box for <strong>pgAdmin 4 </strong>ticked; this will be an important addition to our set-up. If you need to install pgAdmin separately you can do so <a href="https://www.pgadmin.org/">here</a>.</p><h3>Getting Started with pgAdmin</h3><p>pgAdmin is a management tool for Postgres; it essentially sits on top of our PostgreSQL installation and provides an elegant GUI for us to visualise and interact with database objects. Once we have installed Postgres and pgAdmin (as detailed above) we can go ahead and open pgAdmin 4 to see for ourselves. It should pop-up in a browser window and will prompt us to set a master password if this is the first time we are using it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WHC0JbpqKzoM227q0Hvavg.gif" /><figcaption>Exploring pgAdmin</figcaption></figure><p>As we can see, there is one Server available for us to connect to, corresponding to our local PostgreSQL 12 installation. Within this, we can see the default “postgres” database and a nested list of its comprising database objects. We don’t need to worry too much about what all these objects are for the purposes of our set-up, but <a href="https://www.postgresqltutorial.com/postgresql-server-and-database-objects/">this</a> article does a pretty good job of explaining the basic concepts, and the <a href="https://www.pgadmin.org/docs/pgadmin4/3.x/managing_database_objects.html">pgAdmin documentation</a> goes into more detail.</p><p>Let’s now create a new database and try to import some data so we can get started querying it. We will assume no prior knowledge of SQL commands and so, for now, we will use pgAdmin’s user interface to interact with PostgreSQL. To keep topical we will use the <em>Covid-19 Geographic Case Distribution </em>dataset provided by the <a href="https://www.ecdc.europa.eu/en/publications-data/download-todays-data-geographic-distribution-covid-19-cases-worldwide">European Centre for Disease Prevention and Control,</a> which can be found <a href="https://opendata.ecdc.europa.eu/covid19/casedistribution">here</a> as a CSV.</p><p>Creating a database is as simple as right-clicking the ‘<em>Databases</em>’ object, selecting ‘<em>Create</em>’ followed by ‘<em>Database</em>’, and choosing an appropriate name. Equivalently we can create a table by right-clicking the ‘<em>Tables</em>’ object. The only difference now being that we need to specify the names and data types of each column in our table. It’s important that we match the order of our columns with the order in the dataset we intend to import, though the names do not necessarily have to be the same.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HNIyxXVCGbkCFXWsixs5Fw.gif" /><figcaption>Creating a new database and table in pgAdmin</figcaption></figure><p>Now that we have created our database and a table to house our CSV dataset, we can go ahead import it. Right-clicking the table object that we just created we can select the ‘Import/Export’ dialog, specifying that we want to Import data, selecting our chosen file and letting pgAdmin know that the data has a header that we want to skip when importing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BvXDFMURdm5u5PLvv1F6Gg.gif" /><figcaption>Importing a CSV dataset using pgAdmin</figcaption></figure><p>We now have everything we need to get started querying the data for our project. Navigating to the very top of the window, we can select the ‘<em>Tools</em>’ menu, followed by the ‘<em>Query Tool</em>’ option. This will open up a new tab in our window where we can compose our SQL code and execute it, in order to see the data output. We even have the option to download this output as a CSV file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n6Zncibqamt1e5GNyt2pdg.gif" /><figcaption>Querying data in pgAdmin</figcaption></figure><p>Now that we have a basic set-up, allowing us to interact with our PostgreSQL database using the pgAdmin GUI, let’s take a look at how we can extend this further. In the next section, we will see how we can programmatically interact with PostgreSQL databases: a more practical way of embedding Postgres into the heart of our projects.</p><h3>SQLAlchemy and pandas — A Pythonic interface to PostgreSQL</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*iBhq9rOtKSmy5XWm" /><figcaption>Photo by <a href="https://unsplash.com/@art_maltsev?utm_source=medium&amp;utm_medium=referral">Artem Maltsev</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>SQLAlchemy is a neat library for Python: self-proclaimed as “The Database Toolkit for Python”, it enables us to access the functionality of our databases in a clean, Pythonic way. The full extent of what we can do with SQLAlchemy is beyond this set-up, but the official <a href="https://www.sqlalchemy.org/">documentation</a> and <a href="https://www.tutorialspoint.com/sqlalchemy/index.htm">this tutorial</a> should be plenty to get started with if you’re interested in exploring it further. Here we will look at how we can use SQLAlchemy to run raw SQL commands to our database within a Python script, as well as loading in data from a pandas Dataframe.</p><h4>Creating a Table</h4><p>Let’s start by attempting to replicate the process above of creating a table and loading in our CSV dataset. The SQL command for creating a table is simple; all we need to provide is a table name and a list of all the columns we want to be included in our table, along with their corresponding data type:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cfb33373b93065d39e29d2c6b41978bf/href">https://medium.com/media/cfb33373b93065d39e29d2c6b41978bf/href</a></iframe><p>The addition of DROP TABLE IF EXISTS means that if our <em>worldwide_cases</em> table already exists then we will drop, i.e. remove, it before running the CREATE TABLE command.</p><p>To run these SQL commands from within a Python script, we will first need to install SQLAlchemy, this can be done simply through pip with pip install SQLAlchemy . For alternative methods of installation check out the <a href="https://docs.sqlalchemy.org/en/13/intro.html#installation-guide">official installation guide</a>.</p><p>We can now connect to our Postgres database using SQLAlchemy’s create_engine function:</p><pre><em># Connecting to Postgres</em></pre><pre>from sqlalchemy import create_engine</pre><pre>connection_uri = &#39;postgres://username:password@host:port/database&#39;<br>engine = create_engine(connection_uri)</pre><p>The connection_uri string that we pass to create_engine is what lets SQLAlchemy know where to find our database, and is composed using the general structure given above. First we let SQLAlchemy know that we are working with ‘<em>postgres’ </em>and not some other flavour of SQL. We then pass our ‘<em>username’ </em>and ‘<em>password’ </em>(unless you have specifically created a new user then you can use the default username ‘postgres’ and the password which you initially set). The values of ‘<em>host</em>’ and ‘<em>port’ </em>determine the server on which the database is running; given that we have installed Postgres locally this will most likely be the default of localhost:5432. Finally, we specify the name of the database which we are attempting to connect to. For more information on constructing the URI, you can find the documentation <a href="https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls">here</a>.</p><p>Once we have created an engine instance for our database, we can execute SQL commands simply using the following:</p><pre># Executing SQL command<br>with engine.connect() as con:<br>    con.execute(sql)</pre><p>Putting all of this together we can write a create_table.py script which will connect to our database and execute the required SQL commands from our create_table.sql file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1761efbc4f247c01ca607522e4f01915/href">https://medium.com/media/1761efbc4f247c01ca607522e4f01915/href</a></iframe><p>We should now have a blank table named worldwide_cases in our covid-19 database (which will have overwritten the table we created earlier if you followed along in the last section). You can verify this in pgAdmin by right-clicking on the database and selecting “Refresh”, which should then enable us to see the table nested under “Schemas/public/Tables”. Or, alternatively, by opening the Query Tool and running the command SELECT * FROM worldwide_cases .</p><p>Now let’s load some data into our table!</p><h4>Loading data into a table</h4><p>Loading data couldn’t be simpler using SQLAlchemy in conjunction with the familiar pandas library. We can use the df.to_sql() method of pandas DataFrame class to directly load data into a specified table:</p><pre><em># Load DataFrame into database</em></pre><pre>df.to_sql(<br>    name=&#39;table_name&#39;,<br>    con=engine,<br>    if_exists=&#39;append&#39;,<br>    index=False)</pre><p>We specify the name of the table we wish to populate, as well as a SQLAlchemy engine connecting us to our database. We can also stipulate the behaviour in the case that the table already exists with the if_exists argument. In our case, since our table has already been set up, we will ‘<em>append</em>’ our DataFrame to the table. We could, however, also have set the value to ‘<em>fail</em>’, which would raise an exception in our code if the table already exists, or ‘<em>replace</em>’, in which case the table is dropped and recreated from the data.</p><p>In fact, we didn’t need to go to the trouble of creating a table in advance in the previous section. It was a worthwhile demonstration of how we can run raw SQL commands using SQLAlchemy; however, we could have simply run our df.to_sql() command and the table would have been magically created for us. We can specify the data types of the columns in our table using the dtype argument which takes a dictionary of the column names and SQL data types akin to how we specified them in our CREATE TABLE command. If we don’t specify this, pandas will infer the datatypes from the data in our DataFrame.</p><p>The full script to load our dataset into our worldwide_cases table is outlined below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7cb03c171536668cd0ba21a063f8718/href">https://medium.com/media/f7cb03c171536668cd0ba21a063f8718/href</a></iframe><p>The only addition here is a few lines to rename the columns of our CSV dataset in line with the column headers we chose in our table. I have used a bit of SQLAlchemy trickery, offering a nice demonstration of how it can be used to access database objects in a Pythonic way. Importing the MetaData and Table classes, we can instantiate a table object, reflecting our worldwide_cases table; from this, we can access its columns attribute. This was just a shortcut to save writing out a list of all the column names again, but we could have equivalently passed df.columns = list_of_column_names, so feel free to do that if you’d prefer.</p><h4>Querying the database</h4><p>Finally, we can recreate the last part of the procedure we demonstrated in pgAdmin: querying our dataset.</p><p>Say we were interested in exploring which countries currently have the highest total number of cases, we might produce a query similar to the one below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef3ee690dbc26325ad038be0ff108179/href">https://medium.com/media/ef3ee690dbc26325ad038be0ff108179/href</a></iframe><p>We saw above that we could run raw SQL commands through SQLAlchemy to create a table. Let’s try this with our SELECT query and see what happens:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/edffa36f30444262ccc59ef162c32aa7/href">https://medium.com/media/edffa36f30444262ccc59ef162c32aa7/href</a></iframe><p>Running the above script to execute our SQL query and store the results (which can be accessed as a list using the fetchall() method), returns the following output:</p><pre>(&#39;United_States_of_America&#39;, 1920061, 109802)<br>(&#39;Brazil&#39;, 645771, 35026)<br>(&#39;Russia&#39;, 458689, 5725)<br>(&#39;United_Kingdom&#39;, 284868, 40465)<br>(&#39;India&#39;, 246628, 6929)<br>         ...<br>(&#39;Papua_New_Guinea&#39;, 8, 0)<br>(&#39;British_Virgin_Islands&#39;, 8, 1)<br>(&#39;Bonaire, Saint Eustatius and Saba&#39;, 7, 0)<br>(&#39;Lesotho&#39;, 4, 0)<br>(&#39;Anguilla&#39;, 3, 0)</pre><p>We now have a tuple-like object for each row in our dataset, which we can parse and manipulate in whatever way we like to incorporate this data into our Python script. This is great; exactly what we needed. Although, wouldn’t it be nice if we could get the data straight into a pandas DataFrame, without any prior manipulation? Well, we’re in luck. In the same way that pandas offers a df.to_sql() method of loading data into a database, we can also use it’s pd.read_sql() to execute queries through our database connection.</p><p>The pd.read_sql() method is actually a combination of two separate methods: pd.read_sql_table() and pd.read_sql_query(). The former taking its first argument as the name of the table we wish to read, and the latter a SQL query. Hence, with pd.read_sql() we can supply either, depending on whether we want to read a single table or perform a query. Let’s now go ahead and put this into practice to execute our above query.sql query:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a7d402834c81ebe6a3f625522a812ab/href">https://medium.com/media/0a7d402834c81ebe6a3f625522a812ab/href</a></iframe><p>We now have a neat DataFrame object of our query results, giving us immediate access to the suite of resources pandas offers for manipulating data:</p><pre>   countries_and_territories   cases  deaths<br>0  United_States_of_America  1920061  109802<br>1                    Brazil   645771   35026<br>2                    Russia   458689    5725<br>3            United_Kingdom   284868   40465<br>4                     India   246628    6929</pre><h3>Wrapping Up</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zWyVSoKM2AS4tfen" /><figcaption>Photo by <a href="https://unsplash.com/@wolfgang_hasselmann?utm_source=medium&amp;utm_medium=referral">Wolfgang Hasselmann</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The purpose of this post was to demonstrate a simple set-up enabling us to get started learning SQL and give us the freedom to use it within our own projects and data exploration. We now have PostgreSQL installed on our system, pgAdmin to interact with it via a GUI, and we have seen that we can use SQLAlchemy to load and query data from within a Python script.</p><p>Now it’s your turn: go and learn more about SQL and what it is capable of, interrogate datasets of your choice to find insights, and immerse PostgreSQL into the heart of your Python projects!</p><p><strong><em>Thanks for reading!</em></strong></p><p>All the source code for this blog can be found here:</p><p><a href="https://github.com/emilegill743/postgres-setup">emilegill743/postgres-setup</a></p><p>If you enjoyed this post, feel free to check out some of my other articles:</p><p><a href="https://towardsdatascience.com/finding-a-grocery-delivery-slot-the-smart-way-f4f0800c4afe">Finding a grocery delivery slot, the smart way</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a1bf37a0cfd7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/a-practical-guide-to-getting-set-up-with-postgresql-a1bf37a0cfd7">A Practical Guide to Getting Set-up with PostgreSQL 🐘</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Finding a grocery delivery slot, the smart way]]></title>
            <link>https://medium.com/data-science/finding-a-grocery-delivery-slot-the-smart-way-f4f0800c4afe?source=rss-52b1a8846410------2</link>
            <guid isPermaLink="false">https://medium.com/p/f4f0800c4afe</guid>
            <category><![CDATA[twilio]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[web-scraping]]></category>
            <category><![CDATA[heroku]]></category>
            <dc:creator><![CDATA[Emile Gill]]></dc:creator>
            <pubDate>Tue, 28 Apr 2020 05:49:09 GMT</pubDate>
            <atom:updated>2020-04-29T12:37:07.929Z</atom:updated>
            <content:encoded><![CDATA[<h4>Using Python, Heroku and Twilio to scrape data from a grocery website’s API and get a text notification when slots become available</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*99QPnFVWARy4opyN" /><figcaption>Photo by <a href="https://unsplash.com/@nicotitto?utm_source=medium&amp;utm_medium=referral">nrd</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>If you haven’t already heard, we are living in <strong>unprecedented</strong> times…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1HXo8Ppt2FsrbtE6eS7tBg.png" /><figcaption>Usage of the word ‘unprecedented’ over time. Source: Google Trends</figcaption></figure><p>And with unprecedented times come novel challenges. One of these being the question of how we can maintain grocery supply chains, with millions now under lockdown measures due to the spread of Covid-19. For those that are vulnerable, in isolation, or unable to physically get to a supermarket, often the only available option is to book a supermarket delivery slot online. However, with an enormous demand for these services, it has become notoriously difficult to find an available slot- leaving many people incessantly logging in to check for slots.</p><p>That got me thinking about- as with an ever-increasing proportion of the problems I face in life- how I could use Python to automate this process for me?</p><h3>Inspecting our Grocery Website</h3><p>The first step towards our aim of some form of ‘automated delivery slot checker’ is to figure out how we can programmatically extract the data that we want from the grocery website.</p><p>After selecting <a href="https://groceries.asda.com/"><strong>Asda</strong></a> as our grocery website of choice, creating an account, and inputting a delivery postcode, we land on the <a href="https://groceries.asda.com/checkout/book-slot?tab=deliver&amp;origin=/"><strong>delivery slot page</strong></a>, shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dLQbxuD0_GCGVIgTni1LIw.png" /><figcaption><a href="https://groceries.asda.com/checkout/book-slot?tab=deliver&amp;origin=/">Asda delivery slot page</a>, showing data that we want to scrape.</figcaption></figure><p>Here we can see a neatly laid out table of dates, times and availability of each slot. Unsurprisingly, all slots are currently showing as ‘Sold Out’. But we now have visibility on the target data we want to acquire with our tool.</p><p>The next step is probably the most important of all. If you’ve ever done any web scraping before, or worked with web development, you’ll be well versed with the <strong>DevTools</strong> functionality inbuilt into most major browsers. For those that are not, these are a set of tools which allow users to inspect behind the scenes of a webpage and examine the HTML, CSS, Javascript, as well as- crucially for our project- metadata related to network requests being made to and from a server and our webpage. We can access the DevTools tab by <strong>right-clicking</strong> on our webpage and selecting ‘<strong>Inspect Element</strong>’ or typing <strong>Ctrl+Shift+I</strong> (I’m using Firefox, but a similar procedure will work for Chrome, Internet Explorer etc.).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YyCHAdIrvAZEZ7uLiKJjxw.gif" /><figcaption>Accessing Firefox Developer Tools</figcaption></figure><p>With our DevTools window now visible we can start to see what’s going on behind the scenes in our webpage to enable us to see an up-to-date table of slot availability. Navigating to the ‘<strong>Network</strong>’ tab of the DevTools window, we now have access to all the network requests being made by our website to get the latest data we can see displayed. Refreshing our webpage will yield a list of requests, one of which must contain the key to figuring out where our slot availability data is coming from.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OKyXeIDYxLB8zJr42eoTVg.gif" /><figcaption>Tracing network requests made by our grocery website</figcaption></figure><p>This list may initially seem a little confusing as we will have a sea of different requests, gathering everything from the CSS describing the formatting of the webpage to the Javascript determining the functionality of the website. What we are interested in, though, is the data being gathered to be presented on the webpage. Therefore, filtering the requests for only those of type ‘<strong>XHR</strong>’ (XMLHttpRequest), allows us to focus only on those requesting data from the server, ignoring those focussed around the styling of the webpage. This still leaves a fair few requests to be inspected; fortunately gambling that our required request will contain the word ‘slot’ narrows our search to only four remaining requests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V_y9iodToOsdltOI_HNTFA.gif" /><figcaption>Filtering requests</figcaption></figure><p>Clicking on a request and selecting the ‘<strong>Response</strong>’ tab reveals the JSON response yielded by the request and hence the data delivered to the webpage. From this, we can very quickly see that the request containing the data we are looking for is a <strong>POST request</strong> to the URL, <a href="https://groceries.asda.com/api/v3/slot/view"><strong>https://groceries.asda.com/api/v3/slot/view</strong></a>. Taking a look at the ‘<strong>Params</strong>’ tab we can see the JSON data sent by our browser in the POST request and right-clicking and selecting ‘<strong>Copy All</strong>’ to copy this JSON data to our clipboard now means that we have everything that we need to explain to Python how to collect our data.</p><h3>Making a web request with Python</h3><p>The <a href="https://requests.readthedocs.io/en/master/">Requests library</a> in Python makes it incredibly easy to make HTTP requests programmatically. From our above inspection of the website, we now know: the <strong>URL</strong> we need to send a request to, the <strong>type of request</strong> we need to use (POST) and the <strong>JSON data</strong> we need to send (currently stored in our clipboard).</p><p>In practical terms, this leaves us with the code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e321553e3d54a3ecbe272a3faf8fad62/href">https://medium.com/media/e321553e3d54a3ecbe272a3faf8fad62/href</a></iframe><p>All we have done here is pasted out our JSON data from the clipboard and added a simple request, posting this data to our URL using the json argument of the requests.post() method. Our request response object is now stored in the variable r for later use.</p><p>We have also replaced a few of the parameters in our data with variables. The start_date and end_date variables pass a dynamic date range to the API, as we are always going to be interested in looking two weeks ahead from the current date. The strftime method of datetime objects allows us to specify the exact string format that we want our datetime objects to be in, which we can match to the format we inspected in the initial JSON data we copied.</p><p>The parameters stored as os.environ variables are sensitive information that we don’t want to be publicly displayed on GitHub. Later on, we will see how we can securely store these to be deployed in our scripts.</p><p>We now have a fully functioning Python script that we can use to <strong>send a request</strong> to Asda’s API and <strong>store the response object</strong> that we receive as a result. Let’s now take a look at this response object and explore how we can parse it to extract the data we’re looking for.</p><h3>Parsing the JSON response</h3><p>Our response object r contains all the data/metadata received back from our POST request to Asda’s API. The first thing we should check is whether our request to the server was successful, or whether anything went wrong along the way. To do this we can inspect the status_code attribute of the response object.</p><pre>r.status_code # Inspect status code of response</pre><p>If this doesn’t return 200 then something has gone wrong with the request. In which case, we need to double-check that the URL and data are formatted correctly. The full list of possible HTTP status codes can be found <a href="https://en.wikipedia.org/wiki/List_of_HTTP_status_codes">here</a>; but, in general, we will receive a 200 code signifying ‘<strong>OK</strong>’ and a 400 or 404 code for ‘<strong>Bad Request</strong>’, ‘<strong>Not Found’ </strong>respectively.</p><p>Assuming we have received a 200 status code, we are now ready to inspect the data we have received in the response. Since it is quite a common prospect to be receiving data in the form of JSON, requests comes with a built-in JSON decoder.</p><pre>r.json() # Decoding JSON response content</pre><p>Printing the value of r.json to the terminal will quickly reveal that we have received a lot of data back from the server related to slot availability, prices, capacity etc. Since we are primarily interested in slot availability for the purposes of our project, we can loop through this JSON response and fill a dictionary with slots and availabilities.</p><p>We first loop through each slot day in the two-week period which we have asked for, and then within each day to inspect each individual slot, filling our dictionary:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2299a819102552e680797ef86403df8/href">https://medium.com/media/d2299a819102552e680797ef86403df8/href</a></iframe><p>Now that we have all the data that we need, and a way to programmatically extract it whenever we want, let’s take a look at how we might set up a way to notify our end-user when a delivery slot is found to be available.</p><h3>Twilio — Sending a text with Python</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5Lrdkys-wI26PxCz" /><figcaption>Photo by <a href="https://unsplash.com/@alexbeware?utm_source=medium&amp;utm_medium=referral">Alex Ware</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Twilio is a cloud communications platform, offering APIs which enable developers to send and receive text messages and phone calls in their apps and projects. This opens up a whole world of potential for automated SMS notifications, two-factor authentication, building chatbots etc. In our case, we will <strong>create a simple text notification system</strong>, such that we receive a text detailing any available delivery slots each time our script runs.</p><p>Although Twilio is a paid service, they offer a very generous free trial balance of around £13. This is more than enough to get us started with this project- given that it costs roughly £0.08 to send a text. To <strong>get started with Twilio</strong>, all we need to do is <a href="https://www.twilio.com/try-twilio"><strong>sign up</strong></a> on their website (no payment details required) and choose a phone number. Once this is complete, Twilio will provide us with an account SID and authentication token for our project.</p><p>Once we are all set up with a Twilio account, we can start using the Python API offered by Twilio. The Twilio module for Python can be installed simply through pip.</p><pre>pip install twilio</pre><p>The Twilio API for Python is incredibly simple to get started with and there is plenty of documentation available at <a href="https://www.twilio.com/docs">https://www.twilio.com/docs</a>. To send a text with our newly acquired phone number all we need is the following:</p><pre>from twilio.rest import Client<br><br>client = Client(account_sid, auth_token)<br><br>message = client.messages.create(<br>                             body=&#39;message text&#39;,<br>                             from_=&#39;+15017122661&#39;,<br>                             to=&#39;+15558675310&#39;)</pre><p>Incorporating this into our script to get available delivery slots, we can now check our data for any available slots and, if any exist, send a text to a phone number of our choice with a notification of our choice. This is outlined in the final segment of our script:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f569db26d4d2ed847ba2a012b974c506/href">https://medium.com/media/f569db26d4d2ed847ba2a012b974c506/href</a></iframe><p>We now have a complete script, enabling us to check for available delivery slots at Asda and, if any are available, receive a notification by text to let us know. The only remaining step in our project is to find a way to have our script run of its own accord on a schedule.</p><h3>Deploying to the cloud — Heroku</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mQqQqyIbsR_5yTU6" /><figcaption>Photo by <a href="https://unsplash.com/@eberhardgross?utm_source=medium&amp;utm_medium=referral">eberhard grossgasteiger</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><a href="https://www.heroku.com/">Heroku</a> is a cloud computing platform, enabling developers to easily <strong>deploy apps and projects to the cloud</strong>. It’s especially useful when it comes to getting web apps up and running with minimal set-up: making it ideal for personal projects. Here we will use Heroku as a simple way of getting our script to <strong>run at scheduled intervals </strong>in the cloud.</p><p>You can<strong> </strong>sign up to<strong> get started</strong> with Heroku <a href="https://signup.heroku.com/login">here</a>.</p><p>The first step we need to take is to create a new app to house our project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C2c3XYIRgoUZxQAiUoED_Q.gif" /></figure><p>To get our script up and running on the cloud, we need to <strong>create a new GitHub repository</strong> containing our script. You can find mine <a href="https://github.com/emilegill743/grocery-delivery-project"><strong>here</strong></a> for reference. We also need to create a file named requirements.txt. This will contain all the package dependencies we need to tell Heroku to install before it can successfully run our script.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40c205a1e06d7bf6c9673c518729be8d/href">https://medium.com/media/40c205a1e06d7bf6c9673c518729be8d/href</a></iframe><p>We can then connect our app to the GitHub repository we just created for our project. Enabling ‘<strong>automatic deploys</strong>’<strong> </strong>means that when we push to our master branch, our project will automatically deploy with the latest updates: useful if we want to continue development of our project whilst it is in production.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xMgIuVQnDtChRDM87Y7AAg.gif" /><figcaption>Deploy Heroku app from GitHub (sped up for demonstration)</figcaption></figure><p>As we mentioned earlier, there are several variables in our script that we want to keep hidden. We can do this using the ‘<strong>Config vars</strong>’ setting of our Heroku app, a convenient way to store sensitive information in our project which can easily be accessed as environment variables:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VQ_rZ9Bp78VZdVRSZJxybQ.gif" /></figure><p>The only step left is to get our script running automatically on a schedule. To do this we will need to install an add on to our app. <strong>Heroku scheduler</strong>, which can be installed <a href="https://elements.heroku.com/addons/scheduler"><strong>here</strong></a>, allows us to run tasks every 10 minutes, hour, or day.</p><p>Once we have installed Heroku scheduler we can create a new job, which will allow us to choose our schedule frequency and a command that we would like to run. Since slots tend to go quite quickly, a frequency of 10 minutes is probably best for our scheduled job. The run command that we want is simply to run our python script:</p><pre>python get-grocery-delivery-slots.py</pre><p>We can now sit back, relax and wait for our text notification to come!</p><h3>Final Remarks</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tIuq31y63_DlpEQPh6PZIg.jpeg" /><figcaption>‘With great power comes great responsibility’. Source: <a href="https://knowyourmeme.com/photos/1466254-spider-mans-presentation">knowyourmeme</a></figcaption></figure><p>We have seen, in this project, how powerful Python can be in automating tasks for us. But as we know, ‘<strong>with great power comes great responsibility</strong>’.</p><p>Given that the API we have reverse engineered for this project is probably not intended for public usage, we should use it respectfully and keep this in mind. We now theoretically have the power to send many more requests to a server than would be humanly possible using a web interface. Although one would expect a large company to have protective measures in place to block a misbehaving IP address, we shouldn’t push our luck. Sticking to what could, in principle, be performed manually ensures we respect this fair usage.</p><p>Moreover, just because we now effectively have an early warning system for slot availability, doesn’t mean that we should book slots if we don’t need them. The motivation for this project was born out of an acknowledgement of the scarcity of delivery slots and the difficulty that many are facing in finding one. Therefore, we should be only too aware of the hurdle that this puts up for the most vulnerable in society: for which this is not a convenience, but a necessity.</p><p>That said, the skills we have developed in this project open up a <strong>world of possibilities for further projects</strong>:</p><ul><li>We can now inspect a website using DevTools, reverse engineer an API and use Python’s requests library to extract data: this gives us the basic skills we need to extract data from almost any publicly available website.</li><li>We got set up with Twilio, a communications API that allows us to send texts and make calls. This not only gives us a convenient method of receiving or sending out notifications by text, but also opens us up to the further possibilities of Twilio: chatbots, alerts systems, robo-callers etc.</li><li>And we deployed our project to Heroku, allowing our scripts to run autonomously on a schedule on the cloud. A great skill to have, removing the local dependency of having to run scripts on a laptop or PC and giving us a great opportunity to showcase our projects on the web.</li></ul><p><strong><em>Thanks for reading!</em></strong></p><p>All the source code for this project can be found here:</p><p><a href="https://github.com/emilegill743/grocery-delivery-project">emilegill743/grocery-delivery-project</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4f0800c4afe" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/finding-a-grocery-delivery-slot-the-smart-way-f4f0800c4afe">Finding a grocery delivery slot, the smart way</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>