When a file is placed in the shared folder of Raspberry Pi, the process is executed.

Performs processing when files are placed in the shared folder of Raspberry Pi

  1. Create a shared folder on Raspberry Pi
  2. Monitor the status of shared folders
  3. If the folder is updated, execute the process In this example ・ When an Excel file (xls, xlsx) is placed in the shared folder of Raspberry Pi Draw a graph with matplotlib and output EPS -Furthermore, output the file converted from eps to emf format.

The sample of the process to be executed is the program that I wrote down so far. (1) Graph Excel data with matplotlib (1) (2) [Convert matplotlib graph to emf file format] (http://qiita.com/gitytm15/items/418ddcd8f58555d7433b) (3) I tried changing Inkscape of Raspberry Pi from 0.48 to 0.92

It's a Raspberry Pi, so I like that it doesn't bother me so much even if I start it all the time.

1. Create a shared folder on Raspberry Pi

In order to run the program when you put xlsx and xls files in the shared [DATA] folder, first create a shared folder that can be accessed from anywhere.

[share] ← shared folder  │ └ [DATA] Put xlsx, xls here so that the conversion program will be executed   │ ├ [eps] eps file save └ [emf] Save emf file

There are many places to refer to in the search, but I referred to here for the sharing method by samba of Raspberry Pi.

[hiramine.com] (http://www.hiramine.com/physicalcomputing/raspberrypi3/samba_installconfigconnect.html)

Here, `/ home / pi``` is shared, but since it is a problem if the area below pi is completely visible, I decided to create a ``` [share] folder under it. At first, I didn't match the folder names of `` [share] and `` `path = / home / share, and sharing didn't work, so I was worried for a while. The rough flow is as follows.

Installation

sudo apt-get update
sudo apt-get install samba

Rewriting the configuration file (added to the last line)

sudo nano /etc/samba/smb.conf

smb.conf


#Last added
[share]
path = /home/pi/share
read only = No
guest ok = Yes
force user = pi
sudo service smbd restart

You can now access it from Windows, Mac and Ubuntu.

2. Monitor the status of shared folders

It's concrete code on how to monitor and what it actually does, but there's already an easy-to-understand article. I used it as a reference. The one I used is `` `watchdog```

・ [Watchdog is quite convenient for file monitoring] (http://qiita.com/PyYoshi@github/items/9744a138c018734c69db) -[LaTex automatic compilation environment made with Python] (http://qiita.com/syutaro/items/414fc12eeb8960b3c29b) ・ [Watchdog 0.8.3 Example API Usage] (https://pypi.python.org/pypi/watchdog)

watchdog can be installed with pip install watchdog. Just to be sure.

3. Execute when there is an update

See sample1.py below for the code. This is the main part to monitor and execute.

easy explanation In addition to the import around the watchdog, import the module for execution.

import sample2

Here we do this to execute the code in sample2.py. (Replace this part with an arbitrary executable file)

sample2.py summarizes the two codes below. Graph Excel data with matplotlib (1) [Convert matplotlib graph to emf file format] (http://qiita.com/gitytm15/items/418ddcd8f58555d7433b)

Since the file update monitoring target is an Excel file here, set the extension like this.

#Files to be monitored
observed_file_type = ('.xls','.xlsx')

Set the shared folder of the Raspberry Pi created earlier to BASEDIR. Originally, BASEDIR indicates the location of the file body

#BASEDIR = os.path.abspath(os.path.dirname(__file__))

Although it is, it is commented out here. This time, the address of the shared folder of Raspberry Pi is specified as follows.

BASEDIR ='/home/pi/share/DATA'

Next is the execution part when an event occurs. This code will be entered for each event. I am throwing the path of the updated Excel file.

sample2.plot(event.src_path) #Execute when creating a file

When you execute sample1.py, Raspberry Pi will enter the monitoring mode of the specified folder. It will be executed each time you move the Excel file to the specified folder, edit it, and save it. This Excel file corresponds to the structure that puts X in A1 and Y in B1 in the XY axis plot. excel.png

sample1.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
import os
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
import sample2 #← Module to be executed according to the update(Here, sample2)

#Files to be monitored
observed_file_type = ('.xls','.xlsx')

#Setting
#BASEDIR = os.path.abspath(os.path.dirname(__file__))
#→ If you want to monitor the location of this program file

#Specify a shared folder for Raspberry Pi
#shared folder[share]In,[DATA]Placed the folder
BASEDIR ='/home/pi/share/DATA'

#Check if the modified file is monitored
def match(path):
    return any([path.endswith(ft) for ft in observed_file_type])

#Event handler at the time of change
class ChangeHandler(FileSystemEventHandler):
    def on_create(self, event):
        if event.is_directory:
            return
        if match(event.src_path):
            print('Create',event.src_path)
            sample2.plot(event.src_path) #Execute when creating a file

    def on_modified(self, event):
        if event.is_directory:
            return
        if match(event.src_path):
            print('Modified',event.src_path)
            sample2.plot(event.src_path) #Execute when changing files

    def on_deleted(self, event):
        if event.is_directory:
            return
        if match(event.src_path):
            print('delete',event.src_path)
            #Do nothing when the file is deleted


if __name__ in '__main__':
    event_handler = ChangeHandler()
    observer = Observer()
    observer.schedule(event_handler, BASEDIR, recursive=True)
    print('start dir=',BASEDIR)
    observer.start()
    try:
        while True:
            time.sleep(0.1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

4. Module for plotting and file conversion

It is called when the Excel file is updated or a new file is created.

The Excel path will be thrown, so read this Excel file and graph it. After that, draw a graph and save it in eps format, and at the same time convert it from svg to emf file and save it. Delete the original svg file.

The required modules are: ・ Pandas ・ Matplotlib ・ Subprocess

sample2.py



#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import subprocess
import os

#Specify the location of the font file ubuntu(C:\Windows\Fonts\Any Font)
fp = FontProperties(fname="/usr/share/fonts/truetype/fonts-japanese-gothic.ttf") 

def dataload(path):    #Load into Pandas DataFrame
    df=pd.read_excel(path)
    return df

def graph(df):

    #Read x-axis and Y-axis data from the data frame
    #Specified by column
    x=df[[0]]
    y=df[[1]]

    #Graph
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.set_yscale('linear')
    
    #ax1.set_yscale('log')
    #ax1.set_ylim(-1.1, 1.1)
    #ax1.set_xlim(0,360)

    ax1.set_title("Graph sample",fontdict = {"fontproperties": fp},fontsize=12)
    ax1.set_xlabel("x axis",fontdict = {"fontproperties": fp},fontsize=12)
    ax1.set_ylabel("y axis",fontdict = {"fontproperties": fp},fontsize=12)

    ax1.scatter(x, y,s=1,c='r',label='sin')
    ax1.legend(loc="upper right")


def graph_save(path):
    
    SAVEDIR=os.path.dirname(path) #File Path
    fname, ext = os.path.splitext( os.path.basename(path) ) #File name only
    
    plt.savefig(SAVEDIR+'/eps/'+fname+".eps") #eps save
    plt.savefig(SAVEDIR+fname+".svg") #Save as svg for emf conversion
    
    call_cmd='inkscape ' + SAVEDIR+fname + '.svg -M ' + SAVEDIR+'/emf/'+fname +'.emf'
    subprocess.call(call_cmd,shell=True)
    
    os.remove(SAVEDIR+fname+".svg") #I don't need svg, so delete it


def plot(path):

    #Called from a monitoring program

    df=dataload(path) #Load Excel as a pandas dataframe
    graph(df) #Graph drawing with matplotlib
    graph_save(path) #Save and convert graphs

if __name__ == '__main__':
    SAVEDIR='/home/pi/share/DATA/'
    path='/home/pi/Desktop/sample.xlsx'
    plot(path)


that's all

Recommended Posts

When a file is placed in the shared folder of Raspberry Pi, the process is executed.
Get the location of the file where the exe is placed when the exe created by PyInstaller is executed
Process the contents of the file in order with a shell script
Get the file name in a folder using glob
Process the files in the folder in order with a shell script
[Note] Import of a file in the parent directory in Python
Change the message displayed when logging in to Raspberry Pi
Is there a secret to the frequency of pi numbers?
Make a note of what you want to do in the future with Raspberry Pi
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
Write a script in Shell and Python to notify you in Slack when the process is finished
On Linux, the time stamp of a file is a little past.
Anyway, the fastest serial communication log is left in a file
The story of the "hole" in the file
[Python] What to do when PEP8 is violated in the process of importing from the directory added to sys.path
Display the signal strength RSSI of a specific SSID (raspberry pi (linux))
What is the XX file at the root of a popular Python project?
A memo organized by renaming the file names in the folder with python
[Linux] Command to get a list of commands executed in the past
Test & Debug Tips: Create a file of the specified size in Python
IPv6 should be disabled when ssh of Raspberry Pi 4 is very slow
View the full path (absolute path) of a file in a directory in Linux Bash
I made a program to check the size of a file in Python
Implement part of the process in C ++
When the target is Ubuntu 16.04 in Ansible
[python] [meta] Is the type of python a type?
Mount Windows shared folder on Raspberry Pi
I created a script to check if English is entered in the specified position of the JSON file in Python.
Gently explain the process of making a simple serverless surveillance camera using Raspberry Pi, Gmail API and Line API
Various ways to read the last line of a csv file in Python
Output timing is incorrect when standard (error) output is converted to a file in Python
Differences in the behavior of each LL language when the list index is skipped
Don't forget to close the file just because it's in a temporary folder
How to make a Raspberry Pi that speaks the tweets of the specified user
[Python] Get the files in a folder with Python
Get the caller of a function in Python
Make a copy of the list in Python
Find the number of days in a month
Output in the form of a python array
How to set a shared folder with the host OS in CentOS7 on VirtualBOX
[Solution] When inserting "0001" into the column of string in sqlite3, it is entered as "1".
How to display the modification date of a file in C language up to nanoseconds
Realize a super IoT house by acquiring sensor data in the house with Raspberry Pi
A memo that implements the job of loading a GCS file into BigQuery in Python
[Raspberry Pi] Store the timestamp in the Firebase Realtime Database when the motion sensor detects it.
When reading a csv file with read_csv of pandas, the first column becomes index
How to check in Python if one of the elements of a list is in another list
[Caution] When creating a binary image (1bit / pixel), be aware of the file format!
When a local variable with the same name as a global variable is defined in the function
I want to be notified of the connection environment when the Raspberry Pi connects to the network
Create a docx file with thumbnails of photos in the folder pasted with the Python python-docx library and the free software "Reduction only."
A program struggle to process a floating-point list in parallel on four clusters of Raspberry Pi 4B via Python socket communication and reduce the loop calculation time to 1/4.
Check if the string is a number in python
This is a sample of function application in dataframe.
When the selected object in bpy.context.selected_objects is not returned
Automatically determine and process the encoding of the text file
Be careful when differentiating the eigenvectors of a matrix
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
Embedding in datetime when only the time is known
Zip 4 Gbyte problem is a story of the past