As you’re building, we know that there are some common things you’ll need to do in a notebook (like save to your output directory). Below, we have provided some helpful code snippets that you may use in your notebooks.
If you would like to add one to the list, let us know in the Losant Forums.
Reading input files from the
import os import pandas as pd # get the file path to dataset.csv input_dir = os.path.join(os.environ['INPUT_DIR'], 'dataset.csv') # load the csv as a pandas dataframe dataset = pd.read_csv(input_dir)
Saving output files to the
import os myData = # dataframe to export myChart = # create chart to export # save a csv myData.to_csv(os.path.join(os.environ['OUTPUT_DIR'], 'myData.csv')) # save a file myChart.get_figure().savefig(os.path.join(os.environ['OUTPUT_DIR'], 'myChart.png'))
# read data dataset = pd.read_csv(os.path.join(os.environ['INPUT_DIR'], 'dataset.csv')) # convert to datetime dataset['Timestamp'] = pd.to_datetime(dataset['Timestamp'], unit='ms') # Set the timestamp as the index (optional) dataset.set_index('Timestamp', inplace=True)
When developing locally, you may just want to set your inputs and outputs to your present working directory. Here is an easy way to accomplish that:
INPUT_DIR=$(pwd) OUTPUT_DIR=$(pwd) jupyter notebook
When passing in Notebook Context, you may want to retrieve particular properties from it. Here is an example of retrieving a value at
import os import json context_path = os.path.join(os.environ['INPUT_DIR'], "context.json") context = json.load(context_path) device = context['data']['device']
Was this page helpful?
Still looking for help? You can also search the Losant Forums or submit your question there.