Trying to share some code ideas

logo

Category: Python Page 1 of 2

Streamlit Table Editor App

CRUD (Create, Read, Update, and Delete) it is the cornerstone of working with database applications. Today I will walk through using the Streamlit python framework to build a web based app that you can use to interact with a database. The app will allow you to Read, Update, and Delete tables in your database.

The creators of Streamlit framework have taken care of a large amount of the burden of creating a web based app. However not all of the widgets are feature complete. Streamlit is an asynchronous framework allowing 100s or 1000s of users to use your app. With CRUD operations that is great for the Create and Read, but for Update and Delete, not so much.

Today’s tutorial will show you how to overcome those limitations and hopefully inspire you on how you could use Streamlit for your own apps.

All of the code is available on my Github page, please use responsibly.

NOTE: The code presented is not production ready; i.e. no Exception blocks but it wouldn’t take much more effort to adapt the code to be production ready. Instead we will concentrate on the important parts and get you downloading files quickly. Let’s start the tutorial below.


Pages

  1. Main Page
  2. Files
  3. pages
  4. Streamlit Handler Class
  5. Data Handler Class
  6. PandasSqlHandler
  7. Configuration Files
  8. Query
    1. info.columns
    2. sys.row_count
    3. Table Query – Example
  9. Github Files

Connect to sFTP

sFTP is still used on a daily basis. Many times the simpler well proven technologies just do the job, just like a hammer. Today’s post will show how to use Python to connect to an sFTP site and securely upload or download your files.


Many times on stackoverflow you tend to get incomplete or partial samples of code that don’t explain step by step what you need to do. And with sFTP every step is critical. The code presented is not production ready; i.e. no Exception blocks but it wouldn’t take much more effort to adapt the code to be production ready. Instead we will concentrate on the important parts and get you downloading files quickly. Let’s go!



Directory Structure

The directory structure will look like the following:


main.py

This is the file we will invoke to start the program. It is rather simple.

The file is using the sys.argv[1:] to allow you to pass in arguments when you are running the program. In an effort to keep focus, we won’t being using arguments, and will just hard code the file names in the main function.

Now let’s go line by line.

This is the FileUpload class we will create later.

The main function, pretty simple isn’t it?

file_list is a list of files we will upload to the sFTP server. Conversely you could have a list of files you wish to download.

Next we check if the file_list has any values in it; in case the list is being passed in via sys.argv.

Next we assign fp to the FileUpload class

Last step is we send the files, by calling the send_files method in the class.

Configuration File

In the configuration directory we have three files.

  1. __init__.py
  2. known_hosts
  3. sftp_config.yaml

We are going to look at the sftp_config.yaml configuration file and the __init__.py file first. We will come back to the known_hosts file later on.


sftp_config.yaml

The configuration file will store the details on how to connect to the sFTP server and what files we want to upload.

The configuration file is in plain text, the values are pretty self-explanatory. We will talk about the KEY_FILENAME and KNOWN_HOSTS later on.

Make sure to specify your details, including HOSTNAME, USERNAME, LOCAL_DIRECTORY, REMOTE_DIRECTORY, etc…


config

The init file will be used to load the .yaml configuration file. It is handy way of being able to store the sFTP configuration parameters without having to change the main parts of the code.

We need three libraries the standard pathlib and sys, Additionally I use python-box to help referencing values in the configuration. Python-box has a dependency on yaml parser. I use ruamel, finding it easier and quicker.

If not already installed, from your command line you can use pip to install them.


File: __init__.py

The function load_config will take a file name as an argument and return the python-box object.

Next we get the path of the file and store it in the path variable.

Now we will open the file, and convert the .yaml file into a Box object to be used by the FileUpload class later on.

And last step, we call the load_config function.


File: file_upload.py

The FileUpload class will be used to connect to the sFTP server and than upload files. Additional methods could be added to download files, I will leave that exercise to you.

I am going to use the paramiko package. Again if not installed, from the command line use pip to install.

Here we will import in standard libraries and the sftp_config file we created earlier.

Here we are defining the class, and setting some variables to hold, the ssh client, sftp client, and known_hosts file_path.

__set_file_paths

Now we will read the contents of the known_hosts file. If the file doesn’t exist, we will raise an Exception.

__connect_client

When we connect to the ssh server, we will need a key. The user private key should be stored locally on your server. The key needs to be in the .pem file format. The private key should be created on the ssh_server or ask the ssh server’s admin to send one to you.

Then copy the .pem file to your local machine via FileZilla or another sftp tool.

