{"componentChunkName":"component---src-templates-tag-js","path":"/tags/python/","result":{"data":{"site":{"siteMetadata":{"title":"LoginRadius Blog"}},"allMarkdownRemark":{"totalCount":9,"edges":[{"node":{"fields":{"slug":"/engineering/guest-post/opencv-web-app-with-streamlit/"},"html":"<p>In this tutorial, you'll learn how to easily convert an OpenCV project into a web app that you can showcase. You'll use a library called Streamlit, which helps you easily build a web user interface in Python. Yes, you heard it right — no HTML, CSS, or JavaScript required. Just Python!</p>\n<h2 id=\"prerequisites\" style=\"position:relative;\"><a href=\"#prerequisites\" aria-label=\"prerequisites permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Prerequisites</h2>\n<ul>\n<li>You should be comfortable using basic <a href=\"https://opencv.org/\">OpenCV</a> functions.</li>\n<li>You should be comfortable coding in Python.</li>\n</ul>\n<p>Let's get started.</p>\n<h2 id=\"install-dependencies\" style=\"position:relative;\"><a href=\"#install-dependencies\" aria-label=\"install dependencies permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Install Dependencies</h2>\n<p>Install OpenCV and Streamlit using pip. You would also need Pillow — another image library.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"py\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">pip install opencv-python streamlit Pillow</span></span></code></pre>\n<blockquote>\n<p>If you use Python 2.7, note that OpenCV doesn't directly support Python 2.7 in their latest versions. Accordingly, you have to specify an older version like this <code>pip install opencv-python==4.2.0.32</code></p>\n</blockquote>\n<h2 id=\"create-a-small-opencv-project\" style=\"position:relative;\"><a href=\"#create-a-small-opencv-project\" aria-label=\"create a small opencv project permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Create a Small OpenCV Project</h2>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"py\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> cv2</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">brighten_image</span><span class=\"mtk1\">(</span><span class=\"mtk12\">image</span><span class=\"mtk1\">, </span><span class=\"mtk12\">amount</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    img_bright = cv2.convertScaleAbs(image, </span><span class=\"mtk12\">beta</span><span class=\"mtk1\">=amount)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> img_bright</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">blur_image</span><span class=\"mtk1\">(</span><span class=\"mtk12\">image</span><span class=\"mtk1\">, </span><span class=\"mtk12\">amount</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    img = cv2.cvtColor(image, </span><span class=\"mtk7\">1</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    blur_img = cv2.GaussianBlur(img, (</span><span class=\"mtk7\">11</span><span class=\"mtk1\">, </span><span class=\"mtk7\">11</span><span class=\"mtk1\">), amount)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> blur_img</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">enhance_details</span><span class=\"mtk1\">(</span><span class=\"mtk12\">img</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    hdr = cv2.detailEnhance(img, </span><span class=\"mtk12\">sigma_s</span><span class=\"mtk1\">=</span><span class=\"mtk7\">12</span><span class=\"mtk1\">, </span><span class=\"mtk12\">sigma_r</span><span class=\"mtk1\">=</span><span class=\"mtk7\">0.15</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> hdr</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">img = cv2.imread(</span><span class=\"mtk12\">filename</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&#39;tony_stark.webp&#39;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># do some cool image processing stuff</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">img = enhance_details(img)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">img = brighten_image(img, </span><span class=\"mtk12\">amount</span><span class=\"mtk1\">=</span><span class=\"mtk7\">25</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">img = blur_image(img, </span><span class=\"mtk12\">amount</span><span class=\"mtk1\">=</span><span class=\"mtk7\">0.2</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">cv2.imshow(</span><span class=\"mtk8\">&#39;Tony Stark&#39;</span><span class=\"mtk1\">, img)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">cv2.waitKey(</span><span class=\"mtk7\">0</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">cv2.destroyAllWindows()</span></span></code></pre>\n<p>Here are three image processing functions that accept an image, do some processing, and return the processed image.</p>\n<ol>\n<li><code>brighten_image</code> — increases the brightness of the image.</li>\n<li><code>blur_image</code> — applies a blur effect on the image.</li>\n<li><code>enhance_details</code> — applies an effect to enhance the details of the image.</li>\n</ol>\n<p>These functions make use of the OpenCV functions to do the actual processing — for e.g.: cv2.GaussianBlur, etc. I'm not explaining in-depth about them and the various parameters they accept since this tutorial is more focused on integrating OpenCV with Streamlit. However, feel free to jump to the OpenCV documentation to know more details about them. </p>\n<p>This program reads the image from the <code>filepath</code> using <code>cv2.imread()</code>; then, it passes the image to these functions that do the processing. Finally, the image is displayed using <code>cv2.imshow()</code>. <code>cv2.waitKey(0)</code> is to wait till the user presses any key, after which the program is exited.</p>\n<h2 id=\"streamlit-basics\" style=\"position:relative;\"><a href=\"#streamlit-basics\" aria-label=\"streamlit basics permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Streamlit Basics</h2>\n<p>Streamlit offers some common UI components out of the box that you can place on your webpage. This makes it super easy to code up something real quick. The way Streamlit works is that it reruns the Python script every time a user interacts with the components. They have some caching and optimizations, but this simple design makes it easy to build interactive webpages using Streamlit.</p>\n<blockquote>\n<p>Someone said, “Talk is cheap. Show me the code.” So, let's see some code.</p>\n</blockquote>\n<p>Open an editor and copy-paste this to <code>demo-app.py</code></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"py\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> streamlit </span><span class=\"mtk15\">as</span><span class=\"mtk1\"> st</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">st.title(</span><span class=\"mtk8\">&quot;OpenCV Demo App&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">st.subheader(</span><span class=\"mtk8\">&quot;This app allows you to play with Image filters!&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">st.text(</span><span class=\"mtk8\">&quot;We use OpenCV and Streamlit for this demo&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">if</span><span class=\"mtk1\"> st.checkbox(</span><span class=\"mtk8\">&quot;Main Checkbox&quot;</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    st.text(</span><span class=\"mtk8\">&quot;Check Box Active&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">slider_value = st.slider(</span><span class=\"mtk8\">&quot;Slider&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">min_value</span><span class=\"mtk1\">=</span><span class=\"mtk7\">0.5</span><span class=\"mtk1\">, </span><span class=\"mtk12\">max_value</span><span class=\"mtk1\">=</span><span class=\"mtk7\">3.5</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">st.text(</span><span class=\"mtk4\">f</span><span class=\"mtk8\">&quot;Slider value is </span><span class=\"mtk4\">{</span><span class=\"mtk1\">slider_value</span><span class=\"mtk4\">}</span><span class=\"mtk8\">&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">st.sidebar.text(</span><span class=\"mtk8\">&quot;text on side panel&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">st.sidebar.checkbox(</span><span class=\"mtk8\">&quot;Side Panel Checkbox&quot;</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>To start a streamlit app, simply run the command <code>streamlit run</code> with the filename — for example:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"py\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">streamlit run demo-app.py</span></span></code></pre>\n<p>You should get a similar output as follows. </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">  You can now view your Streamlit app in your browser.</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">  Local URL: http://localhost:8501</span>\n<span class=\"grvsc-line\">  Network URL: http://192.168.1.8:8501</span></code></pre>\n<p>Click on the links in the output to open the Streamlit app in your browser. You'll see something as follows. </p>\n<p><img src=\"/9202979fe96eb015f5209a8591dbc320/streamlit-1.webp\" alt=\"Streamlit basic web app\" title=\"Streamlit Basic Demo\"></p>\n<p>If you see the code, it's very straightforward. It imports streamlit as st. The default is a simple linear layout where you can place components on the webpage in a sequential manner.</p>\n<p>For example, <code>st.title() , st.checkbox(), st.slider()</code> places the components on the main page in the order they're called.. </p>\n<p>Streamlit also offers a side panel. In order to place components in the sidepanel, you can do as follows:</p>\n<p><code>st.sidebar.title() , st.sidebar.checkbox(), st.sidebar.slider()</code></p>\n<p>There are other components also apart from these. You can <a href=\"https://docs.streamlit.io/library/get-started\">explore more in the docs</a>.</p>\n<h2 id=\"integrate-streamlit-into-your-opencv-project\" style=\"position:relative;\"><a href=\"#integrate-streamlit-into-your-opencv-project\" aria-label=\"integrate streamlit into your opencv project permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Integrate Streamlit into Your OpenCV Project</h2>\n<p>Let's integrate your OpenCV program into Streamlit. Here is the complete code:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"py\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> cv2</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> streamlit </span><span class=\"mtk15\">as</span><span class=\"mtk1\"> st</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> numpy </span><span class=\"mtk15\">as</span><span class=\"mtk1\"> np</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> PIL </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> Image</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">brighten_image</span><span class=\"mtk1\">(</span><span class=\"mtk12\">image</span><span class=\"mtk1\">, </span><span class=\"mtk12\">amount</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    img_bright = cv2.convertScaleAbs(image, </span><span class=\"mtk12\">beta</span><span class=\"mtk1\">=amount)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> img_bright</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">blur_image</span><span class=\"mtk1\">(</span><span class=\"mtk12\">image</span><span class=\"mtk1\">, </span><span class=\"mtk12\">amount</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    blur_img = cv2.GaussianBlur(image, (</span><span class=\"mtk7\">11</span><span class=\"mtk1\">, </span><span class=\"mtk7\">11</span><span class=\"mtk1\">), amount)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> blur_img</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">enhance_details</span><span class=\"mtk1\">(</span><span class=\"mtk12\">img</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    hdr = cv2.detailEnhance(img, </span><span class=\"mtk12\">sigma_s</span><span class=\"mtk1\">=</span><span class=\"mtk7\">12</span><span class=\"mtk1\">, </span><span class=\"mtk12\">sigma_r</span><span class=\"mtk1\">=</span><span class=\"mtk7\">0.15</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> hdr</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">main_loop</span><span class=\"mtk1\">():</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    st.title(</span><span class=\"mtk8\">&quot;OpenCV Demo App&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    st.subheader(</span><span class=\"mtk8\">&quot;This app allows you to play with Image filters!&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    st.text(</span><span class=\"mtk8\">&quot;We use OpenCV and Streamlit for this demo&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    blur_rate = st.sidebar.slider(</span><span class=\"mtk8\">&quot;Blurring&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">min_value</span><span class=\"mtk1\">=</span><span class=\"mtk7\">0.5</span><span class=\"mtk1\">, </span><span class=\"mtk12\">max_value</span><span class=\"mtk1\">=</span><span class=\"mtk7\">3.5</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    brightness_amount = st.sidebar.slider(</span><span class=\"mtk8\">&quot;Brightness&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">min_value</span><span class=\"mtk1\">=-</span><span class=\"mtk7\">50</span><span class=\"mtk1\">, </span><span class=\"mtk12\">max_value</span><span class=\"mtk1\">=</span><span class=\"mtk7\">50</span><span class=\"mtk1\">, </span><span class=\"mtk12\">value</span><span class=\"mtk1\">=</span><span class=\"mtk7\">0</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    apply_enhancement_filter = st.sidebar.checkbox(</span><span class=\"mtk8\">&#39;Enhance Details&#39;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    image_file = st.file_uploader(</span><span class=\"mtk8\">&quot;Upload Your Image&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">type</span><span class=\"mtk1\">=[</span><span class=\"mtk8\">&#39;jpg&#39;</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&#39;png&#39;</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&#39;jpeg&#39;</span><span class=\"mtk1\">])</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> </span><span class=\"mtk4\">not</span><span class=\"mtk1\"> image_file:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk4\">None</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    original_image = Image.open(image_file)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    original_image = np.array(original_image)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    processed_image = blur_image(original_image, blur_rate)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    processed_image = brighten_image(processed_image, brightness_amount)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> apply_enhancement_filter:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        processed_image = enhance_details(processed_image)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    st.text(</span><span class=\"mtk8\">&quot;Original Image vs Processed Image&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    st.image([original_image, processed_image])</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">if</span><span class=\"mtk1\"> </span><span class=\"mtk12\">__name__</span><span class=\"mtk1\"> == </span><span class=\"mtk8\">&#39;__main__&#39;</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    main_loop()</span></span></code></pre>\n<p><strong>Let's understand what's happening here.</strong></p>\n<p>Apart from the image processing functions, you have a 'main_loop' function to add the logic for the webpage.</p>\n<p>Nothing fancy about <code>st.title(), st.subheader(), st.text()</code> — they just print some text in different sizes.</p>\n<p>Next, there are two sliders to get the amount by which you need to apply the blur and brightness filters.</p>\n<p>Note that <code>st.sidebar</code> places these components in the sidebar.</p>\n<p><code>slider()</code> takes in some arguments: name of the slider, min value, max value, and the default value of the slider. This function returns the current value of the slider.</p>\n<p>Next, there's a checkbox component.</p>\n<p><code>checkbox()</code> returns <em>True</em> if the checkbox is checked; else, it would return <em>False</em>.</p>\n<p>Next, there's a <code>file_uploader</code> component, through which users can upload files of different types specified by the <code>type</code> parameter. Here, it's restricted to the image file types needed for the demo usecase.</p>\n<p>When the program starts, there are no files selected by the user. At this time, this component returns <em>None</em>. When a file is uploaded from the UI, this component returns the path of the file. </p>\n<p>This is why there's an if-check on the return value of this component. If no files are selected, you can skip the rest of the program by returning from the <code>main_loop()</code> function.</p>\n<p>Remember that the entire program is rerun whenever there's user interaction on any components of the page. When a user uploads a file, the whole program is executed again — and the if-check fails so that the program executes the image processing logic.</p>\n<p><code>Pillow.Image()</code> is used to open this file, then it's converted to a numpy array using <code>np.array()</code> so that OpenCV can process it.</p>\n<p>Now, it's passed to the different processing functions along with the <code>amount</code> parameter.</p>\n<p>Finally, the program displays the original and processed images using the <code>st.image()</code> component.</p>\n<p>The web app is ready! </p>\n<p>To start the app, simply run:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">streamlit run demo-app.py</span></code></pre>\n<p>Now you can play with the filters. Of course, these are some basic filters, but you can extend it to more interesting filters — like cartoonify filters, etc. — using the rich features of OpenCV.</p>\n<p><img src=\"/8a3adfc96648a8ef90ba512e93ba1648/streamlit-final.webp\" alt=\"Streamlit final Screenshot\" title=\"Streamlit OpenCV Webapp\"></p>\n<h2 id=\"how-to-publish-your-app\" style=\"position:relative;\"><a href=\"#how-to-publish-your-app\" aria-label=\"how to publish your app permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How to Publish Your App?</h2>\n<p>To publish your app, you need to host this program somewhere. There are different hosting providers for complex projects. But for this hobby project, is there a simple and quick solution?</p>\n<p>Yes, there's one!</p>\n<p>You can host the Streamlit application in <a href=\"https://streamlit.io/cloud\">Streamlit Cloud</a> for free. You can host up to 3 apps in an account for free with up to 1GB of memory.</p>\n<p>Check out this tutorial from the community to host your app in Streamlit Cloud.</p>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>You can find the source code for this tutorial <a href=\"https://github.com/LoginRadius/engineering-blog-samples/tree/master/Python/OpenCV-Streamlit-Integration\">here</a>. The web app discussed above is <a href=\"https://share.streamlit.io/dingusagar/opencv-streamlit-demo/main/demo-app.py\">hosted here</a> if you want to try it out.</p>\n<p>Want to add user authentication and registration in your app quickly? Check out the <a href=\"https://www.loginradius.com/integrations/python/\">LoginRadius Python integration</a>.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n</style>","frontmatter":{"date":"November 10, 2021","updated_date":null,"title":"How to Build an OpenCV Web App with Streamlit","tags":["OpenCV","Streamlit","Python"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/818e31111fb212b94c72ff9edb09bd3f/58556/coverimage.webp","srcSet":"/static/818e31111fb212b94c72ff9edb09bd3f/61e93/coverimage.webp 200w,\n/static/818e31111fb212b94c72ff9edb09bd3f/1f5c5/coverimage.webp 400w,\n/static/818e31111fb212b94c72ff9edb09bd3f/58556/coverimage.webp 800w,\n/static/818e31111fb212b94c72ff9edb09bd3f/99238/coverimage.webp 1200w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Dingu Sagar","github":"dingusagar","avatar":null}}}},{"node":{"fields":{"slug":"/engineering/guest-post/user-authentication-in-python/"},"html":"<p>Authentication and user identity management are challenging tasks you are bound to run into when building applications. For example, you will need to create profiles for users, validate provided passwords, implement a password reset functionalities, manage user sessions (sometimes on multiple devices), manage social media authentication, and many others.</p>\n<p>You still have to work on other parts of your application, and you might not have a lot of time. A lot of developers might hack their way through authentication, but that could lead to improper implementations. It is not advisable to do this as you can create doorways for cyber-related attacks in your application.</p>\n<p>In this tutorial, you will learn how to properly implement user authentication and identity management in a Flask application.</p>\n<blockquote>\n<p>Here for the code alone? Head over to the <a href=\"#integrating-loginradius-with-python-and-flask\">implementation section</a> of this article or visit this <a href=\"https://gist.github.com/LordGhostX/01e9330dc4533a992a481fcd58fdd115\">GitHub gist</a> to browse demo code.</p>\n</blockquote>\n<h2 id=\"introduction\" style=\"position:relative;\"><a href=\"#introduction\" aria-label=\"introduction permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Introduction</h2>\n<h3 id=\"what-is-user-authentication\" style=\"position:relative;\"><a href=\"#what-is-user-authentication\" aria-label=\"what is user authentication permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What Is User Authentication?</h3>\n<p>User authentication is the process of validating a person’s identity to ascertain that they are who they claim to be. Authentication is achievable using passwords, one-time pins (OTP), biometrics, authentication apps, access tokens, certificates, and many more.</p>\n<h3 id=\"what-is-user-identity\" style=\"position:relative;\"><a href=\"#what-is-user-identity\" aria-label=\"what is user identity permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What Is User Identity?</h3>\n<p>User identity is an entity used to identify a user of an application uniquely. Forms of user identifiers include full names, email addresses, system-generated values, and <a href=\"https://en.wikipedia.org/wiki/Universally_unique_identifier\">UUIDs</a>.</p>\n<h3 id=\"what-is-an-identity-provider\" style=\"position:relative;\"><a href=\"#what-is-an-identity-provider\" aria-label=\"what is an identity provider permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What Is an Identity Provider?</h3>\n<p>An identity provider is a system that helps create, maintain, and manage user identity information. It also provides authentication services to external applications to ease their authentication flow and make it seamless.</p>\n<h2 id=\"what-is-authentication-in-python\" style=\"position:relative;\"><a href=\"#what-is-authentication-in-python\" aria-label=\"what is authentication in python permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What Is Authentication in Python?</h2>\n<p>When referring to authentication in <a href=\"https://www.python.org\">Python</a>, we talk about user authentication concerning web applications built with it. Python is actively used in making web applications with many supporting frameworks, including but not limited to <a href=\"https://flask.palletsprojects.com/en/2.0.x/\">Flask</a>, Django, FastAPI, Bottle, and Hug.</p>\n<p>Every web application built with Python at one point or another would need to implement user authentication features. This article will cover implementing authentication and proper handling of user identity information using <a href=\"https://loginradius.com\">LoginRadius</a> and Flask.</p>\n<h2 id=\"getting-started-with-loginradius\" style=\"position:relative;\"><a href=\"#getting-started-with-loginradius\" aria-label=\"getting started with loginradius permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Getting Started with LoginRadius</h2>\n<h3 id=\"what-is-loginradius\" style=\"position:relative;\"><a href=\"#what-is-loginradius\" aria-label=\"what is loginradius permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What Is LoginRadius?</h3>\n<p><a href=\"https://accounts.loginradius.com/auth.aspx?action=register&#x26;return_url=https://dashboard.loginradius.com/login\">LoginRadius</a> is a cloud-based consumer identity and access management (CIAM) platform that allows seamless user authentication and SSO integration into your application. LoginRadius is simple to use, completely secure, and highly customizable.</p>\n<p>To proceed with this tutorial, you will need an account with LoginRadius. If you have not created one before now, create one on the <a href=\"https://accounts.loginradius.com/auth.aspx?action=register&#x26;return_url=https://dashboard.loginradius.com/login\">LoginRadius website</a>.</p>\n<h3 id=\"benefits-of-using-loginradius\" style=\"position:relative;\"><a href=\"#benefits-of-using-loginradius\" aria-label=\"benefits of using loginradius permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Benefits of Using LoginRadius</h3>\n<ul>\n<li>It simplifies user management.</li>\n<li>It provides unmatched data, user, and account security.</li>\n<li>It ensures automatic privacy compliance.</li>\n<li>It integrates seamlessly into your applications.</li>\n<li>It provides scalable infrastructure.</li>\n</ul>\n<h1 id=\"integrating-loginradius-with-python-and-flask\">Integrating LoginRadius with Python and Flask</h1>\n<ul>\n<li>This section covers setting up the demo application that we will be integrating LoginRadius into in the remaining parts of the tutorial.</li>\n<li>It also covers LoginRadius integration with the Python and Flask demo application code.</li>\n</ul>\n<h3 id=\"acquiring-loginradius-api-credentials\" style=\"position:relative;\"><a href=\"#acquiring-loginradius-api-credentials\" aria-label=\"acquiring loginradius api credentials permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Acquiring LoginRadius API Credentials</h3>\n<p>Login to your <a href=\"https://accounts.loginradius.com/auth.aspx?return_url=https://dashboard.loginradius.com/login\">LoginRadius dashboard</a>, then navigate to the app you want to integrate with Python (LoginRadius will set up a free app for you when you create an account).</p>\n<p><img src=\"/9f9fcb0430b89fa5c0ab46252144e277/pw6s1mqnn-yrtard7nbx.webp\" alt=\"LoginRadius Dashboard\"></p>\n<p>Next, head over to the <code>Configuration</code> tab on the LoginRadius sidebar (left side of the screen).</p>\n<p><img src=\"/ea9ed1468469fcdba3d8c0fdebf2c088/i_alrgdnugpmtschkuuj.webp\" alt=\"LoginRadius Configuration\"></p>\n<p>Your API credentials are located under the <code>API Key And Secret</code> section. Once you have retrieved this, copy the <code>APP Name</code>, <code>API Key</code>, and <code>API Secret</code> and store them somewhere secure and easily retrievable.</p>\n<p><img src=\"/1d658bc9362be0fd7ed1a643af35012e/7lev6yc_ebtxcg62wrbe.webp\" alt=\"LoginRadius API credentials\"></p>\n<h3 id=\"whitelisting-your-domains\" style=\"position:relative;\"><a href=\"#whitelisting-your-domains\" aria-label=\"whitelisting your domains permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Whitelisting Your Domains</h3>\n<p>LoginRadius requires you to whitelist domains you will be integrating with your app. To whitelist, a domain, scroll down to the <code>Whitelist Your Domain</code> section in the <code>Configuration</code> tab of your app dashboard and add it.</p>\n<p><img src=\"/689af03e0b523f8f87a86a4f38ffe91a/5ng50vbosmuhdhfuz-gi.webp\" alt=\"Domain Whitelisting\"></p>\n<blockquote>\n<p>By default, LoginRadius whitelists your local computer (localhost).</p>\n</blockquote>\n<h3 id=\"installing-loginradius-python-sdk\" style=\"position:relative;\"><a href=\"#installing-loginradius-python-sdk\" aria-label=\"installing loginradius python sdk permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Installing LoginRadius Python SDK</h3>\n<p>We need to install the LoginRadius Python SDK. It provides functionalities that allow Python programs to communicate with LoginRadius APIs.</p>\n<p>In the terminal, type:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"bash\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">pip install LoginRadius-v2 requests cryptography pbkdf2</span></span></code></pre>\n<h3 id=\"setting-up-our-flask-server\" style=\"position:relative;\"><a href=\"#setting-up-our-flask-server\" aria-label=\"setting up our flask server permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Setting up Our Flask Server</h3>\n<p>First, we need to install the Flask framework from PyPI. In the terminal, type:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"bash\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">pip install flask</span></span></code></pre>\n<p>After that, create a file named <code>server.py</code> and save the following code in it:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> flask </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> *</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">app = Flask(</span><span class=\"mtk12\">__name__</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">app.config[</span><span class=\"mtk8\">&quot;SECRET_KEY&quot;</span><span class=\"mtk1\">] = </span><span class=\"mtk8\">&quot;SECRET_KEY&quot;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">@app.route</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;/&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">index</span><span class=\"mtk1\">():</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;Hello World!&quot;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">if</span><span class=\"mtk1\"> </span><span class=\"mtk12\">__name__</span><span class=\"mtk1\"> == </span><span class=\"mtk8\">&quot;__main__&quot;</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    app.run(</span><span class=\"mtk12\">debug</span><span class=\"mtk1\">=</span><span class=\"mtk4\">True</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>When you run the <code>server.py</code> script and open your browser, you will get a response similar to the image below:</p>\n<p><img src=\"/b5ad3d364b16b60f9e8630be44e3cf84/vordrrnvz-vekwuickak.webp\" alt=\"Hello World\"></p>\n<h3 id=\"initializing-the-loginradius-sdk\" style=\"position:relative;\"><a href=\"#initializing-the-loginradius-sdk\" aria-label=\"initializing the loginradius sdk permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Initializing the LoginRadius SDK</h3>\n<p>Update the <code>server.py</code> file with the code below:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> LoginRadius </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> LoginRadius </span><span class=\"mtk15\">as</span><span class=\"mtk1\"> LR</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">LR.API_KEY = </span><span class=\"mtk8\">&quot;API Key&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">LR.API_SECRET = </span><span class=\"mtk8\">&quot;API Secret&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">loginradius = LR()</span></span></code></pre>\n<p>Replace the values of the <code>API_KEY</code> and <code>API_SECRET</code> variables with your LoginRadius application keys we saved earlier.</p>\n<h2 id=\"setting-up-user-registration\" style=\"position:relative;\"><a href=\"#setting-up-user-registration\" aria-label=\"setting up user registration permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Setting up User Registration</h2>\n<p>To register users, you have to redirect them from your application to your LoginRadius Auth Page (IDX). Each LoginRadius app has a custom IDX. You can access it with the following URL pattern.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">https://{APP_NAME}.hub.loginradius.com/auth.aspx?action={AUTH_ACTION}&return_url={RETURN_URL}</span></code></pre>\n<ul>\n<li>The <code>APP_NAME</code> parameter refers to your LoginRadius app name, which you can retrieve from the <code>API Key And Secret</code> section in the <code>Configuration</code> tab of your dashboard.</li>\n<li>The <code>AUTH_ACTION</code> parameter refers to the authentication action you’re attempting to perform. It is either <code>register</code> or <code>login</code>.</li>\n<li>The <code>RETURN_URL</code> parameter refers to the URL LoginRadius should redirect your users to after successful authentication. It is usually a route on your application server.</li>\n</ul>\n<p>Update the <code>server.py</code> file with the code below:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">LR_AUTH_PAGE = </span><span class=\"mtk8\">&quot;https://&lt;APP_NAME&gt;.hub.loginradius.com/auth.aspx?action=</span><span class=\"mtk4\">{}</span><span class=\"mtk8\">&return_url=</span><span class=\"mtk4\">{}</span><span class=\"mtk8\">&quot;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">@app.route</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;/register/&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">register</span><span class=\"mtk1\">():</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk3\"># redirect the user to our LoginRadius register URL</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> redirect(LR_AUTH_PAGE.format(</span><span class=\"mtk8\">&quot;register&quot;</span><span class=\"mtk1\">, request.host_url))</span></span></code></pre>\n<p>In the code above, we created a <code>register</code> route that redirects users to our LoginRadius registration IDX. We also set our <code>AUTH_ACTION</code> to “register” and our <code>RETURN_URL</code> to our application home page.</p>\n<p><img src=\"/79192f0d84a9ebca455ede5522497ab9/smn-8jr5ahgmhtynezje.webp\" alt=\"LoginRadius Login Page\"></p>\n<blockquote>\n<p>NOTE: Don’t forget to replace the &#x3C;APP_NAME> placeholder with your LoginRadius app name we saved earlier.</p>\n</blockquote>\n<h3 id=\"authenticating-registered-users-user-login\" style=\"position:relative;\"><a href=\"#authenticating-registered-users-user-login\" aria-label=\"authenticating registered users user login permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Authenticating Registered Users (User Login)</h3>\n<p>To authenticate registered users, you have to redirect them to your IDX page, passing “login” as the <code>AUTH_ACTION</code>.</p>\n<p>Update the <code>server.py</code> file with the code below:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">@app.route</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;/login/&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">login</span><span class=\"mtk1\">():</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    access_token = request.args.get(</span><span class=\"mtk8\">&quot;token&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> access_token </span><span class=\"mtk4\">is</span><span class=\"mtk1\"> </span><span class=\"mtk4\">None</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk3\"># redirect the user to our LoginRadius login URL if no access token is provided</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> redirect(LR_AUTH_PAGE.format(</span><span class=\"mtk8\">&quot;login&quot;</span><span class=\"mtk1\">, request.base_url))</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;You have successfully logged in!&quot;</span></span></code></pre>\n<blockquote>\n<p>When LoginRadius successfully authenticates a user, it attaches a <code>token</code> parameter to the <code>REDIRECT_URL</code> before redirecting your user there. This parameter contains the <a href=\"https://en.wikipedia.org/wiki/Access_token\">access token</a> of the user that we authenticated.</p>\n</blockquote>\n<p>In the code above, we redirect users to our LoginRadius login IDX if the <code>token</code> parameter is absent (this means LoginRadius did not redirect the user here). We also set our <code>AUTH_ACTION</code> to “login” and our <code>RETURN_URL</code> to our login page.</p>\n<p><img src=\"/8624b9e29ef548d70f975ecf65a31f92/7gs3xz6qhxhas7qgtjma.webp\" alt=\"LoginRadius Login Page\"></p>\n<p><img src=\"/30d20a60f432e9415ebaf7645af42f80/xwzztcogdtmnyfq4j5nc.webp\" alt=\"LoggedIn\"></p>\n<h2 id=\"fetching-user-profiles-from-access-tokens\" style=\"position:relative;\"><a href=\"#fetching-user-profiles-from-access-tokens\" aria-label=\"fetching user profiles from access tokens permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Fetching User Profiles From Access Tokens</h2>\n<p>We also want to fetch user profiles from the <code>access token</code> given by LoginRadius. It comes in handy when we want to verify if a given access token is valid (or has expired) or just fetch information about the current user.</p>\n<p>Update the <code>login</code> route with the code below. We also added a <code>dashboard</code> route where we will redirect users after successful authentication.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">@app.route</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;/login/&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">login</span><span class=\"mtk1\">():</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    access_token = request.args.get(</span><span class=\"mtk8\">&quot;token&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> access_token </span><span class=\"mtk4\">is</span><span class=\"mtk1\"> </span><span class=\"mtk4\">None</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk3\"># redirect the user to our LoginRadius login URL if no access token is provided</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> redirect(LR_AUTH_PAGE.format(</span><span class=\"mtk8\">&quot;login&quot;</span><span class=\"mtk1\">, request.base_url))</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk3\"># fetch the user profile details with their access tokens</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    result = loginradius.authentication.get_profile_by_access_token(</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        access_token)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> result.get(</span><span class=\"mtk8\">&quot;ErrorCode&quot;</span><span class=\"mtk1\">) </span><span class=\"mtk4\">is</span><span class=\"mtk1\"> </span><span class=\"mtk4\">not</span><span class=\"mtk1\"> </span><span class=\"mtk4\">None</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk3\"># redirect the user to our login URL if there was an error</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> redirect(url_for(</span><span class=\"mtk8\">&quot;login&quot;</span><span class=\"mtk1\">))</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    session[</span><span class=\"mtk8\">&quot;user_acccess_token&quot;</span><span class=\"mtk1\">] = access_token</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> redirect(url_for(</span><span class=\"mtk8\">&quot;dashboard&quot;</span><span class=\"mtk1\">))</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">@app.route</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;/dashboard/&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">dashboard</span><span class=\"mtk1\">():</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;You have successfully logged in!&quot;</span></span></code></pre>\n<p>In the code above, we used the <code>authentication.get_profile_by_access_token</code> method from the LoginRadius SDK to fetch our user’s details. If the request was successful and the result does not contain an <code>ErrorCode</code> parameter, we save the access token in the user’s session and redirect them to the <code>dashboard</code> route. But if an error occurs somewhere, e.g., the access token is invalid/expired, we redirect the user back to the <code>login</code> route.</p>\n<p><img src=\"/d15eed00435b16b1d05cbb201630c333/f2p7ddnwin3yihucx2em.webp\" alt=\"LoggedIn\"></p>\n<p>Next, we want to add more functionality to the <code>dashboard</code> route. Instead of just displaying a dummy text, let it show the user information we fetched earlier. Update the <code>dashboard</code> route with the code below:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">@app.route</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;/dashboard/&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">dashboard</span><span class=\"mtk1\">():</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    access_token = session.get(</span><span class=\"mtk8\">&quot;user_acccess_token&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> access_token </span><span class=\"mtk4\">is</span><span class=\"mtk1\"> </span><span class=\"mtk4\">None</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> redirect(url_for(</span><span class=\"mtk8\">&quot;login&quot;</span><span class=\"mtk1\">))</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk3\"># fetch the user profile details with their access tokens</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    result = loginradius.authentication.get_profile_by_access_token(</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        access_token)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> result.get(</span><span class=\"mtk8\">&quot;ErrorCode&quot;</span><span class=\"mtk1\">) </span><span class=\"mtk4\">is</span><span class=\"mtk1\"> </span><span class=\"mtk4\">not</span><span class=\"mtk1\"> </span><span class=\"mtk4\">None</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk3\"># redirect the user to our login URL if there was an error</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> redirect(url_for(</span><span class=\"mtk8\">&quot;login&quot;</span><span class=\"mtk1\">))</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> jsonify(result)</span></span></code></pre>\n<p>Here, we fetched the access token stored in the user’s session earlier, used it to get their details, and rendered the result.</p>\n<p><img src=\"/ed807cbc18a2c163fbbce57eb7789e5c/1zsvbg3rk013zlbpxx2u.webp\" alt=\"LoggedIn\"></p>\n<h3 id=\"invalidating-access-tokens-user-logout\" style=\"position:relative;\"><a href=\"#invalidating-access-tokens-user-logout\" aria-label=\"invalidating access tokens user logout permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Invalidating Access Tokens (User Logout)</h3>\n<p>Invalidating access tokens means rendering particular access tokens useless and unusable. It comes in handy when we log out users. The LoginRadius SDK provides an <code>auth_in_validate_access_token</code> method that takes in an access token to be invalidated.</p>\n<p>To add this to our server, create a <code>logout</code> route with the code below:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">@app.route</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;/logout/&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">logout</span><span class=\"mtk1\">():</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    access_token = session.get(</span><span class=\"mtk8\">&quot;user_acccess_token&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> access_token </span><span class=\"mtk4\">is</span><span class=\"mtk1\"> </span><span class=\"mtk4\">None</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> redirect(url_for(</span><span class=\"mtk8\">&quot;login&quot;</span><span class=\"mtk1\">))</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk3\"># invalidate the access token with LoginRadius API</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    loginradius.authentication.auth_in_validate_access_token(access_token)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    session.clear()</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;You have successfully logged out!&quot;</span></span></code></pre>\n<p><img src=\"/bc9fb664300826a7397cfb8826284223/xyvodwpjtxjrjgxoffx5.webp\" alt=\"Log Out\"></p>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>This article taught us about user authentication, user identity management, and implementing it correctly. In addition, we saw how easy it is to integrate LoginRadius services into a Python application to ease the implementation of authentication and user identity management.</p>\n<p>The source code of the demo application is available as a <a href=\"https://gist.github.com/LordGhostX/01e9330dc4533a992a481fcd58fdd115\">GitHub gist</a>. You can learn more about the LoginRadius Python SDK features from the <a href=\"https://www.loginradius.com/developers/\">official documentation</a>.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n</style>","frontmatter":{"date":"July 07, 2021","updated_date":null,"title":"Implementing User Authentication in a Python Application","tags":["Python","Authentication","Flask"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/2005b7a832c4ee0e2301194e9f8eb64b/58556/coverImage.webp","srcSet":"/static/2005b7a832c4ee0e2301194e9f8eb64b/61e93/coverImage.webp 200w,\n/static/2005b7a832c4ee0e2301194e9f8eb64b/1f5c5/coverImage.webp 400w,\n/static/2005b7a832c4ee0e2301194e9f8eb64b/58556/coverImage.webp 800w,\n/static/2005b7a832c4ee0e2301194e9f8eb64b/99238/coverImage.webp 1200w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Solomon Esenyi","github":"LordGhostX","avatar":null}}}},{"node":{"fields":{"slug":"/engineering/python-basics-in-minutes/"},"html":"<p>Python is a general-purpose programming language and has overtaken Java in popularity according to a recent Stackoverflow survey. </p>\n<p>People who have never programmed before are tempted to try due to the simplicity to learn and use it. Well, let's get down to business.</p>\n<h2 id=\"before-we-get-started\" style=\"position:relative;\"><a href=\"#before-we-get-started\" aria-label=\"before we get started permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Before we get started</h2>\n<p>Before we get started, there are a few things you should know about python:</p>\n<ul>\n<li>Python is a high-level programming language, which means it has a strong abstraction from the computer's details (that's why it's so easy and understandable). Because of that, it may be not so efficient as other languages like assembly, C, or C++;</li>\n<li>Python is an interpreted language. Its syntax is read and then executed directly. The interpreter reads each program statement, following the program flow, then decides what to do and does it. That's why you should test all your programs, even if everything seems to be working correctly. If there is an error within a loop, for example, it will only be shown if the loop is executed;</li>\n<li>Python has excellent documentation <a href=\"https://docs.python.org/3/\">that you can access here</a> and an incredible community. Use them.</li>\n</ul>\n<h2 id=\"print-function\" style=\"position:relative;\"><a href=\"#print-function\" aria-label=\"print function permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Print function</h2>\n<p><em>We are assuming that you have already installed python. If you do not, please click <a href=\"https://www.python.org/downloads/\">here</a>.</em></p>\n<p>To write your first Python code, open your text editor and type: </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">print(&quot;Hello world, this is my first python code&quot;)</span></code></pre>\n<p>Save the file as <code>helloworld.py</code> and put it into the python interpreter to be executed.\nYou also can run your code on the command line:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">C:\\Users\\Your Name&gt;python helloworld.py</span></code></pre>\n<p>If everything went well, you should see something like this:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt; Hello world, this is my first python code</span></code></pre>\n<p>You can also use the print function to show integers, variables, lists, etc. Try it!</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\">#show the sum of two integers</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">number1 = </span><span class=\"mtk7\">5</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">number2 = </span><span class=\"mtk7\">7</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(number1+number2)</span></span></code></pre>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\">#dividing two decimals</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">number1 = </span><span class=\"mtk10\">float</span><span class=\"mtk1\">(</span><span class=\"mtk7\">12.1</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">number2 = </span><span class=\"mtk10\">float</span><span class=\"mtk1\">(</span><span class=\"mtk7\">4</span><span class=\"mtk1\">)</span></span></code></pre>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\">#concatenating strings</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">name = </span><span class=\"mtk8\">&quot;Python&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">phrase = </span><span class=\"mtk10\">str</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;I hate &quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;I love &quot;</span><span class=\"mtk1\">, name)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(phrase+name)</span></span></code></pre>\n<h2 id=\"data-types\" style=\"position:relative;\"><a href=\"#data-types\" aria-label=\"data types permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Data Types</h2>\n<p>Python has the following data types built-in by default, in these categories:</p>\n<ul>\n<li>Text Type:\t<code>str</code> <em>(string)</em></li>\n<li>Numeric Types:\t<code>int</code> <em>(integer)</em>, <code>float</code> <em>(decimal)</em>, <code>complex</code></li>\n<li>Sequence Types:\t<code>list</code>, <code>tuple</code>, <code>range</code></li>\n<li>Mapping Type:\t<code>dict</code></li>\n<li>Set Types:\t<code>set</code>, <code>frozenset</code></li>\n<li>Boolean Type:\t<code>bool</code></li>\n<li>Binary Types:\t<code>bytes</code>, <code>bytearray</code>, <code>memoryview</code></li>\n</ul>\n<blockquote>\n<p><strong>Note:</strong> Variables in Python are interpreted as integers by default. Is a good practice to declare them as another type <em>explicitly</em> (if they aren't integers). You can see the kind of a variable using the function <code>type()</code>.</p>\n</blockquote>\n<h2 id=\"python-operators\" style=\"position:relative;\"><a href=\"#python-operators\" aria-label=\"python operators permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Python Operators</h2>\n<p>Operators are used to performing operations on variables and values.\nThe main groups are:</p>\n<ul>\n<li>Arithmetic operators</li>\n<li>Comparison operators</li>\n<li>Logical operators</li>\n</ul>\n<h3 id=\"arithmetic-operators\" style=\"position:relative;\"><a href=\"#arithmetic-operators\" aria-label=\"arithmetic operators permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Arithmetic operators</h3>\n<p><em>Arithmetic operators are used with numeric values to perform common mathematical operations.</em></p>\n<ul>\n<li><strong>Addition</strong>:\t<code>x + y</code></li>\n<li><strong>Subtraction:</strong> <code>x - y</code></li>\n<li><strong>Multiplication:</strong> <code>x * y</code></li>\n<li><strong>Division:</strong> <code>x / y</code></li>\n<li><strong>Modulus:</strong> <code>x % y</code>\t</li>\n<li><strong>Exponentiation:</strong> <code>x ** y</code>\t</li>\n<li><strong>Floor division:</strong> <code>x // y</code></li>\n</ul>\n<h3 id=\"comparison-operators\" style=\"position:relative;\"><a href=\"#comparison-operators\" aria-label=\"comparison operators permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Comparison operators</h3>\n<p><em>Comparison operators are used to compare two values.</em></p>\n<ul>\n<li><strong>Equal:</strong> <code>x == y</code></li>\n<li><strong>Not equal:</strong> <code>x != y</code></li>\n<li><strong>Greater than:</strong> <code>x > y</code></li>\n<li><strong>Less than:</strong> <code>x &#x3C; y</code>\t</li>\n<li><strong>Greater than or equal to:</strong> <code>x >= y</code></li>\n<li><strong>Less than or equal to:</strong>\t<code>x &#x3C;= y</code></li>\n</ul>\n<h3 id=\"logical-operators\" style=\"position:relative;\"><a href=\"#logical-operators\" aria-label=\"logical operators permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Logical operators</h3>\n<p><em>Logical operators are used to combine conditional statements.</em></p>\n<ul>\n<li><strong>and</strong> -> Returns True if both statements are true\n<code>x &#x3C; 2 and x &#x3C; 4</code></li>\n<li><strong>or</strong> ->\tReturns True if one of the statements is true\n<code>x &#x3C; 10 or x &#x3C; 9</code></li>\n<li><strong>not</strong> -> Reverse the result, returns False if the result is true\n<code>not(x &#x3C; 2 and x &#x3C; 4)</code></li>\n</ul>\n<h2 id=\"if-statement\" style=\"position:relative;\"><a href=\"#if-statement\" aria-label=\"if statement permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>If statement</h2>\n<p>Look to the code below:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">age = </span><span class=\"mtk7\">18</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">if</span><span class=\"mtk1\"> (age&lt;</span><span class=\"mtk7\">21</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;The student is underage!&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">elif</span><span class=\"mtk1\"> (age==</span><span class=\"mtk7\">21</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;The student is 21!&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">else</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;The student isn&#39;t underage!&quot;</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>In programming, we often have to choose what to do depending on the situation. It is essential to know how to use conditional arguments like <code>if</code>and <code>else</code>.\nThe code above print a different message according to a condition.</p>\n<p>Try to write a code that asks for two test scores. If the average is less than 7, the user should see \"I'm sorry, you didn't do well on the tests\". If the average is exactly 7, the user should see \"You did it!\". And if it is greater than 7, the user should see \"Congratulations!! You're a great student.\".</p>\n<blockquote>\n<p><strong>Note:</strong> to request a response from the user, you need to use the <code>input()</code> function. For example:</p>\n</blockquote>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">name = </span><span class=\"mtk10\">str</span><span class=\"mtk1\">(</span><span class=\"mtk11\">input</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;What&#39;s your name?&quot;</span><span class=\"mtk1\">))</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">age = </span><span class=\"mtk10\">int</span><span class=\"mtk1\">(</span><span class=\"mtk11\">input</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;How old are you? &quot;</span><span class=\"mtk1\">))</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;I&#39;m </span><span class=\"mtk4\">%s</span><span class=\"mtk8\"> and I&#39;m </span><span class=\"mtk4\">%d</span><span class=\"mtk8\">&quot;</span><span class=\"mtk1\">% (name, age))</span></span></code></pre>\n<h2 id=\"loops\" style=\"position:relative;\"><a href=\"#loops\" aria-label=\"loops permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Loops</h2>\n<p>Python has two primitive loop commands: <code>while</code> and <code>for</code>.</p>\n<h3 id=\"while-loop\" style=\"position:relative;\"><a href=\"#while-loop\" aria-label=\"while loop permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>While loop</h3>\n<p><em>With the while loop, we can execute a set of statements as long as a condition is true.</em></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">count = </span><span class=\"mtk7\">0</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">while</span><span class=\"mtk1\"> count &lt; </span><span class=\"mtk7\">10</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(count)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    count += </span><span class=\"mtk7\">1</span><span class=\"mtk1\">    </span><span class=\"mtk3\">#this line is the same as    count = count+1</span></span></code></pre>\n<p>The code above will print count as long as count is less than 10.</p>\n<h3 id=\"for-loop\" style=\"position:relative;\"><a href=\"#for-loop\" aria-label=\"for loop permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>For loop</h3>\n<p>A for loop is used for iterating over a sequence. Using it, we can execute a set of statements, once for each item in a list, tuple, set, string, etc. For example:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\">#loop through a string</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> x </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;banana&quot;</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(x)</span></span></code></pre>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"10\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\">#loop through a list of fruits</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">fruits = [</span><span class=\"mtk8\">&quot;apple&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&quot;banana&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&quot;melon&quot;</span><span class=\"mtk1\">]</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> x </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> fruits:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;I like&quot;</span><span class=\"mtk1\">, x)</span></span></code></pre>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>Did you understand why python became so popular? In a few minutes, you were able to learn the main concepts of this fantastic language.</p>\n<p>Please comment and share this article!</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n</style>","frontmatter":{"date":"November 06, 2020","updated_date":null,"title":"Python basics in minutes","tags":["Python"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/e65362be2b3196aeea5b79ea97dab3a6/58556/python.webp","srcSet":"/static/e65362be2b3196aeea5b79ea97dab3a6/61e93/python.webp 200w,\n/static/e65362be2b3196aeea5b79ea97dab3a6/1f5c5/python.webp 400w,\n/static/e65362be2b3196aeea5b79ea97dab3a6/58556/python.webp 800w,\n/static/e65362be2b3196aeea5b79ea97dab3a6/99238/python.webp 1200w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Sara Lins","github":"saranicoly","avatar":null}}}},{"node":{"fields":{"slug":"/engineering/beginners-guide-to-tweepy/"},"html":"<p>As we all know, Twitter is one of the most preferred microblogging websites when it comes to putting your thoughts on the internet. Big organizations use this platform for advertising their product; government institutions even use it to provide prompt customer resolution; various groups use Twitter to run social awareness campaigns and media campaigns. Twitter has close to 330 million monthly active users worldwide, out of which 17 million hits are generated from India itself along with more than 1 billion downloads on Play Store. With all this in mind, Twitter allows access to Twitter API to developers to create some cool applications like bots, automation tools, etc. This Twitter API gives developers access to almost all of Twitter's functionalities like likes, retweets, tweets, etc. Tweepy, a python package, helps us in achieving all this.</p>\n<p>Tweepy is a python package that smoothly and transparently accesses Twitter's endpoints made available for the developers. Without Tweepy, the user would have to take care of various low-level details about HTTP requests, rate limiting, authentication, serialization, etc. Tweepy handles all this mess on behalf of the user making the application prone to errors.\nIn simple words, Tweepy is an open-source python package that provides a way for developers to communicate with the Twitter API. But keep in mind that Twitter levies a rate limit on the number of requests made to the Twitter API. To be precise, 900 requests/15 minutes are allowed; Twitter feeds anything above that an error.</p>\n<h3 id=\"installation\" style=\"position:relative;\"><a href=\"#installation\" aria-label=\"installation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Installation</h3>\n<p>Tweepy can be installed by using Python package manager <strong>pip</strong>. A simple demonstration can be seen below:\n<img src=\"/8400326bc3a2fdc039d749397d750ee8/installation.webp\" alt=\"installation\" title=\"Installation\"></p>\n<p>Installation on Linux and macOS should follow similar steps as well.</p>\n<p>Twitter API uses OAuth for authentication, so initially, you need to apply for authentication credentials from Twitter. These authentication credentials basically consists of 4 components namely : <em>consumer</em>key, consumer<em>secret</em>key, access<em>token, access</em>token<em>secret</em> . These credentials from Twitter are used to instantiate the API. <strong>Each account gets a unique key, so don't reuse someone else's keys.</strong>\nFor getting those credentials from Twitter, apply for a developer account on the <a href=\"https://developer.twitter.com/en\">Twitter Developers</a> page.\n<img src=\"/a2766dccfa074e3f507c089a3fdd370d/twitterdev.webp\" alt=\"twitterdev\" title=\"Twitter Dev Dashboard\">\nThis is what the account looks like. Here you will get detailed information about the total no. of requests made, your API credentials, and much more information. After creating an account, you need to create an app wherein you will be asked to name your app and a short description. You must be wondering what an app is?\nThe app is like a gateway that contains a set of permissions and keys used to access the Twitter API. An app is needed for accessing the Twitter API as a part of Twitter's OAuth authentication. After creating an app, generate new authentication tokens for authorization purposes.</p>\n<p><img src=\"/fde857dd8c739c14bfa27571d14bc364/keys.webp\" alt=\"keys\"></p>\n<h3 id=\"getting-started-tweepy\" style=\"position:relative;\"><a href=\"#getting-started-tweepy\" aria-label=\"getting started tweepy permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Getting Started: Tweepy</h3>\n<p>There are four common basic steps in any Tweepy application.</p>\n<ol>\n<li>Importing tweepy package.</li>\n<li>Setting the authentication credentials.</li>\n<li>Instantiating the API.</li>\n<li>Creating API object.</li>\n</ol>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> tweepy</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># authenticating twitter api credentials</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">consumer_key = </span><span class=\"mtk8\">&#39;2OsNoPKOYCpxxxxxxxxxxxxxxxxxxxxxx&#39;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">consumer_secret = </span><span class=\"mtk8\">&#39;Xw07uU51xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#39;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">access_token = </span><span class=\"mtk8\">&#39;24621057xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#39;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">access_token_secret = </span><span class=\"mtk8\">&#39;pXt5xxxxxxxxxxxxxxxxxxxxxxxxxxxx&#39;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># instantiating the api</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">auth = tweepy.OAuthHandler(consumer_key, consumer_secret)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">auth.set_access_token(access_token, access_token_secret)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># creating API object</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">api = tweepy.API(auth,</span><span class=\"mtk12\">wait_on_rate_limit</span><span class=\"mtk1\">=</span><span class=\"mtk4\">True</span><span class=\"mtk1\">,</span><span class=\"mtk12\">wait_on_rate_limit_notify</span><span class=\"mtk1\">=</span><span class=\"mtk4\">True</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> tabulate</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">user = api.get_user(</span><span class=\"mtk8\">&quot;Cristiano&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;User Details:&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(tabulate([[</span><span class=\"mtk8\">&quot;Name&quot;</span><span class=\"mtk1\">,</span><span class=\"mtk8\">&quot;Description&quot;</span><span class=\"mtk1\">,</span><span class=\"mtk8\">&quot;Location&quot;</span><span class=\"mtk1\">],[user.name,user.description,user.location]],</span><span class=\"mtk12\">headers</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;firstrow&quot;</span><span class=\"mtk1\">))</span></span></code></pre>\n<blockquote>\n<p>Note: Use your credentials in the hidden keys above._</p>\n</blockquote>\n<p>Objects created from the Tweepy. API class helps us access most of Twitter's available functionality like tweets, retweets, likes, etc. In the code snippet, we used the api.get_user method for getting information about a certain user on Twitter. Likewise, there can be several use cases of different methods(discussed below) made available by Twitter to developers. You can find the link for this code <a href=\"https://colab.research.google.com/drive/1dN02ioXElOQPOktIzNBACCncyrI2eiBR?usp=sharing\">here.</a></p>\n<p>Now we will see different methods provided by Twitter. The API methods have been divided into groups based on their functionality. The detailed guide for the API methods can be found at the official <a href=\"https://tweepy.readthedocs.io/en/latest/api.html\">API Reference</a> documentation. </p>\n<ul>\n<li>Timeline methods</li>\n<li>Status methods</li>\n<li>User methods</li>\n<li>Direct Message Methods</li>\n<li>Friendship Methods</li>\n<li>Favorite Methods</li>\n<li>Block Methods</li>\n<li>Search Methods</li>\n<li>Trends Methods</li>\n<li>Geo Methods</li>\n</ul>\n<h4 id=\"timeline-methods\" style=\"position:relative;\"><a href=\"#timeline-methods\" aria-label=\"timeline methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Timeline Methods</h4>\n<p>These methods handles the tweets, retweets, statuses on your/someone else's timeline as long as the account is public.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">tweets = api.home_timeline()</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> tweet </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> tweets:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk4\">f</span><span class=\"mtk8\">&quot;</span><span class=\"mtk4\">{</span><span class=\"mtk1\">tweet.user.name</span><span class=\"mtk4\">}</span><span class=\"mtk8\"> said </span><span class=\"mtk4\">{</span><span class=\"mtk1\">tweet.text</span><span class=\"mtk4\">}</span><span class=\"mtk8\">&quot;</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>api.home_timeline() is an API method that returns the 20 most recent tweets on the user's timeline.</p>\n<h4 id=\"status-methods\" style=\"position:relative;\"><a href=\"#status-methods\" aria-label=\"status methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Status Methods</h4>\n<p>These methods deal with creating, fetching tweets, retweeting tweets.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">api.update_status(</span><span class=\"mtk8\">&quot;Hello World!&quot;</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>api.update_status() is an API method used to create a tweet on the user's timeline. For each update request, it will check the user's recent tweets. If any duplication is found, the request will be blocked by Twitter as a user cannot post the same tweet more than once.</p>\n<h4 id=\"user-methods\" style=\"position:relative;\"><a href=\"#user-methods\" aria-label=\"user methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>User Methods</h4>\n<p>These methods help to find the user details using various paramaeter like name, location, description,friends, followers,etc. as long as the account is public. </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">user = api.get_user(</span><span class=\"mtk8\">&quot;ISRO&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(user.name)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(user.decscription)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(user.followers)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(user.location)</span></span></code></pre>\n<p>We have used get_user() previously to fetch user details of specific accounts.</p>\n<h4 id=\"friendship-methods\" style=\"position:relative;\"><a href=\"#friendship-methods\" aria-label=\"friendship methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Friendship Methods</h4>\n<p>These methods help the user to follow, unfollow certain accounts, list the accounts user follows, etc.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">api.create_friendship(</span><span class=\"mtk8\">&quot;elonmusk&quot;</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>create_friendship() will add @elonmusk to the list of accounts you follow.</p>\n<h4 id=\"favorite-methods\" style=\"position:relative;\"><a href=\"#favorite-methods\" aria-label=\"favorite methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Favorite Methods</h4>\n<p>Likes or unlikes(if already liked) the status specified in the ID parameter as the authenticating user.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">api.create_favorite(tweet.id)</span></span></code></pre>\n<p>create_favorite() will like a tweet based on the tweet id provided.</p>\n<h4 id=\"block-methods\" style=\"position:relative;\"><a href=\"#block-methods\" aria-label=\"block methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Block Methods</h4>\n<p>Used to block, unblock, list blocked accounts of the user.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">api.create_block(</span><span class=\"mtk11\">id</span><span class=\"mtk1\">/screen_name)</span></span></code></pre>\n<p>create<em>block() will block the specific user using the id/screen</em>name provided. </p>\n<h4 id=\"search-methods\" style=\"position:relative;\"><a href=\"#search-methods\" aria-label=\"search methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Search Methods</h4>\n<p>These methods help the user to search specific tweet based upon the search query and parameters provided. But not all tweets will be indexed or made available through the search methods.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> tweets </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> api.search(</span><span class=\"mtk12\">q</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;iphone&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">lang</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;en&quot;</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(tweet.text)</span></span></code></pre>\n<p>search() will look out for all the tweets available for the query keyword 'q' provided.</p>\n<h4 id=\"trends-methods\" style=\"position:relative;\"><a href=\"#trends-methods\" aria-label=\"trends methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Trends Methods</h4>\n<p>It returns the trends going on at a specific geographical location.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">api.trends_place(</span><span class=\"mtk7\">1</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>trends_place() will show trends in the specific area. Here one stands for worldwide.</p>\n<h4 id=\"geo-methods\" style=\"position:relative;\"><a href=\"#geo-methods\" aria-label=\"geo methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Geo Methods</h4>\n<p>It returns the geographical location like latitude, the longitude of the place id provided.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">api.geo_id(</span><span class=\"mtk11\">id</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>geo_id() returns more geographical information of the concerned place id.</p>\n<h3 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h3>\n<p>Tweepy allows the user to concentrate on the application's logic by hiding many low-level details, thus making the application bug-free. You can use tweepy to do some cool projects like bots, automation, machine learning applications, etc. </p>\n<h5 id=\"ket-takeaways\" style=\"position:relative;\"><a href=\"#ket-takeaways\" aria-label=\"ket takeaways permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Ket Takeaways</h5>\n<ul>\n<li>What is Tweepy?</li>\n<li>Installation</li>\n<li>Getting Started with Tweepy</li>\n<li>Various API methods</li>\n</ul>\n<p>More information about tweepy can be found at <a href=\"https://tweepy.readthedocs.io/en/latest/index.html\">docs.</a> Make sure to look at the official documentation as it will provide you with a greater picture of the package. So what are you waiting for? Go ahead, use your imagination, and get started with the side-project you've always been thinking of.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n</style>","frontmatter":{"date":"November 02, 2020","updated_date":null,"title":"Beginners Guide to Tweepy ","tags":["Twitter","Tweepy","Python"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/f706bedc2cff52e9c0f73c8e799ee39c/58556/coverimage.webp","srcSet":"/static/f706bedc2cff52e9c0f73c8e799ee39c/61e93/coverimage.webp 200w,\n/static/f706bedc2cff52e9c0f73c8e799ee39c/1f5c5/coverimage.webp 400w,\n/static/f706bedc2cff52e9c0f73c8e799ee39c/58556/coverimage.webp 800w,\n/static/f706bedc2cff52e9c0f73c8e799ee39c/99238/coverimage.webp 1200w,\n/static/f706bedc2cff52e9c0f73c8e799ee39c/7c22d/coverimage.webp 1600w,\n/static/f706bedc2cff52e9c0f73c8e799ee39c/25f09/coverimage.webp 1920w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Sameer Mahajan","github":"sameermahajan101","avatar":null}}}},{"node":{"fields":{"slug":"/engineering/how-to-make-telegram-bot/"},"html":"<h2 id=\"what-is-bot\" style=\"position:relative;\"><a href=\"#what-is-bot\" aria-label=\"what is bot permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is Bot?</h2>\n<p>According to Wikipedia, It is a software application that runs automated tasks (scripts) over the Internet. In other terms, It is a program written to mimic human behaviour in order to perform some tasks. Bots take input and do simple and repetitive tasks, which is much faster than humans. </p>\n<p>There are two types of bots:</p>\n<ul>\n<li>Good Bots: Bots that are  beneficial to organizations as well as individuals such as ChatBots, Social Media bots, etc</li>\n<li>Bad Bots: These are the bots that are used to perform malicious activities such as Scraping and Spamming.</li>\n</ul>\n<p>In this, We will make Telegram Chatbot, which would send a copy of the input that the user has sent.</p>\n<h2 id=\"making-our-telegram-chatbot\" style=\"position:relative;\"><a href=\"#making-our-telegram-chatbot\" aria-label=\"making our telegram chatbot permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Making our Telegram Chatbot</h2>\n<p>We will be using <strong>Python language</strong> to make the bot and will be using <strong>Telegram package</strong> for our bot.\nYou can know more about the telegram package from <a href=\"https://python-telegram-bot.readthedocs.io/en/stable/telegram.html\">here</a></p>\n<p>Firstly, we will generate our telegram token. Below are the steps to follow to generate your own token.</p>\n<ol>\n<li>Search BotFather on Telegram.</li>\n<li>Type <strong>/start</strong> to get started.</li>\n<li>Type <strong>/newbot</strong> to get a bot.</li>\n<li>Enter your Bot name and unique Username, which should end with the bot.</li>\n<li>Then, you would get your Bot token.</li>\n</ol>\n<p>After generating our token, we will make a python program to create a Telegram bot that will send the Text, Emojis, and Stickers similar to the user's input.</p>\n<ul>\n<li>Importing libraries required.</li>\n</ul>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> logging</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> telegram.ext </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> Updater, Filters, CommandHandler, MessageHandler</span></span></code></pre>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">logging.basicConfig(</span><span class=\"mtk12\">format</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&#39;</span><span class=\"mtk4\">%(asctime)s</span><span class=\"mtk8\"> - </span><span class=\"mtk4\">%(name)s</span><span class=\"mtk8\"> - </span><span class=\"mtk4\">%(levelname)s</span><span class=\"mtk8\"> - </span><span class=\"mtk4\">%(message)s</span><span class=\"mtk8\">&#39;</span><span class=\"mtk1\">,     </span><span class=\"mtk3\">#take time,level,name</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    </span><span class=\"mtk12\">level</span><span class=\"mtk1\">=logging.INFO)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">logger = logging.getLogger(</span><span class=\"mtk12\">__name__</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>This script does a basic configuration for the logging system. It takes time, level, and name.</p>\n<ul>\n<li>Now, we will make a variable Token, which will take your Telegram token.</li>\n</ul>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">TOKEN = </span><span class=\"mtk8\">&quot;ENTER YOUR TOKEN&quot;</span><span class=\"mtk1\">  </span></span></code></pre>\n<ul>\n<li>After writing the token, we will make some functions that our bot will perform.</li>\n</ul>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">start</span><span class=\"mtk1\">(</span><span class=\"mtk12\">bot</span><span class=\"mtk1\">,</span><span class=\"mtk12\">update</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    name  = update.message.from_user.first_name  </span><span class=\"mtk3\">#first name of the user messaging</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    reply = </span><span class=\"mtk8\">&quot;Hi!! </span><span class=\"mtk4\">{}</span><span class=\"mtk8\">&quot;</span><span class=\"mtk1\">.format(name)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    bot.send_message(</span><span class=\"mtk12\">chat_id</span><span class=\"mtk1\"> = update.message.chat_id, </span><span class=\"mtk12\">text</span><span class=\"mtk1\"> = reply)      </span><span class=\"mtk3\">#sending message</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">help</span><span class=\"mtk1\">(</span><span class=\"mtk12\">bot</span><span class=\"mtk1\">,</span><span class=\"mtk12\">update</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    reply = </span><span class=\"mtk8\">&quot;How can I help You&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    bot.send_message(</span><span class=\"mtk12\">chat_id</span><span class=\"mtk1\"> = update.message.chat_id, </span><span class=\"mtk12\">text</span><span class=\"mtk1\"> = reply)  </span><span class=\"mtk3\">#sending message</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">echo_text</span><span class=\"mtk1\">(</span><span class=\"mtk12\">bot</span><span class=\"mtk1\">,</span><span class=\"mtk12\">update</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    reply = update.message.text</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    bot.send_message(</span><span class=\"mtk12\">chat_id</span><span class=\"mtk1\"> = update.message.chat_id, </span><span class=\"mtk12\">text</span><span class=\"mtk1\"> = reply)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">sticker</span><span class=\"mtk1\">(</span><span class=\"mtk12\">bot</span><span class=\"mtk1\">,</span><span class=\"mtk12\">update</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    reply = update.message.sticker.file_id</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    bot.send_sticker(</span><span class=\"mtk12\">chat_id</span><span class=\"mtk1\"> = update.message.chat_id, </span><span class=\"mtk12\">sticker</span><span class=\"mtk1\"> = reply)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">error</span><span class=\"mtk1\">(</span><span class=\"mtk12\">bot</span><span class=\"mtk1\">,</span><span class=\"mtk12\">update</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    logger.error(</span><span class=\"mtk8\">&quot;Shit!! Update </span><span class=\"mtk4\">{}</span><span class=\"mtk8\"> caused error </span><span class=\"mtk4\">{}</span><span class=\"mtk8\">&quot;</span><span class=\"mtk1\">.format(update,update.error))</span></span></code></pre>\n<p><strong>start</strong> function would be taking the user's input and would be sending, Hi, with the user's name.</p>\n<p><strong>help</strong> function will prompt the message of how can I help you.</p>\n<p><strong>echo_text</strong> and <strong>sticker</strong> function will send the same message or the stickers which the user has sent.</p>\n<p><strong>error</strong> function would be printing the error message on the command prompt.</p>\n<ul>\n<li>Finally, we will make the main function, which would be executed first on running the program. It will take the updates and handle the updates.</li>\n</ul>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">main</span><span class=\"mtk1\">():</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    updater = Updater(TOKEN)  </span><span class=\"mtk3\">#take the updates</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    dp = updater.dispatcher   </span><span class=\"mtk3\">#handle the updates</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    dp.add_handler(CommandHandler(</span><span class=\"mtk8\">&quot;start&quot;</span><span class=\"mtk1\">, start))</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    dp.add_handler(CommandHandler(</span><span class=\"mtk8\">&quot;help&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk11\">help</span><span class=\"mtk1\">))</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    dp.add_handler(MessageHandler(Filters.text, echo_text))   </span><span class=\"mtk3\">#if the user sends text</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    dp.add_handler(MessageHandler(Filters.sticker, sticker))  </span><span class=\"mtk3\">#if the user sends sticker</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    dp.add_error_handler(error)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    updater.start_polling()</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    logger.info(</span><span class=\"mtk8\">&quot;Started...&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    updater.idle()</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">if</span><span class=\"mtk1\"> </span><span class=\"mtk12\">__name__</span><span class=\"mtk1\">==</span><span class=\"mtk8\">&quot;__main__&quot;</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    main()</span></span></code></pre>\n<blockquote>\n<p>Save the program and execute it to check the working of the telegram bot on telegram.</p>\n</blockquote>\n<p>You can get the source code to make the Telegram bot from this Github Repository <a href=\"https://github.com/LoginRadius/engineering-blog-samples/tree/master/Telegram-Bot\">Bot :robot:</a>. You can also make some changes to the code to make the bot do cool stuff such as Getting News, Articles, or Movies recommendations.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n</style>","frontmatter":{"date":"October 20, 2020","updated_date":null,"title":"How to make a telegram bot","tags":["Telegram Bot","Python"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.4814814814814814,"src":"/static/aceaa6a49195f6d0417cb80f93f56be4/58556/bot.webp","srcSet":"/static/aceaa6a49195f6d0417cb80f93f56be4/61e93/bot.webp 200w,\n/static/aceaa6a49195f6d0417cb80f93f56be4/1f5c5/bot.webp 400w,\n/static/aceaa6a49195f6d0417cb80f93f56be4/58556/bot.webp 800w,\n/static/aceaa6a49195f6d0417cb80f93f56be4/99238/bot.webp 1200w,\n/static/aceaa6a49195f6d0417cb80f93f56be4/135cd/bot.webp 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Rajan Puri","github":"rjn01","avatar":null}}}},{"node":{"fields":{"slug":"/engineering/python-virtual-environments/"},"html":"<p>In this article, we'll talk about the basic concept of virtual environment in python -  what it is and how to use it and also how to build and manage separate environments for your Python projects using virtual environments. </p>\n<h2 id=\"what-is-a-virtual-environment\" style=\"position:relative;\"><a href=\"#what-is-a-virtual-environment\" aria-label=\"what is a virtual environment permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is a virtual environment?</h2>\n<p>A virtual environment is simply a tool that separates the dependencies of different projects by creating a separate isolated environment for each project.</p>\n<p>These are simply the directories so that unlimited virtual environments can be created. This is one of the popular tools used by most of the Python developers.</p>\n<h2 id=\"why-do-we-need-a-virtual-environment\" style=\"position:relative;\"><a href=\"#why-do-we-need-a-virtual-environment\" aria-label=\"why do we need a virtual environment permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why do we need a virtual environment?</h2>\n<p><a href=\"/python-basics-in-minutes/\">Python has various modules</a> and packages for different applications. During our project, it may require a third-party library, which we install. Another project also uses the same directory for retrieval and storage but doesn't require any other third-party packages.</p>\n<p>So, the virtual environment can come into play and make a separate isolated environment for both projects, and each project can store and retrieve packages from their specific environment.</p>\n<p>Also, let us consider another case where we are creating a web application <a href=\"https://www.djangoproject.com/start/\">using Django</a>. Suppose you are working on two projects project1 and project2. </p>\n<p>If project1 uses Django-2.2 and project2 uses Django-3.2, they would be stored in the same directory with the same name, and the error may occur. Then, in such cases, virtual environments can be really helpful for you to maintain the dependencies of both the projects.</p>\n<h2 id=\"how-does-a-virtual-environment-work\" style=\"position:relative;\"><a href=\"#how-does-a-virtual-environment-work\" aria-label=\"how does a virtual environment work permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How does a virtual environment work?</h2>\n<p>To create a virtual environment, we need a <a href=\"https://pypi.org/project/virtualenv/\">module named</a> virtualenv. It creates a folder with all the necessary executables to run Python projects. Make sure <strong>pip</strong> is installed on your computer. If not, then use the following command:</p>\n<p><img src=\"/9fa10fcc5576d9428768731619cba792/pip.webp\" alt=\"Pip\" /><br></p>\n<h3 id=\"install-virtualenv\" style=\"position:relative;\"><a href=\"#install-virtualenv\" aria-label=\"install virtualenv permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Install virtualenv:</h3>\n<p>Open the terminal and paste the following command to install a virtualenv:</p>\n<p><img src=\"/f9658abd5f1e49b3e951afcd884adb6a/install.webp\" alt=\"Install\" /><br></p>\n<h3 id=\"check-your-version-of-virtualenv\" style=\"position:relative;\"><a href=\"#check-your-version-of-virtualenv\" aria-label=\"check your version of virtualenv permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Check your version of virtualenv:</h3>\n<p>If you want to confirm whether it is installed or not, paste the command below in your terminal:</p>\n<p><img src=\"/5221153f420814832190b6a8eb7c5ec4/check.webp\" alt=\"Check\" /><br></p>\n<h3 id=\"create-a-virtual-environment\" style=\"position:relative;\"><a href=\"#create-a-virtual-environment\" aria-label=\"create a virtual environment permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Create a virtual environment:</h3>\n<p>After successful installation of virtualenv, now you can create a virtual environment with your desired name using following command:</p>\n<p><img src=\"/76cce15130697d584fb75e02df60601b/name.webp\" alt=\"Check\" /><br></p>\n<p>Here, I created <code>venv</code> as the name of my virtual environment. You can name it whatever you desire. The folder with the name <code>venv</code> is created that contains all the necessary executables to <a href=\"/speed-up-python-code/\">run the Python project</a>. This is the folder where all your python packages will run.</p>\n<p>To specify the Python interpreter of your choice, you can do it easily by specifying Python{version-name}.\nEg: To create python2.7 virtual environment, we use following command:</p>\n<p><img src=\"/a5b7c69d3dff106d6fe355ae725541dd/version.webp\" alt=\"Version\" /><br></p>\n<p>After successfully creating the virtual environment, you need to activate it to enter into that particular isolated environment. Always remember to activate the required virtual environment before working on the project. To activate it, we move to the location where the name of your desired virtual environment is located and follow the command below:</p>\n<p><img src=\"/9d3d2c7f55a4fd52999a514f73ac46ef/activate.webp\" alt=\"Activate\" /><br></p>\n<p><strong>Note:</strong> Instead of <code>venv</code>, you write the name of your virtual environment you created.<br></p>\n<h4 id=\"in-windows\" style=\"position:relative;\"><a href=\"#in-windows\" aria-label=\"in windows permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>In Windows,</h4>\n<p><img src=\"/9fb235d96fd1d14f07bfa3fffa942419/windows.webp\" alt=\"Windows\" /><br></p>\n<p>After activating the virtual environment, you can see your name on the virtual environment on the terminal's left side. It confirms the activation of your virtual environment and is currently active. </p>\n<p><img src=\"/affe1c8aa1265fe5783364e3f41de2fc/activated.webp\" alt=\"Activated\" /><br></p>\n<p>Finally, you successfully created and activated your virtual environment. Now, you can install the dependencies and requirements your project asks for without interfering with other projects.</p>\n<p>Once you completed your work, then you can return to the system default environment by just using the <strong>deactivate</strong> command as shown below:</p>\n<img src=\"/2d752ab9b41bfc22473be6e2614d02c2/deactivate.webp\" alt=\"Deactivate\" />\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n</style>","frontmatter":{"date":"October 16, 2020","updated_date":null,"title":"Python Virtual Environment: What is it and how it works?","tags":["Python"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":2.127659574468085,"src":"/static/674ce17a9dc4e13f0616a1fd935bc596/58556/python.webp","srcSet":"/static/674ce17a9dc4e13f0616a1fd935bc596/61e93/python.webp 200w,\n/static/674ce17a9dc4e13f0616a1fd935bc596/1f5c5/python.webp 400w,\n/static/674ce17a9dc4e13f0616a1fd935bc596/58556/python.webp 800w,\n/static/674ce17a9dc4e13f0616a1fd935bc596/7bd08/python.webp 1171w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Ashish Maharjan","github":"AshishMhrzn10","avatar":null}}}},{"node":{"fields":{"slug":"/engineering/speed-up-python-code/"},"html":"<h1 id=\"a-few-ways-to-speed-up-your-python-code\" style=\"position:relative;\"><a href=\"#a-few-ways-to-speed-up-your-python-code\" aria-label=\"a few ways to speed up your python code permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>A Few Ways to Speed Up Your Python Code</h1>\n<p>Python is one of the most popular languages all over the world. Nowadays it is being used in competitive programming also because of its simple syntax and rich libraries. Most of us probably started coding with python. At first, everything goes simple and easy. But while solving a hard algorithmic problem, most of us suffer from <code>Time Limit Exceeded</code>. However, it is not a problem of python; it is the programmer's problem. I am not saying that language is not slow, but if a programmer writes an efficient programme, it will get <code>Accepted</code> for sure. Here are some tips to speed up your python programme.</p>\n<h2 id=\"use-proper-data-structure\" style=\"position:relative;\"><a href=\"#use-proper-data-structure\" aria-label=\"use proper data structure permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use proper data structure</h2>\n<p>Use of proper data structure has a significant effect on runtime. Python has list, tuple, set and dictionary as the built-in data structures. However, most of the people use the list in all cases. But it is not a right choice. Use proper data structures depending on your task. Especially use a tuple instead of a list. Because iterating over tuple is easier than iterating over a list.</p>\n<h2 id=\"decrease-the-use-of-for-loop\" style=\"position:relative;\"><a href=\"#decrease-the-use-of-for-loop\" aria-label=\"decrease the use of for loop permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Decrease the use of for loop</h2>\n<p>As for loop is dynamic in python, it takes more time than while loop. So, use while loop instead of for loop.</p>\n<h2 id=\"use-list-comprehension\" style=\"position:relative;\"><a href=\"#use-list-comprehension\" aria-label=\"use list comprehension permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use list comprehension</h2>\n<p>Do not use any other technique if you can use list comprehension. For example, here is a code to list all the numbers between 1 and 1000 that is the multiplier of 3:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">L = []</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> i </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> </span><span class=\"mtk11\">range</span><span class=\"mtk1\"> (</span><span class=\"mtk7\">1</span><span class=\"mtk1\">, </span><span class=\"mtk7\">1000</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> i%</span><span class=\"mtk7\">3</span><span class=\"mtk1\"> == </span><span class=\"mtk7\">0</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        L.append (i)</span></span></code></pre>\n<p>Using list comprehension, it would be:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">L = [i </span><span class=\"mtk15\">for</span><span class=\"mtk1\"> i </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> </span><span class=\"mtk11\">range</span><span class=\"mtk1\"> (</span><span class=\"mtk7\">1</span><span class=\"mtk1\">, </span><span class=\"mtk7\">1000</span><span class=\"mtk1\">) </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> i%</span><span class=\"mtk7\">3</span><span class=\"mtk1\"> == </span><span class=\"mtk7\">0</span><span class=\"mtk1\">]</span></span></code></pre>\n<p>List comprehension works faster than using the append method.</p>\n<h2 id=\"use-multiple-assignments\" style=\"position:relative;\"><a href=\"#use-multiple-assignments\" aria-label=\"use multiple assignments permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use multiple assignments</h2>\n<p>Do not assaign variables like this:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">a = </span><span class=\"mtk7\">2</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">b = </span><span class=\"mtk7\">3</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">c = </span><span class=\"mtk7\">5</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">d = </span><span class=\"mtk7\">7</span></span></code></pre>\n<p>Instead, assign variables like this:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">a, b, c, d = </span><span class=\"mtk7\">2</span><span class=\"mtk1\">, </span><span class=\"mtk7\">3</span><span class=\"mtk1\">, </span><span class=\"mtk7\">5</span><span class=\"mtk1\">, </span><span class=\"mtk7\">7</span></span></code></pre>\n<h2 id=\"do-not-use-global-variables\" style=\"position:relative;\"><a href=\"#do-not-use-global-variables\" aria-label=\"do not use global variables permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Do not use global variables</h2>\n<p>Python has <code>global</code> keyword to declare global variables. But global variables take higher time during operation than a local variable. So, do not use global variables if it is not necessary.</p>\n<h2 id=\"use-library-function\" style=\"position:relative;\"><a href=\"#use-library-function\" aria-label=\"use library function permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use library function</h2>\n<p>Do not write your function (manually) if it is already in the library. Library functions are highly efficient, and you will probably won't be able to code with that efficiency.</p>\n<h2 id=\"concatenate-strings-with-join\" style=\"position:relative;\"><a href=\"#concatenate-strings-with-join\" aria-label=\"concatenate strings with join permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Concatenate strings with join</h2>\n<p>In python, you can concatenate strings with <code>+</code> operation.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">concatenatedString = </span><span class=\"mtk8\">&quot;Programming &quot;</span><span class=\"mtk1\"> + </span><span class=\"mtk8\">&quot;is &quot;</span><span class=\"mtk1\"> + </span><span class=\"mtk8\">&quot;fun.&quot;</span></span></code></pre>\n<p>It can also be done with <code>join()</code> method.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">concatenatedString = </span><span class=\"mtk8\">&quot; &quot;</span><span class=\"mtk1\">.join ([</span><span class=\"mtk8\">&quot;Programming&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&quot;is&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&quot;fun.&quot;</span><span class=\"mtk1\">])</span></span></code></pre>\n<p><code>join()</code> concatenates strings faster than <code>+</code> operation because <code>+</code> operators create a new string and then copies the old content at each step. But <code>join()</code> doesn't work that way.</p>\n<h2 id=\"use-generators\" style=\"position:relative;\"><a href=\"#use-generators\" aria-label=\"use generators permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use generators</h2>\n<p>If you have a large amount of data in your list and you need to use one data at a time and for once then use <code>generator</code>s. It will save you time.</p>\n<h2 id=\"it-may-seem-efficient-but-its-not\" style=\"position:relative;\"><a href=\"#it-may-seem-efficient-but-its-not\" aria-label=\"it may seem efficient but its not permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>It may seem efficient, but it's not</h2>\n<p>See the below code:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">L = []</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> element </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> </span><span class=\"mtk10\">set</span><span class=\"mtk1\">(L):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    ...</span></span></code></pre>\n<p>The above code may seem efficient because it used set to delete duplicate data. But the reality is that the code is not efficient. Do not forget that converting a list into set takes time. So this code will work better than the previous:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> element </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> L:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    ...</span></span></code></pre>\n<h2 id=\"do-not-use-dot-operation\" style=\"position:relative;\"><a href=\"#do-not-use-dot-operation\" aria-label=\"do not use dot operation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Do not use dot operation</h2>\n<p>Try to avoid dot operation. See the below programme.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> math</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">val = math.sqrt(</span><span class=\"mtk7\">60</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>Instead of the above style write code like this:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> math </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> sqrt</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">val = sqrt(</span><span class=\"mtk7\">60</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>Because when you call a function using <code>.</code> (dot) it first calls <code>__getattribute()__</code> or <code>__getattr()__</code> which then use dictionary operation which costs time. So, try using <code>from module import function</code>.</p>\n<h2 id=\"use-1-for-infinity-loops\" style=\"position:relative;\"><a href=\"#use-1-for-infinity-loops\" aria-label=\"use 1 for infinity loops permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use 1 for infinity loops</h2>\n<p>Use <code>while 1</code> instead of <code>while True</code>. It will reduce some runtime.</p>\n<h2 id=\"try-a-different-approach\" style=\"position:relative;\"><a href=\"#try-a-different-approach\" aria-label=\"try a different approach permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Try a different approach</h2>\n<p>Try new ways to write your code efficiently. See the below code.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"10\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">if</span><span class=\"mtk1\"> a_condition:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> another_condition:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        do_something</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">else</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">raise</span><span class=\"mtk1\"> exception</span></span></code></pre>\n<p>Instead of the above code you can write:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"11\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">if</span><span class=\"mtk1\"> (</span><span class=\"mtk4\">not</span><span class=\"mtk1\"> a_condition) </span><span class=\"mtk4\">or</span><span class=\"mtk1\"> (</span><span class=\"mtk4\">not</span><span class=\"mtk1\"> another_condition):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">raise</span><span class=\"mtk1\"> exception</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">do_something</span></span></code></pre>\n<h2 id=\"use-speed-up-applications\" style=\"position:relative;\"><a href=\"#use-speed-up-applications\" aria-label=\"use speed up applications permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use speed up applications</h2>\n<p>For python's slow speed, some projects have been taken to decrease runtime. Pypy and Numba two of them. In most of the programming contests, you will see pypy if it allows python. These applications will reduce the runtime of your programme.</p>\n<h2 id=\"use-special-libraries-to-process-large-datasets\" style=\"position:relative;\"><a href=\"#use-special-libraries-to-process-large-datasets\" aria-label=\"use special libraries to process large datasets permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use special libraries to process large datasets</h2>\n<p>C/C++ is faster than python. So, many packages and modules have been written in C/C++ that you can use in your python programme. <code>Numpy</code>, <code>Scipy</code> and <code>Pandas</code> are three of them and are popular for processing large datasets.</p>\n<h2 id=\"use-the-latest-release-of-python\" style=\"position:relative;\"><a href=\"#use-the-latest-release-of-python\" aria-label=\"use the latest release of python permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use the latest release of python</h2>\n<p>Python is updated and upgraded regularly, and every release is faster and more optimized. So always use the latest version of python.</p>\n<p>These were some of the tips to decrease the runtime of python code. There are a few more techniques that you can use. Use a search engine to find those and write efficient code!</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n</style>","frontmatter":{"date":"October 15, 2020","updated_date":null,"title":"Speed Up Python Code","tags":["Python","Performance"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/65558b9da57861b919205b297c81828b/58556/speed-up-python-code-1.webp","srcSet":"/static/65558b9da57861b919205b297c81828b/61e93/speed-up-python-code-1.webp 200w,\n/static/65558b9da57861b919205b297c81828b/1f5c5/speed-up-python-code-1.webp 400w,\n/static/65558b9da57861b919205b297c81828b/58556/speed-up-python-code-1.webp 800w,\n/static/65558b9da57861b919205b297c81828b/99238/speed-up-python-code-1.webp 1200w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Md. Tahmid Hossain","github":"tahmid02016","avatar":null}}}},{"node":{"fields":{"slug":"/engineering/full-data-science-pipeline-implementation/"},"html":"<h2 id=\"what-is-data-enrichment-and-its-importance\" style=\"position:relative;\"><a href=\"#what-is-data-enrichment-and-its-importance\" aria-label=\"what is data enrichment and its importance permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is data enrichment? and its importance</h2>\n<p>Data enrichment is the process of combining first-party data from internal sources with disparate data from other internal systems or third-party data from external sources.</p>\n<p>Usually, the data available from clients or stakeholders are not enough to solve the given problem statement, like if a client comes with a problem statement to build a recommendation engine for his mutual fund industry, the usual data they have is old purchase data but that's not enough as client behaviour changes with time and is impacted by the present market condition, oil prices, etc. which needs to be incorporated in the model to make it efficient.</p>\n<p>Codes for this tutorial is at <a href=\"https://github.com/LoginRadius/engineering-blog-samples/tree/master/Data_Science/Full_DataScience_Pipeline_Implementation\">Link</a></p>\n<p><strong>The whole process id divided into four steps:</strong></p>\n<p>I have implemented a full pipeline of data science from scrapping data from web to implementing ml and NLP classification.</p>\n<ul>\n<li>Phase I:</li>\n</ul>\n<p>Here I have scraped data from IMDB website (imdb.py)</p>\n<ul>\n<li>Phase II:</li>\n</ul>\n<p>I have tried to implement simple ML regression on the data (ml_imdb.py)</p>\n<ul>\n<li>Phase III:</li>\n</ul>\n<p>I have prepared the data for NLP classification (multilabel_prep.py)</p>\n<ul>\n<li>Phase IV:</li>\n</ul>\n<p>I have implemented multilabel NLP classifier using various techniques like chain classifier etc. (multilabel<em>nlp</em>classifier.ipynb)</p>\n<h2 id=\"what-is-web-scraping\" style=\"position:relative;\"><a href=\"#what-is-web-scraping\" aria-label=\"what is web scraping permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is web scraping?</h2>\n<p>Web scraping is the process of extracting and parsing raw data from the web. Web scraping is a technique which helps data scientist to make their data-rich and is an efficient technique of data collection.</p>\n<p>This world is full of data, but unfortunately, most of them are not in the form to be used. Data is like crude oil, or we say it is in unstructured form. For a data scientist or engineer, our first challenge is to make the data model consumption ready, which takes the majority of the time, and this whole process is collectively known as data preprocessing.</p>\n<p>HTML  is a form of primary markup language and the base framework of mostly all websites. For performing web scraping its necessary to know it</p>\n<p>Here we will start with requesting the web page using python package requests.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk15\">from</span><span class=\"mtk1\"> requests </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> get</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  url = </span><span class=\"mtk8\">&#39;http://www.imdb.com/search/title?release_date=2017&sort=num_votes,desc&page=1&#39;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  response = get(url)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk11\">len</span><span class=\"mtk1\">(response.text))</span></span></code></pre>\n<p>The whole web page is now stored in the variable object response.\nThen we parse the web page using beautifulsoup package.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk15\">from</span><span class=\"mtk1\"> bs4 </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> BeautifulSoup</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  html_soup = BeautifulSoup(response.text, </span><span class=\"mtk8\">&#39;html.parser&#39;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk10\">type</span><span class=\"mtk1\">(html_soup)</span></span></code></pre>\n<p>Then I will store all the div with the class named lister-item mode-advanced in variable movie_containers.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">movie_containers = html_soup.find_all(</span><span class=\"mtk8\">&#39;div&#39;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">class_</span><span class=\"mtk1\"> = </span><span class=\"mtk8\">&#39;lister-item mode-advanced&#39;</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>Then I iterate through this object and store the information in lists to make my final DataFrame, using simple for loops.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\"># Lists to store the scraped data in</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">names = []</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">years = []</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">imdb_ratings = []</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">metascores = []</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">votes = []</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">#gross=[] #many movies have no record</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">movie_description=[]</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">movie_duration=[]</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">movie_genre=[]</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># Extract data from individual movie container</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> container </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> movie_containers:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># If the movie has Metascore, then extract:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> container.find(</span><span class=\"mtk8\">&#39;div&#39;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">class_</span><span class=\"mtk1\"> = </span><span class=\"mtk8\">&#39;ratings-metascore&#39;</span><span class=\"mtk1\">) </span><span class=\"mtk4\">is</span><span class=\"mtk1\"> </span><span class=\"mtk4\">not</span><span class=\"mtk1\"> </span><span class=\"mtk4\">None</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># The name</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        name = container.h3.a.text</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        names.append(name)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># The year</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        year = container.h3.find(</span><span class=\"mtk8\">&#39;span&#39;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">class_</span><span class=\"mtk1\"> = </span><span class=\"mtk8\">&#39;lister-item-year&#39;</span><span class=\"mtk1\">).text</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        years.append(year)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># The IMDB rating</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        imdb = </span><span class=\"mtk10\">float</span><span class=\"mtk1\">(container.strong.text)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        imdb_ratings.append(imdb)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># The Metascore</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        m_score = container.find(</span><span class=\"mtk8\">&#39;span&#39;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">class_</span><span class=\"mtk1\"> = </span><span class=\"mtk8\">&#39;metascore&#39;</span><span class=\"mtk1\">).text</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        metascores.append(</span><span class=\"mtk10\">int</span><span class=\"mtk1\">(m_score))</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># The number of votes</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        vote = container.find(</span><span class=\"mtk8\">&#39;span&#39;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">attrs</span><span class=\"mtk1\"> = {</span><span class=\"mtk8\">&#39;name&#39;</span><span class=\"mtk1\">:</span><span class=\"mtk8\">&#39;nv&#39;</span><span class=\"mtk1\">})[</span><span class=\"mtk8\">&#39;data-value&#39;</span><span class=\"mtk1\">]</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        votes.append(</span><span class=\"mtk10\">int</span><span class=\"mtk1\">(vote))</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># Gross income of movie</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk3\">#gross_inc =container.find_all(&#39;span&#39;, attrs = {&#39;name&#39;:&#39;nv&#39;})[1][&#39;data-value&#39;]</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk3\">#gross.append(gross_inc)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># movie description</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        movie_desc=container.find_all(</span><span class=\"mtk8\">&#39;p&#39;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">class_</span><span class=\"mtk1\"> = </span><span class=\"mtk8\">&#39;text-muted&#39;</span><span class=\"mtk1\">)[</span><span class=\"mtk7\">1</span><span class=\"mtk1\">].text</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        movie_description.append(movie_desc)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        movie_det=container.find_all(</span><span class=\"mtk8\">&#39;p&#39;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">class_</span><span class=\"mtk1\"> = </span><span class=\"mtk8\">&#39;text-muted&#39;</span><span class=\"mtk1\">)[</span><span class=\"mtk7\">0</span><span class=\"mtk1\">]</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># Movie duration</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        movie_dur=movie_det.find(</span><span class=\"mtk8\">&#39;span&#39;</span><span class=\"mtk1\">,</span><span class=\"mtk12\">class_</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&#39;runtime&#39;</span><span class=\"mtk1\">).text</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        movie_duration.append(movie_dur)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># Movie genre</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        movie_gen=movie_det.find(</span><span class=\"mtk8\">&#39;span&#39;</span><span class=\"mtk1\">,</span><span class=\"mtk12\">class_</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&#39;genre&#39;</span><span class=\"mtk1\">).text</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        movie_genre.append(movie_gen)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> pandas </span><span class=\"mtk15\">as</span><span class=\"mtk1\"> pd</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">one_df = pd.DataFrame({</span><span class=\"mtk8\">&#39;movie&#39;</span><span class=\"mtk1\">: names,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk8\">&#39;year&#39;</span><span class=\"mtk1\">: years,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk8\">&#39;imdb&#39;</span><span class=\"mtk1\">: imdb_ratings,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk8\">&#39;metascore&#39;</span><span class=\"mtk1\">: metascores,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk8\">&#39;votes&#39;</span><span class=\"mtk1\">: votes,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">#&#39;gross&#39;:gross,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk8\">&#39;movie decription&#39;</span><span class=\"mtk1\">:movie_description,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk8\">&#39;movie duration&#39;</span><span class=\"mtk1\">:movie_duration,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk8\">&#39;movie genre&#39;</span><span class=\"mtk1\">:movie_genre</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">})</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(one_df.info())</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">one_df.to_csv(</span><span class=\"mtk8\">&#39;50_movie_details.csv&#39;</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>But this was only for one page which has data for 50 movies only which is not enough to build a model.</p>\n<p>Please refer my code to understand how I use simple for loops to iterate through all the movies and downloading data for 20 years(approx).</p>\n<h2 id=\"implementing-simple-linear-algorithms-in-numerical-data-we-just-scrapped\" style=\"position:relative;\"><a href=\"#implementing-simple-linear-algorithms-in-numerical-data-we-just-scrapped\" aria-label=\"implementing simple linear algorithms in numerical data we just scrapped permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Implementing simple linear algorithms in numerical data we just scrapped</h2>\n<p>Whats is linear regression??</p>\n<p>It is one of the most popular and used statistical techniques\n• Used to understand the relationship between variables</p>\n<p>  • Can also be used to predict a value of interest for new observations</p>\n<p>  • The aim is to predict the value of a continuous numeric variable of interest (known as the response or dependent or target variable)</p>\n<p>  • The values of one or more predictor (or independent) variables are used to make the prediction</p>\n<p>  • One predictor = simple regression</p>\n<p>  • More predictors = multiple regression</p>\n<p>Here I just tried to use metascore of movies firstly to predict IMDB ratings and secondly I wanted to enhance it by using metascore and votes to predict IMDB rating. </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\">## ML model</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">X = data.loc[:, </span><span class=\"mtk8\">&#39;metascore&#39;</span><span class=\"mtk1\">].values</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">y = data.loc[:, </span><span class=\"mtk8\">&#39;imdb&#39;</span><span class=\"mtk1\">].values</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># Splitting the dataset into the Training set and Test set</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> sklearn.cross_validation </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> train_test_split</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">X_train, X_test, y_train, y_test = train_test_split(X, y, </span><span class=\"mtk12\">test_size</span><span class=\"mtk1\"> = </span><span class=\"mtk7\">0.33</span><span class=\"mtk1\">, </span><span class=\"mtk12\">random_state</span><span class=\"mtk1\"> = </span><span class=\"mtk7\">0</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> sklearn.linear_model </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> LinearRegression</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">regressor = LinearRegression()</span><span class=\"mtk3\">#making object for reg package</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">regressor.fit(X_train.reshape(-</span><span class=\"mtk7\">1</span><span class=\"mtk1\">,</span><span class=\"mtk7\">1</span><span class=\"mtk1\">), y_train.reshape(-</span><span class=\"mtk7\">1</span><span class=\"mtk1\">,</span><span class=\"mtk7\">1</span><span class=\"mtk1\">))</span><span class=\"mtk3\">#to fit the regressor to our training data</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">#predict the test results</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">y_pred =regressor.predict(X_test.reshape(-</span><span class=\"mtk7\">1</span><span class=\"mtk1\">,</span><span class=\"mtk7\">1</span><span class=\"mtk1\">))</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> sklearn.metrics </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> mean_squared_error</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">mean_squared_error(y_test, y_pred)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># 0.18041462828221905</span></span></code></pre>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\">## Let try with imdb and votes</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">X1 = data.loc[:, [</span><span class=\"mtk8\">&#39;metascore&#39;</span><span class=\"mtk1\">,</span><span class=\"mtk8\">&#39;votes&#39;</span><span class=\"mtk1\">]].values</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">y1 = data.loc[:, </span><span class=\"mtk8\">&#39;imdb&#39;</span><span class=\"mtk1\">].values</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># Splitting the dataset into the Training set and Test set</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> sklearn.cross_validation </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> train_test_split</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">X_train, X_test, y_train, y_test = train_test_split(X1, y1, </span><span class=\"mtk12\">test_size</span><span class=\"mtk1\"> = </span><span class=\"mtk7\">0.33</span><span class=\"mtk1\">, </span><span class=\"mtk12\">random_state</span><span class=\"mtk1\"> = </span><span class=\"mtk7\">0</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> sklearn.linear_model </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> LinearRegression</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">regressor = LinearRegression()</span><span class=\"mtk3\">#making object for reg package</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">regressor.fit(X_train, y_train)</span><span class=\"mtk3\">#to fit the regressor to our training data</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">#predict the test results</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">y_pred =regressor.predict(X_test)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> sklearn.metrics </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> mean_squared_error</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">mean_squared_error(y_test, y_pred)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># 0.15729132122310804 good score</span></span></code></pre>\n<p>I tried to scrape data from the IMDB site and then applied ML regression techniques on it. Later I found that the movies listed are multi-class like Logan belongs to Action, Drama, Sci-Fi, which led me to think about how to implement the classifier model in the multilabel data. Usually, the data we get in real-world is mostly multi labelled like chatbot data; the intent is many and like these movies which are multi-class.</p>\n<p>Here we will first see how we prep our data for multilabel classification.</p>\n<p>Here we have all tags in one single column which is not usable while we do classification, so we have to make separate columns for all labels, and if the row doesn't belong to that category, it will be filled by 0 else 1.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> os</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">os.chdir(</span><span class=\"mtk8\">&#39;Desktop/web_scraping/imdb scrapper_ml/&#39;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> pandas </span><span class=\"mtk15\">as</span><span class=\"mtk1\"> pd</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">data=pd.read_csv(</span><span class=\"mtk8\">&#39;multilabel_nlp_classification.csv&#39;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">movie_list=[x </span><span class=\"mtk15\">for</span><span class=\"mtk1\"> x </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> data[</span><span class=\"mtk8\">&#39;movie genre&#39;</span><span class=\"mtk1\">]]</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">movie_list1=</span><span class=\"mtk8\">&#39;&#39;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> x </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> data[</span><span class=\"mtk8\">&#39;movie genre&#39;</span><span class=\"mtk1\">]:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    movie_list1+=</span><span class=\"mtk8\">&#39;,&#39;</span><span class=\"mtk1\">+x</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">li_m=movie_list1.split(</span><span class=\"mtk8\">&#39;,&#39;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">li=[x.strip() </span><span class=\"mtk15\">for</span><span class=\"mtk1\"> x </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> li_m]</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">list_s=</span><span class=\"mtk10\">list</span><span class=\"mtk1\">(</span><span class=\"mtk10\">set</span><span class=\"mtk1\">(li))</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> x </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> list_s:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    data[x]=</span><span class=\"mtk7\">0</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">data[</span><span class=\"mtk8\">&#39;movie_genre&#39;</span><span class=\"mtk1\">]=[x.strip().split(</span><span class=\"mtk8\">&#39;,&#39;</span><span class=\"mtk1\">) </span><span class=\"mtk15\">for</span><span class=\"mtk1\"> x </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> data[</span><span class=\"mtk8\">&#39;movie genre&#39;</span><span class=\"mtk1\">]]</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">de=data.copy()</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">#data.loc[0,&#39;Action&#39;]=1</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">de[</span><span class=\"mtk8\">&#39;id&#39;</span><span class=\"mtk1\">]=</span><span class=\"mtk11\">range</span><span class=\"mtk1\">(</span><span class=\"mtk7\">0</span><span class=\"mtk1\">,</span><span class=\"mtk7\">6116</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">#print(de.loc[de[&#39;id&#39;]==0,&#39;Action&#39;])</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> i </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> </span><span class=\"mtk11\">range</span><span class=\"mtk1\">(</span><span class=\"mtk7\">0</span><span class=\"mtk1\">,</span><span class=\"mtk7\">6116</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">for</span><span class=\"mtk1\"> x </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> de.loc[de[</span><span class=\"mtk8\">&#39;id&#39;</span><span class=\"mtk1\">]==i,</span><span class=\"mtk8\">&#39;movie_genre&#39;</span><span class=\"mtk1\">]:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk15\">for</span><span class=\"mtk1\"> y </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> x:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            y=y.strip()</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            de.loc[de[</span><span class=\"mtk8\">&#39;id&#39;</span><span class=\"mtk1\">]==i,y]=</span><span class=\"mtk7\">1</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">de.to_csv(</span><span class=\"mtk8\">&#39;multilabel_nlp_classification.csv&#39;</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>Now, as our data is ready, we can start with NLP implementation.</p>\n<p>For multilabel classification, I used techniques like classifier chain, label powerset, etc.</p>\n<p>Here the problem statement is that using the movie description our model has to guess which genre the movie belongs to. It is a popular use case. Take an example of ecommerce product description data; now instead of manually assigning the labels to it, we can use a model which will find relevant labels or genre for it and make the content relevant to the type it belongs.</p>\n<p>I start with Exploratory data analysis and then data cleaning, which is the most crucial step as if all the description has some very 30-50 common words it will simply make the data-heavy and model slow and inefficient.</p>\n<p>Then we go on to make the data model ready as ML models don't understand text data we have to feed numbers in it. For that purpose, we use TfidfVectorizer.</p>\n<h3 id=\"what-is-tfidfvectorizer\" style=\"position:relative;\"><a href=\"#what-is-tfidfvectorizer\" aria-label=\"what is tfidfvectorizer permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is TfidfVectorizer?</h3>\n<p>TfidfVectorizer - Transforms text to feature vectors that can be used as input to the estimator.</p>\n<p>Then simply diving the data in train and test split. </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">x_train = vectorizer.transform(train_text)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">y_train = train.drop(</span><span class=\"mtk12\">labels</span><span class=\"mtk1\"> = [</span><span class=\"mtk8\">&#39;id&#39;</span><span class=\"mtk1\">,</span><span class=\"mtk8\">&#39;movie decription&#39;</span><span class=\"mtk1\">], </span><span class=\"mtk12\">axis</span><span class=\"mtk1\">=</span><span class=\"mtk7\">1</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">x_test = vectorizer.transform(test_text)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">y_test = test.drop(</span><span class=\"mtk12\">labels</span><span class=\"mtk1\"> = [</span><span class=\"mtk8\">&#39;id&#39;</span><span class=\"mtk1\">,</span><span class=\"mtk8\">&#39;movie decription&#39;</span><span class=\"mtk1\">], </span><span class=\"mtk12\">axis</span><span class=\"mtk1\">=</span><span class=\"mtk7\">1</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>I tried first with applying logistic regression and one vs rest classifier.</p>\n<h3 id=\"what-is-onevsrestclassifier\" style=\"position:relative;\"><a href=\"#what-is-onevsrestclassifier\" aria-label=\"what is onevsrestclassifier permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is OneVsRestClassifier??</h3>\n<p>OneVsRestClassifier strategy splits a multi-class classification into one binary classification problem per class.\nOneVsRestClassifier is when we want to do multi-class or multilabel classification, and its strategy consists of fitting one classifier per class. For each classifier, the class is fitted against all the other classes. </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\"># Using pipeline for applying logistic regression and one vs rest classifier</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">LogReg_pipeline = Pipeline([</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                (</span><span class=\"mtk8\">&#39;clf&#39;</span><span class=\"mtk1\">, OneVsRestClassifier(LogisticRegression(</span><span class=\"mtk12\">solver</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&#39;sag&#39;</span><span class=\"mtk1\">), </span><span class=\"mtk12\">n_jobs</span><span class=\"mtk1\">=-</span><span class=\"mtk7\">1</span><span class=\"mtk1\">)),</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            ])</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> category </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> categories:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    printmd(</span><span class=\"mtk8\">&#39;**Processing </span><span class=\"mtk4\">{}</span><span class=\"mtk8\"> comments...**&#39;</span><span class=\"mtk1\">.format(category))</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk3\"># Training logistic regression model on train data</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    LogReg_pipeline.fit(x_train, train[category])</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk3\"># calculating test accuracy</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    prediction = LogReg_pipeline.predict(x_test)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&#39;Test accuracy is </span><span class=\"mtk4\">{}</span><span class=\"mtk8\">&#39;</span><span class=\"mtk1\">.format(accuracy_score(test[category], prediction)))</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;</span><span class=\"mtk6\">\\n</span><span class=\"mtk8\">&quot;</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>Next, I tried with BinaryRelevance</p>\n<h3 id=\"what-is-binaryrelevance\" style=\"position:relative;\"><a href=\"#what-is-binaryrelevance\" aria-label=\"what is binaryrelevance permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is BinaryRelevance?</h3>\n<p>It is a simple technique which treats each label as a separate single class classification problem.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\"># using binary relevance</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> skmultilearn.problem_transform </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> BinaryRelevance</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> sklearn.naive_bayes </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> GaussianNB</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># initialize binary relevance multi-label classifier</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># with a gaussian naive bayes base classifier</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">classifier = BinaryRelevance(GaussianNB())</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># train</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">classifier.fit(x_train, y_train)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># predict</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">predictions = classifier.predict(x_test)</span></span></code></pre>\n<p>Next, I tried using ClassifierChain.</p>\n<h3 id=\"what-is-classifierchain\" style=\"position:relative;\"><a href=\"#what-is-classifierchain\" aria-label=\"what is classifierchain permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is ClassifierChain?</h3>\n<p>It is almost similar to BinaryRelevance, here the first classifier is trained just on the input data, and then each next classifier is trained on the input space and all the previous classifiers in the chain.  </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"10\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> skmultilearn.problem_transform </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> ClassifierChain</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> sklearn.linear_model </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> LogisticRegression</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># initialize classifier chains multi-label classifier</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">classifier = ClassifierChain(LogisticRegression())</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># Training logistic regression model on train data</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">classifier.fit(x_train, y_train)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># predict</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">predictions = classifier.predict(x_test)</span></span></code></pre>\n<p>Next, I tried using Label Powerset.</p>\n<h3 id=\"what-is-labelpowerset\" style=\"position:relative;\"><a href=\"#what-is-labelpowerset\" aria-label=\"what is labelpowerset permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is LabelPowerset?</h3>\n<p>Here we transform the problem into a multi-class problem with one multi-class classifier is trained on all unique label combinations found in the training data.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"11\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> skmultilearn.problem_transform </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> LabelPowerset</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># initialize label powerset multi-label classifier</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">classifier = LabelPowerset(LogisticRegression())</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># train</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">classifier.fit(x_train, y_train)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># predict</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">predictions = classifier.predict(x_test)</span></span></code></pre>\n<p>Please refer my notebook multilabel<em>nlp</em>classifier.ipynb from my repo for more details.</p>\n<h2 id=\"improvement\" style=\"position:relative;\"><a href=\"#improvement\" aria-label=\"improvement permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Improvement:</h2>\n<ol>\n<li>More feature engineering and data to avoid this overfitting and make more efficient pipeline</li>\n<li>If we collect more data, deep learning and state of the art algorithms like BERT can help us to leverage the efficiency of the model.</li>\n</ol>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary:</h2>\n<ul>\n<li>We have learnt how to collect data by web scraping and tools to perform the same.</li>\n<li>We completed the modelling techniques on in numerical data</li>\n<li>We prepared the label data to be model fed ready</li>\n<li>We learnt how different ML techniques could be applied to text data and build a multilabel classifier.</li>\n</ul>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk6 { color: #D7BA7D; }\n</style>","frontmatter":{"date":"October 09, 2020","updated_date":null,"title":" Full data science pipeline implementation","tags":["DataScience","Python","Web scraping","NLP","Machine learning"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/5a2492e69b5cbb1c2f5bb0a3d4ca9fc2/58556/ds.webp","srcSet":"/static/5a2492e69b5cbb1c2f5bb0a3d4ca9fc2/61e93/ds.webp 200w,\n/static/5a2492e69b5cbb1c2f5bb0a3d4ca9fc2/1f5c5/ds.webp 400w,\n/static/5a2492e69b5cbb1c2f5bb0a3d4ca9fc2/58556/ds.webp 800w,\n/static/5a2492e69b5cbb1c2f5bb0a3d4ca9fc2/df6a9/ds.webp 1125w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Rinki Nag","github":"eaglewarrior","avatar":null}}}},{"node":{"fields":{"slug":"/engineering/write-a-highly-efficient-python-web-crawler/"},"html":"<p>As my previous blog, I use the python web Crawler library to help crawl the static website. For the Scrapy, there can be customize download middle ware, which can deal with static content in the website like JavaScript.</p>\n<p>However, the Scrapy already helps us with much of the underlying implementation, for example, it uses it own dispatcher and it has pipeline for dealing the parsing word after download.  One drawback for using such library is hard to deal with some strange bugs occurring because they run the paralleled jobs.</p>\n<p>For this tutorial, I want to show the structure of a simple and efficient web crawler.</p>\n<p>First of all, we need a scheduler, who can paralleled the job. Because the most of the time is on the requesting.  I use the  <a href=\"http://www.gevent.org/\">gevent</a> to schedule the jobs. Gevent uses the <a href=\"http://libevent.org/\">libevent</a> as its underlying library, which combines the multithreading and event-based techniques to parallel the job.</p>\n<p>There is the sample code:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> gevent</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> gevent </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> Greenlet</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> gevent </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> monkey</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> selenium </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> webdriver</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">monkey.patch_socket()</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">class</span><span class=\"mtk1\"> </span><span class=\"mtk10\">WebCrawler</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">__init__</span><span class=\"mtk1\">(</span><span class=\"mtk12\">self</span><span class=\"mtk1\">,</span><span class=\"mtk12\">urls</span><span class=\"mtk1\">=[],</span><span class=\"mtk12\">num_worker</span><span class=\"mtk1\"> = </span><span class=\"mtk7\">1</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">self</span><span class=\"mtk1\">.url_queue = Queue()</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">self</span><span class=\"mtk1\">.num_worker = num_worker</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">worker</span><span class=\"mtk1\">(</span><span class=\"mtk12\">self</span><span class=\"mtk1\">,</span><span class=\"mtk12\">pid</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        driver = </span><span class=\"mtk4\">self</span><span class=\"mtk1\">.initializeAnImegaDisabledDriver()  </span><span class=\"mtk3\">#initilize the webdirver</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">#</span><span class=\"mtk4\">TODO</span><span class=\"mtk3\"> catch the exception</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk15\">while</span><span class=\"mtk1\"> </span><span class=\"mtk4\">not</span><span class=\"mtk1\"> </span><span class=\"mtk4\">self</span><span class=\"mtk1\">.url_queue.empty():</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            url = </span><span class=\"mtk4\">self</span><span class=\"mtk1\">.url_queue.get()</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk4\">self</span><span class=\"mtk1\">.driver.get(url)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            elem = </span><span class=\"mtk4\">self</span><span class=\"mtk1\">.driver.find_elements_by_xpath(</span><span class=\"mtk8\">&quot;//script | //iframe | //img&quot;</span><span class=\"mtk1\">) </span><span class=\"mtk3\"># get such element from webpage</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">run</span><span class=\"mtk1\">(</span><span class=\"mtk12\">self</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        jobs = [gevent.spawn(</span><span class=\"mtk4\">self</span><span class=\"mtk1\">.worker,i) </span><span class=\"mtk15\">for</span><span class=\"mtk1\"> i </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> </span><span class=\"mtk12\">xrange</span><span class=\"mtk1\">(</span><span class=\"mtk4\">self</span><span class=\"mtk1\">.num_worker)]</span></span></code></pre>\n<p>The next part is the headless browser part. I use the phantomjs with <code>--webdriver=4444 --disk-cache=true --ignore-ssl-errors=true --load-images=false --max-disk-cache-size=100000</code>. You can get the detailed option from their documents.</p>\n<p>Phantomjs uses selenium webdriver as front-end to handle the request. However phantomjs is using the webkit and QT as its underlying browser and controller. It has memory leak bugs therefore the phantomjs will consume ton of memory and it only can use one core of your CPU but you can deploy many instances of the phantomjs on different ports. I wrote a daemon process to monitor the memory and its situation but later I realize I can use Perl script to get the status of process and when it exceeds the limits like 1G memory and send kill signal to the process.</p>\n<p>To speed up the crawler, I choose to use static browser to verify the website first because the website is bad written, there might be deadlock occurring so just skip them.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n</style>","frontmatter":{"date":"July 14, 2015","updated_date":null,"title":"Write a highly efficient python Web Crawler","tags":["Python","Coding"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1,"src":"/static/c068d4f41cc4f2a33e3bd2737ca105df/7fbdd/python-web-crawler.webp","srcSet":"/static/c068d4f41cc4f2a33e3bd2737ca105df/61e93/python-web-crawler.webp 200w,\n/static/c068d4f41cc4f2a33e3bd2737ca105df/1f5c5/python-web-crawler.webp 400w,\n/static/c068d4f41cc4f2a33e3bd2737ca105df/7fbdd/python-web-crawler.webp 610w","sizes":"(max-width: 610px) 100vw, 610px"}}},"author":{"id":"Mark Duan","github":null,"avatar":null}}}}]}},"pageContext":{"tag":"Python"}},"staticQueryHashes":["1171199041","1384082988","2100481360","23180105","528864852"]}