Certainly! Below is a simple example of how to create an Android app in Python using the Kivy library to plot mathematical functions. This example assumes that you have already installed Kivy, Kivy Garden, Matplotlib, and SymPy as described in the previous steps. It demonstrates plotting a single-variable function entered by the user.
We can't directly compile or run this code here, but you can try it on your development machine.
First, let's write the Python script (main.py) that sets up the UI and handles the plotting:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
import matplotlib.pyplot as plt
import numpy as np
from sympy import symbols, lambdify, sympify
class PlotterApp(App):
def build(self):
self.box = BoxLayout(orientation='vertical')
# User input for function
self.input_function = TextInput(hint_text='Enter function (e.g., "sin(x)")', multiline=False)
self.box.add_widget(self.input_function)
# Plot button
self.plot_button = Button(text='Plot', size_hint=(1, 0.1))
self.plot_button.bind(on_release=self.plot_graph)
self.box.add_widget(self.plot_button)
# Graph placeholder
self.figure, self.ax = plt.subplots()
self.canvas = FigureCanvasKivyAgg(self.figure)
self.box.add_widget(self.canvas)
return self.box
def plot_graph(self, instance):
function_input = self.input_function.text.strip()
# Handle invalid input
if not function_input:
return
x = symbols('x')
try:
# Parse input into a usable function
parsed_function = sympify(function_input)
# Create Numpy lambda function
f = lambdify(x, parsed_function, 'numpy')
# Generate data for plotting
x_vals = np.linspace(-10, 10, 1000)
y_vals = f(x_vals)
# Clear current plot
self.ax.clear()
# Plot new function
self.ax.plot(x_vals, y_vals)
# Draw the updated plot
self.canvas.draw_idle()
except Exception as e:
print(f"Error parsing function: {e}")
if __name__ == '__main__':
PlotterApp().run()
After you have written the script, you need to initialize Buildozer and configure the buildozer.spec file. Run the following from the terminal in your project directory:
buildozer init
Now configure your buildozer.spec file, setting the requirements line to include our dependencies:
requirements = python3,kivy,sympy,numpy,matplotlib
Make sure you have the following line in your buildozer.spec file to enable permissions for your app to access external storage, which may be needed for matplotlib:
android.permissions = INTERNET,WRITE_EXTERNAL_STORAGE
It's also good practice to add matplotlib to the garden requirements:
garden_requirements = matplotlib
Once you've set up your buildozer.spec, run the following in the terminal to build, deploy, and run your application:
buildozer -v android debug deploy run
This script provides a starting point and there's a lot more to do to make your application production ready. You'll need to handle incorrect input more gracefully, potentially add more complex plotting features, and generally test thoroughly on different Android devices.