We use paramiko function to read the key from the file.

Next we connect to the client and store the client object in self._ssh_client.

Know we need to load a list of known hosts to avoid a connection error.

Lastly we connect to the ssh server.


known_hosts file

One of the more difficult issues I saw with using the paramiko package was many folks were struggling with the error generated when there was not a known hosts file. Often the “solution” proposed by others was to use following line of code.

Using the option will leave you open to man-in-the-middle attacks. The proper way is to use a known_hosts file.

The file is a simple text file, with no file extension. You can have one if you want but… why?

Your known_hosts file should look like the following.

With the “….” replaced with many more characters. You should get the known hosts from the ssh server admin. However you can generate it if you are positive it is secure.


send_files

Here is the method for sending the files.

First call the private method to connect to the ssh_client.

Next we set the sftp client object.

Next we change the directory to the remote directory we set in the configuration file earlier.

Now we have a simple loop, that will read the name of each file in the file_list, and try to upload (PUT) the file to the sFTP server.

An exception will be raised if the local file path is invalid.

And at the end we close the sftp_client and ssh_client.


That’s it. Connecting to an sFTP server.

Address Verification API

Where do you live? May be a simple question, but when companies are asking where someone lives, the answer isn’t always so easy. The physical address and the mailing address of a person can be two completely separate things. And when companies want to send direct mail, they need to ensure the message is appropriate.

Today I am going to show a Python program that I wrote that can process millions of addresses and verify if the address is a valid mailing address that matches the physical address. And how the majority of online mapping services get it wrong.

Lyrical Success

I’m not gonna write you a love songSara Bareilles

Post 1 of 4

A question I’ve always wondered is their a magic formula for creating a number one song. Many song writers are prolific however their song doesn’t necessarily have commercial success. It may be difficult to quantify how the musical composition relates to the song’s success. However I am going to make an attempt at evaluating the lyrics of songs to determining if we can accurately predict if a song will be commercially successful.

Lyrical Success – Getting the Data

To obtain the data I am going to use Beautiful Soup and a few other packages to scrap the content from the websites.

Part 2 of 4

Steps for Getting the Data

  1. Get the Songs Made by the Artists
  2. Extract the List of Songs by the Artist
  3. Scrap the Lyrics for Each Song by the Artist
  4. Extract the Lyrics from Each File
  5. Scrap Rankings by Artists
  6. Parse the Song Rankings Files

The first step is getting the list of the songs by the artist. I am using the website http://www.azlyrics.com to obtain the list of songs and the lyrics for the songs.

Computing the Minimum Number of Flight Segments

Computing the Minimum Number of Flight Segments

If we want to compute the minimum number of flight segments between a starting city and target city, we can construct an undirected graph.  In the graph the nodes represent cities and the edges represent the flight segments.  We can count the number of segments to determine the shortest distance.

The following can be applied to any situation in finding the shortest path.  It is an implementation of the breadth first search algorithm.

See code below.

Testing for Unconnected Components in a Graph

Testing for Unconnected Components in an Undirected Graph

With a graph structure it is possible that parts of the graph will not be connected to each other.  An example of this would be with social networks, not all users are friends with other users.

The code will find the total number of connected components of the graph, or graph parts in an undirected graph.

See code below.

Finding an Exit from a Maze

Finding an Exit from a Maze using undirected graphs.

We can think of a maze as a rectangular grid of cells with paths between adjacent cells. If we want to find if there is a path from a given cell to a given exit from the maze, where the exit is represented by a cell, you can represent the maze as an undirected graph.

The nodes of the graph are cells of the maze, and two nodes are connected an undirected edge if they are adjacent and there is no wall between them. Therefore we can surmise we just need to see if a path, series of edges connecting the nodes, to determine if the two nodes are connected.

See code below.

Spark – Streaming Data

Spark – Streaming Data, Capturing and Querying

Today we will look at how to capture streaming data and perform some simple queries as the data is streamed. We will use the regular expressions library and the PySpak library.  The streaming data comes from a weather station that transmits different weather at different intervals.  We will need to find the correct data out of the stream and output the results.

Let’s get started.

Spark – SQLContext

Spark – SQLContext

Today we will look at the SQLContext object from the PySpark library and how you can use it to connect to a local database.  In the example below we will:
Connect to a local PostgreSQL database and read the contents into a dataframe.
Run some simple SQL queries
And join two data frames together

Let’s get started.

Page 1 of 2

Powered by WordPress & Theme by Anders Norén