Skip to main content

Posts

How to support both JSON or Form Data in Flask Routes ?

I have been recently working on a flask web API project. One of my API routes' is expecting data to be posted from a form, or sent via JSON payload (Swagger UI).  In this blog, we'll explore a smart approach to handling both JSON and form data seamlessly within your Flask routes. The challenge was in creating routes that can gracefully handle both scenarios without cumbersome if-else blocks. data = request.json or request.form.to_dict() This single line of code drastically simplifies the route's logic. Let's break it down: request.json attempts to parse the incoming data as JSON. If the data isn't in JSON format, it returns None. request.form.to_dict() takes care of parsing form data into a dictionary. This method returns an empty dictionary if no form data is present. By using the or operator, we create a neat and efficient way to handle both JSON and form payload scenarios.  from flask import Flask, request app = Flask(__name__) @app.route('/process_data&#

How to plot Pandas Dataframe as a Table in Python using Plotly?

Sometime its necessary to plot the pandas Dataframe as a table in python/Jupyter notebook, for instance for an easy reference to the Data dictionary. This is made easy by using plotly. Step 1: Install Plolty  pip install plotly Step 2: Import Plotly import plotly.graph_objects as go Step 3:  Plot the table  # read the data dictonary data_dict = pd.read_csv("data_dictionary.csv") fig = go.Figure(data=[go.Table(     header=dict(values=list(data_dict.columns),                 fill_color='paleturquoise',                 align='left'),     cells=dict(values=data_dict.transpose().values.tolist(),                fill_color='lavender',                align='left')) ]) fig.show()

How to Integrate Anaconda Prompt and Jupyter Notebook with CMDER

I personally find CMDER a fantastic tool and do believe its the best windows console emulator out there. I prefer make use of its  capability  to run multiple commands side by side with split panes and tabs, which helps multitasking and productivity. For those of you who aren't aware,  CMDER  is a user-friendly console emulator for Windows that offers numerous features, including a sleek and intuitive GUI, making it easy for users, including beginners, to interact with the command line.   In this blog, I'll guide you through the steps to set up CMDER to work similar to Anaconda Prompt, a popular environment for Python development.  A Step-by-Step Guide to Anaconda Prompt Integration in CMDER     Step 1: Navigate to Anaconda Prompt Before we begin, ensure you have Anaconda installed on your system. Open the Anaconda Prompt to find the location of the 'conda' executable. Step 2: Locate the 'conda' Executable In the Anaconda Prompt, enter the following command: whe

How to Add/Edit environment variables on Windows.

 1. Search and Open Environment variables 2. On the "System Properties" panel click Environment Variables 3. Double click on the required variable to edit/add new values.

SharePoint 2013 CSOM Error : Custom Login Page (Forms Based / Mixed mode authentication)

If you have a SharePoint On Premise installation with a Custom Login Page , you might encounter issues working with CSOM. The error would be primarily related to Authentication Failure. The following solution worked for me. Step 1 : The Custom Login Page should be placed in IIS Virtual Directory. As a general practise the custom login page is normally placed in the 15 Hive Layouts folder. Move/Copy the ASPX file of your custom login page from the 15 hive layouts folder to  the following folder in IIS Virtual directory of the concerned Web Application: “C:inetpubwwwrootwssVirtualDirectories<yoursite>_forms” Step 2 : Edit the Custom Login page file to make sure the master page url is correct. After the movement of the ASPX file, open the file in a notepad and check for the  MasterPageFile  value and change it to the following  =”~/ _layouts / 15 / simple . master “. Step 3 : Change the Custom Login Page Url in Central Administration. Navigate to Central Admin and change the Url def

Simultaneously start multiple Sharepoint 2007 workflows using C# for a List

 Its known that we can start SharePoint 2007 workflows  programmatically .  Here is the code that loops through all items in a list and starts the workflow for each item: foreach(SPListItem item in list.Items) { SPListItem wrkItem =list.GetItemById(item.ID); wrkflowmgr.StartWorkflow(wrkItem,wflassociation, wflassociation.AssociationData); } However the SharePoint Paradox here is that you can start only one workflow at a time , and have to wait for it to take its sweet time to finish, before you start the workflow for next item in list.  So what do you do if you have ( like i had ) a requirement to start a workflow on multiple items in a list simultaneously ?  Obviously you post a question shouting for help in  stackoverflow . I was told: there is no  simultaneous  method to start workflows for multiple list items at the same time. But  i eventually figured out, that  Multi Threading is the solution. Steps Create a Class “startWorkflow” that starts the workflow for a l

FTP C# Error : “The remote server returned an error: (530) Not logged in ."

  Recently working on a FTP solution using C# , i encountered an error  “ The  remote server returned an error: (530) Not logged in.” The code i used was following: FtpWebRequest request = (FtpWebRequest)WebRequest.Create( ftp://xxxxxx/file.txt ); request.Method = WebRequestMethods.Ftp.UploadFile request.Credentials = new NetworkCredential(usernameVariable, passwordVariable); What was more bewildering was if i modified the code to following, the solution was working fine. But this for obvious reasons is not an option as the username cannot be hardcoded //works but implausible to use in realtime solutions request.Credentials = new NetworkCredential("dmn/#gsgs", password);  Some googling revealed that special charcters create issues in the NetworkCredential Object. Hence some playing around worked for me, and it works irrespective of wether i do a FTPWebRequest or WebRequest. Solution: Instantiate NetworkCredential object with three paramters (username, password, domain) and m