mazerunner Posted September 12, 2022 Share Posted September 12, 2022 Hey guys! First: English isn't my native language, so I'm sorry for some unconventional writing. Secondly: I just started with the amazing capabilities of Alfred Workflows and im not really in that coding stuff, but I will give my best. So be kind of fair with me.^^ ---My goal so far: I want to calculate Stuff via Sympy in Python and display the result in Alfred.My problem: JSON doesn't support the latex output of Sympy (seems so).My Question: Is there something I dismiss or just did wrong? Or is there a completly other possibility to do it? I will give you a short example of what I did before (it's just like a minimal-example): My Python file: import json import sys import sympy as sym args = sys.argv[1] x = sym.symbols('x') result = {"items": [ { "type": "file", "title": x, "subtitle": "... is the solution for the given equation.", "arg": x } ]} finalResult = json.dumps(result) print(finalResult) My Script: python3 /Users/ma***/Desktop/temp/testfile.py "{query}" The supposed error as I mentioned before: TypeError: Object of type Symbol is not JSON serializable. Note: I just want no additionally window that opens and that I need to close manually. Something like the AsciiMath did would be okay. I hope you can understand my Problem and we can find a solution! Thanks! Link to comment
vitor Posted September 12, 2022 Share Posted September 12, 2022 Welcome @mazerunner, 5 hours ago, mazerunner said: TypeError: Object of type Symbol is not JSON serializable. You need to convert your object to something JSON will understand, such as a String (text). str(x) should do it, but there are other methods. Link to comment
mazerunner Posted September 13, 2022 Author Share Posted September 13, 2022 Hey Vitor! That has solved my current problem. Thanks for that! But how its likely, by solving one Problem there comes another across: I have no clue, how I can receive the input from Alfred just as it typed. Just like: (this was manually written down in the Code) But if I try it with that with my code: import json import sys import sympy as sym args = sys.argv[1:] x = sym.symbols('x') y = sym.solve(args,x) result = {"items": [ { "type": "file", "title": str(y), "subtitle": "... is the solution for the given equation.", "arg": str(y) } ]} finalResult = json.dumps(result) print(finalResult) it gives me: doesn't matter what I haved typed in. Debug: [12:19:31.817] Sympy: Solving Equations with 1 unknown Variable[Script Filter] Queuing argument 'x' [12:19:32.086] Sympy: Solving Equations with 1 unknown Variable[Script Filter] Script with argv '(null)' finished [12:19:32.089] Sympy: Solving Equations with 1 unknown Variable[Script Filter] {"items": [{"type": "file", "title": "[]", "subtitle": "... is the solution for the given equation.", "arg": "[]"}]} [12:19:33.494] Sympy: Solving Equations with 1 unknown Variable[Script Filter] Queuing argument 'x+' [12:19:33.602] Sympy: Solving Equations with 1 unknown Variable[Script Filter] Queuing argument 'x+2' [12:19:33.699] Sympy: Solving Equations with 1 unknown Variable[Script Filter] Script with argv '(null)' finished [12:19:33.702] Sympy: Solving Equations with 1 unknown Variable[Script Filter] {"items": [{"type": "file", "title": "[]", "subtitle": "... is the solution for the given equation.", "arg": "[]"}]} The Queuing Argument seems to be right. I have the guess, that the input isn't the right data-type. Hope you can help me once again. Thanks! Link to comment
mazerunner Posted September 13, 2022 Author Share Posted September 13, 2022 Update: in my Script {query} was missing. xD It's working for right now. Link to comment
vitor Posted September 13, 2022 Share Posted September 13, 2022 21 minutes ago, mazerunner said: in my Script {query} was missing. Avoid with input as {query} in favour of with input as argv. It is the default for a reason: it allows for multiple arguments and you don’t have to worry about escaping. Link to comment
mazerunner Posted September 13, 2022 Author Share Posted September 13, 2022 Thanks for this hint! I have changed it immediately. Link to comment
andreles Posted June 6, 2023 Share Posted June 6, 2023 (edited) If you're using Sympy in Python to perform calculations and you want to display the results in Alfred, JSON might not be the best option for directly supporting the latex output of Sympy here. JSON is primarily used for data interchange and may not have built-in support for complex text formatting like latex. However, there are other possibilities you can explore. One option is to convert the latex output from Sympy into an image format (such as PNG or SVG) and then display that image in Alfred. You can use libraries like Matplotlib or Pillow in Python to generate the image representation of the latex equation. Edited June 9, 2023 by andreles Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now