Linked Questions

Popular Questions

I implemented a simple addin in python to retrieve stocks prices and other data via an API. It works ok, however I have many tickers and the whole thing blocks for a few minutes because each cell is evaluated one by one and the latency to the API just adds up.

Is there any way to launch a thread and return a temporary "loading..." value and repopulate the cell with data when it becomes available? I've tried doing it but the documentation is very scarce and I couldn't find out how to populate the data from the threads after the requests finishes.

The code is simple and looks like this:

import json
import urllib.request

import uno
import unohelper
from com.stizard.api import XStizardApi

class StizardImpl(unohelper.Base, XStizardApi):
    def __init__(self, ctx):
        self.ctx = ctx
    def stizardPrice(self, symbol):
        with urllib.request.urlopen("https://www.example.com/prices/prices.php?symbol=" + symbol) as response:
            rawResponse = response.read()
            data = json.loads(rawResponse)
            return data['price']
    def stizard(self, symbol, q = "price"):
        with urllib.request.urlopen("https://www.example.com/prices/prices.php?symbol=" + symbol +"&q=" + q) as response:
            rawResponse = response.read()
            data = json.loads(rawResponse)
            return data['price']

def createInstance(ctx):
    return StizardImpl(ctx)
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(
    createInstance,
    "com.stizard.api.python.StizardImpl",
    ("com.sun.star.sheet.AddIn",)
)

Later edit: idl file XStizardApi.idl

#include <com/sun/star/uno/XInterface.idl>

module com { module stizard { module api {

    interface XStizardApi {
        double stizardPrice( [in] string symbol );
        string stizard( [in] string symbol, [in] string q);
    };

}; }; };

description.xml

<?xml version="1.0" encoding="UTF-8"?>
<description xmlns="http://openoffice.org/extensions/description/2006"
xmlns:d="http://openoffice.org/extensions/description/2006"
xmlns:xlink="http://www.w3.org/1999/xlink">

<dependencies>
    <OpenOffice.org-minimal-version value="2.4" d:name="OpenOffice.org 2.4"/>
</dependencies>

<identifier value="com.stizard.api" />
<version value="1.0.0" />
<display-name><name lang="en">Stizard API</name></display-name>
<publisher><name xlink:href="http://www.github.com/" lang="en">Stizard.com</name></publisher>

</description>

Related Questions