Skip to content

Commit

Permalink
Renamed @Pro decorator into @server. @Pro remains working though. (#2320
Browse files Browse the repository at this point in the history
)

* Renamed @Pro decorator into @server. @Pro remains working though.

* fixed add-in for server refactor

* resolve merge conflicts
  • Loading branch information
fzumstein committed Aug 25, 2023
1 parent 95dfbbc commit d931d60
Show file tree
Hide file tree
Showing 9 changed files with 442 additions and 437 deletions.
78 changes: 39 additions & 39 deletions docs/pro/server/officejs_custom_functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ As long as you don't change the name or arguments of the function, you can edit
Basic syntax
------------

As you could see in the quickstart sample, the simplest custom function only requires the ``@pro.func`` decorator:
As you could see in the quickstart sample, the simplest custom function only requires the ``@server.func`` decorator:

.. code-block:: python
from xlwings import pro
from xlwings import server
@pro.func
@server.func
def hello(name):
return f"Hello {name}!"
.. note::

The decorators for Office.js are imported from ``xlwings.pro`` instead of ``xlwings`` and therefore read ``pro.func`` instead of ``xw.func``. See also :ref:`pro/server/officejs_custom_functions:Custom functions vs. legacy UDFs`.
The decorators for Office.js are imported from ``xlwings.server`` instead of ``xlwings`` and therefore read ``server.func`` instead of ``xw.func``. See also :ref:`pro/server/officejs_custom_functions:Custom functions vs. legacy UDFs`.

Python modules
--------------
Expand All @@ -56,17 +56,17 @@ Note that ``custom_functions`` needs to be imported where you define the require
pandas DataFrames
-----------------

By using the ``@pro.arg`` and ``@pro.ret`` decorators, you can apply converters and options to arguments and the return value, respectively.
By using the ``@server.arg`` and ``@server.ret`` decorators, you can apply converters and options to arguments and the return value, respectively.

For example, to read in the values of a range as pandas DataFrame and return the correlations without writing out the header and the index, you would write:

.. code-block:: python
from xlwings import pro
@pro.func
@pro.arg("df", pd.DataFrame, index=False, header=False)
@pro.ret(index=False, header=False)
@server.func
@server.arg("df", pd.DataFrame, index=False, header=False)
@server.ret(index=False, header=False)
def correl2(df):
return df.corr()
Expand All @@ -81,8 +81,8 @@ To describe your function and its arguments, you can use a function docstring or
from xlwings import pro
@pro.func
@pro.arg("name", doc='A name such as "World"')
@server.func
@server.arg("name", doc='A name such as "World"')
def hello(name):
"""This is a classic Hello World example"""
return f"Hello {name}!"
Expand All @@ -103,8 +103,8 @@ In the context of custom functions, xlwings will detect numbers, strings, and bo
import datetime as dt
from xlwings import pro
@pro.func
@pro.arg("date", dt.datetime)
@server.func
@server.arg("date", dt.datetime)
def isoformat(date):
return date.isoformat()
Expand All @@ -118,7 +118,7 @@ If you have multiple values that you need to convert, you can use the ``xlwings.
import xlwings as xw
from xlwings import pro
@pro.func
@server.func
def isoformat(dates):
dates = [xw.to_datetime(d) for d in dates]
return [d.isoformat() for d in dates]
Expand All @@ -130,8 +130,8 @@ And if you are dealing with pandas DataFrames, you can simply use the ``parse_da
import pandas as pd
from xlwings import pro
@pro.func
@pro.arg("df", pd.DataFrame, parse_dates=[0])
@server.func
@server.arg("df", pd.DataFrame, parse_dates=[0])
def timeseries_start(df):
return df.index.min()
Expand All @@ -146,7 +146,7 @@ When writing datetime object to Excel, xlwings automatically formats the cells a
import xlwings as xw
import datetime as dt
@pro.func
@server.func
def pytoday():
return dt.date.today()
Expand All @@ -157,8 +157,8 @@ By default, it will format the date according to the content language of your Ex
import xlwings as xw
import datetime as dt
@pro.func
@pro.ret(date_format="yyyy-m-d")
@server.func
@server.ret(date_format="yyyy-m-d")
def pytoday():
return dt.date.today()
Expand All @@ -178,7 +178,7 @@ A namespace groups related custom functions together by prepending the namespace
import numpy as np
from xlwings import pro
@pro.func(namespace="numpy")
@server.func(namespace="numpy")
def standard_normal(rows, columns):
rng = np.random.default_rng()
return rng.standard_normal(size=(rows, columns))
Expand All @@ -191,7 +191,7 @@ You can create sub-namespaces by including a dot like so:

.. code-block:: python
@pro.func(namespace="numpy.random")
@server.func(namespace="numpy.random")
This function will be shown as ``NUMPY.RANDOM.STANDARD_NORMAL`` in Excel.

Expand All @@ -216,7 +216,7 @@ You can include a link to an internet page with more information about your func
from xlwings import pro
@pro.func(help_url="https://www.xlwings.org")
@server.func(help_url="https://www.xlwings.org")
def hello(name):
return f"Hello {name}!"
Expand All @@ -240,8 +240,8 @@ However, if the argument can be anything from a single cell to a one- or two-dim

