using Kivy Garden Graph in KV language

7.7k Views Asked by At

How do I use the kivy module garden.graph inside the kv file? I only found documentation that explained how to use it in the main python script.

I imported the kivy.garden.graph in the python file, and I can add the Graph inside the kv file, but I didn't find any documentation how to set the size, plots etc.

Graph:
    id: graph_test
    plot: MeshLinePlot

this gives an error since MeshLinePlot is not defined, though I imported it on the python side.

any help would be highly appreciated, maybe we could then add this info to the graph's github readme as well.

4

There are 4 best solutions below

0
On

Building on the answer from piwnk:

I added this to the .kv file:

#:import MeshLinePlot kivy.garden.graph.MeshLinePlot
<SetGraph>:
    graph_test : graph_test
    Graph:
    id: graph_test
    plot: MeshLinePlot
    xlabel:'X'
    ylabel:'Y'
    x_ticks_minor:5
    x_tics_major:25
    y_ticks_major:1
    y_grid_label:True
    x_grid_label:True
    padding:5
    x_grid:True
    y_grid:True
    xmin:-0
    xmax:100
    ymin:-1
    ymax:1
    pos: 0, root.height / 6
    size: root.width * 2 / 3 , root.height * 18 / 24

In main.py, I added:

from math import sin
from kivy.garden.graph import Graph, MeshLinePlot

class SetGraph(Widget):
    graph_test = ObjectProperty(None)

    update_graph(self):
         plot = MeshLinePlot(color=[1, 0, 0, 1])
         plot.points = [(x, sin(x / 10.)) for x in range(0, 101)]
         self.graph_test.add_plot(plot)

class graphLayoutApp(App):
    def build(self):
        disp = SetGraph()
        disp.update_graph()
        return disp


if __name__ == '__main__':
    graphLayoutApp().run()

I have changed my original tested solution to more descriptive names. Hopefully, I have not made any mistakes. Let me know if the solution is not complete.

0
On

I think inclement was on the right track. Using #:import should be able to import the file.

write this in the kv file:

#:import MeshLinePlot

it should be able to import the module, as the kv documentation shows, also

0
On

Had the same problem. Here's the solution:

Generally, according to kivy documentation, in kv file:

#:import name x.y.z

is equivalent to:

from x.y import z as name

So you should use the following:

#:import MeshLinePlot kivy.garden.graph.MeshLinePlot

Worked in my case with Graph class but, to be honest, I didn't managed to add this plot to the graph yet.

0
On

The answer from Mattis Asp was very helpful but didn't quite work for me. I am new to this, so maybe these things are too obvious to need stating. But in case it helps someone else at my level, I had to:

  1. Indent the properties under the Graph: declaration in the kv file (to get around an "invalid data after declaration" exception from the kv parser.

  2. Add these includes:

    language: lang-py

    from kivy.properties import ObjectProperty  
    from kivy.app import App    
    from kivy.uix.widget import Widget  
    

to the top of the python file.

  1. Name the kv file to match the app class definition, so: graphLayout.kv (I had called it graph.kv so it was just ignored -- newbie mistake!)

  2. I was getting "invalid property name" for graph_test : graph_test. So I commented that out and used the id instead, changing the line

    self.graph_test.add_plot(plot)  
    

    to:

    self.ids["graph_test"].add_plot(plot)
    

I bet at least some of these changes have to do with version differences in kivy so, for clarity, I am using kivy 1.9.1 and python 2.7.13.