question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

QUESTION: How to hvplot from csv file updated every 1 second

See original GitHub issue

Hello I have a very simple use case. I have the following python script that just generates CPU usage of my machine every second and saves the data into a csv file. I am trying to use the streamz library to then use this csv file as a source data and plot the data using hvplot library.

Here’s my script for generating CPU usage date:

# pip install psutil
import csv
import psutil
import time

x_value = 0

fieldnames = ["x_value", "cpu_perc"]


with open('data.csv', 'w') as csv_file:
    csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    csv_writer.writeheader()

while True:

    with open('data.csv', 'a') as csv_file:
        csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
        y_value = psutil.cpu_percent()

        info = {
            "x_value": x_value,
            "cpu_perc": y_value
        }

        csv_writer.writerow(info)
        print(x_value, y_value)

        x_value += 1

    time.sleep(1)

Then in my jupyter notebook, I’ve tried:

import hvplot.streamz
import pandas as pd
from streamz import Stream

source = Stream.from_textfile('data/data.csv', poll_interval=1)
source.start()
example = pd.DataFrame({'x': [], 'cpu_perc': []})
sdf = source.to_dataframe(example=example)
sdf.hvplot()

But I get an emtpy bokeh chart. I looked at the dataframe page in the documentation, but doesn’t really show how to use a csv file. I looked at the API docs and found the from_textfile() method from the Stream class and also found the to_dataframe() method, and just went from there.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
martindurantcommented, Sep 8, 2020

@chinmaychandak - an excellent example of the documentation being inadequate?

Perhaps it would be nice to have some sort of debug mode where, as an event travels through the graph, you can see what becomes of it at each node.

0reactions
jbednarcommented, Sep 25, 2020

Sort of, though my version (tornado_data_stream.ipynb in https://anaconda.org/jbednar/project/streaming_hvplot) doesn’t use a CSV file. But I think we were about to make a new release of streamz that would make it a whole lot simpler.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Plotting — hvPlot 0.8.2 documentation
A small CSV file of US crime data, broken down by state. A larger Parquet-format file of airline flight data. The hvplot.sample_data module...
Read more >
Update chart every time a point added to CSV file - python
I just found a solution, thanks to my hero Corey Schafer and his useful tutorial on YouTube : https://www.youtube.com/watch?v=Ercd-Ip5PfQ
Read more >
Difference between pandas and hvplot for ecobee dataset #289
I'm very confused now. Correcting the read_csv parsing aligns the behavior of plot() and hvplot() , but why was it different then before?...
Read more >
Advanced Data Visualization in Python with HoloViews
The point of showing this is to make sure that the reader has all the dependent packages installed. Please note the hvplot package...
Read more >
Chart visualization — pandas 1.5.2 documentation - PyData |
In [1]: import matplotlib.pyplot as plt In [2]: plt.close("all") ... You can plot one column versus another using the x and y keywords...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found