.. code-block:: python
@pro.func
@pro.arg("x", ndim=2)
@server.func
@server.arg("x", ndim=2)
def add_one(x):
return [[cell + 1 for cell in row] for row in data]
Expand All @@ -253,8 +253,8 @@ If you need to write out a list in vertical orientation, the ``transpose`` optio

.. code-block:: python
@pro.func
@pro.ret(transpose=True)
@server.func
@server.ret(transpose=True)
def vertical_list():
return [1, 2, 3, 4]
Expand Down Expand Up @@ -284,8 +284,8 @@ By default, error cells are converted to ``None`` (scalars and lists) or ``np.na

.. code-block:: python
@pro.func
@pro.arg("x", err_to_str=True)
@server.func
@server.arg("x", err_to_str=True)
def myfunc(x):
...
Expand All @@ -295,7 +295,7 @@ To format cells as proper error cells in Excel, simply use their string represen

.. code-block:: python
@pro.func
@server.func
def myfunc(x):
return ["#N/A", "#VALUE!"]
Expand All @@ -314,7 +314,7 @@ Returning a simple list:
from xlwings import pro
@pro.func
@server.func
def programming_languages():
return ["Python", "JavaScript"]
Expand All @@ -325,7 +325,7 @@ Returning a NumPy array with standard normally distributed random numbers:
import numpy as np
from xlwings import pro
@pro.func
@server.func
def standard_normal(rows, columns):
rng = np.random.default_rng()
return rng.standard_normal(size=(rows, columns))
Expand All @@ -337,7 +337,7 @@ Returning a pandas DataFrame:
import pandas as pd
from xlwings import pro
@pro.func
@server.func
def get_dataframe():
df = pd.DataFrame({"Language": ["Python", "JavaScript"], "Year": [1991, 1995]})
return df
Expand All @@ -352,7 +352,7 @@ Volatile functions are recalculated whenever Excel calculates something, even if
import datetime as dt
from xlwings import pro
@pro.func(volatile=True)
@server.func(volatile=True)
def last_calculated():
return f"Last calculated: {dt.datetime.now()}"
Expand Down Expand Up @@ -382,17 +382,17 @@ The backend needs to implement the following three endpoints to support custom f
@app.get("/xlwings/custom-functions-meta")
async def custom_functions_meta():
return xw.pro.custom_functions_meta(custom_functions)
return xw.server.custom_functions_meta(custom_functions)
@app.get("/xlwings/custom-functions-code")
async def custom_functions_code():
return PlainTextResponse(xw.pro.custom_functions_code(custom_functions))
return PlainTextResponse(xw.server.custom_functions_code(custom_functions))
@app.post("/xlwings/custom-functions-call")
async def custom_functions_call(data: dict = Body):
rv = await xw.pro.custom_functions_call(data, custom_functions)
rv = await xw.server.custom_functions_call(data, custom_functions)
return {"result": rv}
.. tab-item:: Starlette
Expand All @@ -404,16 +404,16 @@ The backend needs to implement the following three endpoints to support custom f
import custom_functions
async def custom_functions_meta(request):
return JSONResponse(xw.pro.custom_functions_meta(custom_functions))
return JSONResponse(xw.server.custom_functions_meta(custom_functions))
async def custom_functions_code(request):
return PlainTextResponse(xw.pro.custom_functions_code(custom_functions))
return PlainTextResponse(xw.server.custom_functions_code(custom_functions))
async def custom_functions_call(request):
data = await request.json()
rv = await xw.pro.custom_functions_call(data, custom_functions)
rv = await xw.server.custom_functions_call(data, custom_functions)
return JSONResponse({"result": rv})
You'll also need to load the custom functions by adding the following line at the end of the ``head`` element in your HTML file, see ``app/taskpane.html`` in the quickstart project:
Expand Down Expand Up @@ -561,7 +561,7 @@ While Office.js-based custom functions are mostly compatible with the VBA-based
- Requires ``@xw.func(async_mode="threading")``

* - Decorators
- ``from xlwings import pro``, then ``pro.func`` etc.
- ``from xlwings import pro``, then ``server.func`` etc.
- ``import xlwings as xw``, then ``xw.func`` etc.

* - Formula Intellisense
Expand Down
2 changes: 1 addition & 1 deletion xlwings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class NoSuchObjectError(XlwingsError):

# UDFs
if sys.platform.startswith("win") and has_pywin32:
from .server import serve
from .com_server import serve
from .udfs import (
get_udf_module,
import_udfs,
Expand Down
2 changes: 1 addition & 1 deletion xlwings/addin/Main.bas
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ Function XLPyCommand()
Else
' Spaces in path of python.exe require quote around path AND quotes around whole command, see:
' https://stackoverflow.com/questions/6376113/how-do-i-use-spaces-in-the-command-prompt
tail = " -B -c ""import sys, os;" & LicenseKeyEnvString & "import xlwings.utils;xlwings.utils.prepare_sys_path(\""" & PYTHONPATH & "\"");import xlwings.server; xlwings.server.serve('$(CLSID)')"""
tail = " -B -c ""import sys, os;" & LicenseKeyEnvString & "import xlwings.utils;xlwings.utils.prepare_sys_path(\""" & PYTHONPATH & "\"");import xlwings; xlwings.serve('$(CLSID)')"""
XLPyCommand = PYTHON_WIN & tail & Chr(34)
End If
End Function
Expand Down
Binary file modified xlwings/addin/xlwings.xlam
Binary file not shown.

0 comments on commit d931d60

Please sign in to comment.