Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Add User Input Feature to Obsidian Plugin #8

Open
mathe00 opened this issue Oct 1, 2024 · 8 comments
Open

Feature request: Add User Input Feature to Obsidian Plugin #8

mathe00 opened this issue Oct 1, 2024 · 8 comments
Labels
bug Something isn't working enhancement New feature or request help wanted Extra attention is needed

Comments

@mathe00
Copy link
Owner

mathe00 commented Oct 1, 2024

Description:

Currently, the Obsidian plugin allows for communication with Python scripts through a Unix socket. However, there is no feature to request user input from the Obsidian interface. This feature would enable Python scripts to prompt the user for input and receive the response in the Python script. This would be similar to the input() function in Python, but with a more user-friendly interface in Obsidian.

This feature would be implemented by adding a new action to the Unix socket server in the Obsidian plugin. When the Python script sends a request for user input, the Obsidian plugin would display a modal to the user with the appropriate input field (text, number, boolean, or date). The user would then be able to enter their input and submit the form. The Obsidian plugin would then send the user's input back to the Python script as a JSON object.

This feature would be beneficial for a variety of use cases, such as creating dynamic templates, automating tasks, and integrating with external services. It would also enable more interactive and personalized experiences for users of the Obsidian plugin.

Compared to using the input() function in Python in a terminal, this feature would provide a more user-friendly interface in Obsidian. For example, boolean inputs could be represented as checkboxes, date inputs could be represented as date pickers, and other input types could be represented as appropriate UI elements. This would make it easier for users to enter their input and would provide a more seamless experience.

Here is an example of how the feature could be used:

  1. The Python script sends a request for user input to the Obsidian plugin.
  2. The Obsidian plugin displays a modal to the user with a text input field and the message "Please enter your name:".
  3. The user enters their name and submits the form.
  4. The Obsidian plugin sends the user's input back to the Python script as a JSON object: {"userInput": "John Doe", "successStatus": true}.
  5. The Python script receives the user's input and uses it to generate a personalized greeting: "Hello, John Doe!".

This feature would be implemented in a way that is backwards-compatible with existing Python scripts and would not require any changes to the existing API. It would also be documented in the plugin's documentation and would include examples and best practices for using the feature.

I believe that this feature would be a valuable addition to the Obsidian plugin and would enhance its functionality and usability. I would be happy to contribute to the implementation of this feature and would be open to feedback and suggestions during the development process.

@mathe00 mathe00 changed the title Add User Input Feature to Obsidian Plugin Feature request: Add User Input Feature to Obsidian Plugin Oct 1, 2024
@mathe00 mathe00 added the enhancement New feature or request label Oct 1, 2024
mathe00 added a commit that referenced this issue Oct 1, 2024
…. seen in issue #8

added support for user input (like input() in python but in obsidian). seen in issue #8
mathe00 added a commit that referenced this issue Oct 1, 2024
…. seen in issue #8

added support for user input (like input() in python but in obsidian). seen in issue #8
@mathe00
Copy link
Owner Author

mathe00 commented Oct 1, 2024

Hello,

I am happy to inform you that I have successfully developed the user input feature in the Obsidian plugin. This feature enables Python scripts to prompt the user for input and receive the response in the Python script.

However, I have encountered an issue with the output returned by Obsidian. The output is not being parsed correctly as I would like. I have implemented this feature in the commit 7bd7b6f and efaa5ab, but the output returned by Obsidian is nested in an unexpected way.

The problem is as follows: when a Python script sends a request for user input to Obsidian, Obsidian displays a modal to the user with the appropriate input field. When the user submits the form, Obsidian sends the user's input back to the Python script as a JSON object. However, the output returned by Obsidian is nested in an unexpected way, making it difficult to extract the data in the Python script.

I am open to any suggestions or help to resolve this issue. I have included an example of the output returned by Obsidian in the commit, so that you can see the problem in question.

Thank you for your attention and assistance.

@mathe00
Copy link
Owner Author

mathe00 commented Oct 1, 2024

Need Help Refactoring JSON Parsing from Obsidian

Hi everyone,

I need someone's help to refactor the JSON parsing returned by Obsidian. Currently, the JSON I receive looks like this:

{
    'inputValue':
    {
        'inputValue': 'RandomNameIType',
        'successStatus': True,
        'success': False
    }
}

However, I would like the JSON to be structured as follows:

{
  "inputValue": "RandomNameIType",
  "successStatus": true
}

If anyone has the time and skills to help me refactor this parsing, I would be really grateful. Thank you in advance for your help!

@mathe00 mathe00 added bug Something isn't working help wanted Extra attention is needed labels Oct 1, 2024
@gfxholo
Copy link

gfxholo commented Oct 1, 2024

I need someone's help to refactor the JSON parsing returned by Obsidian. Currently, the JSON I receive looks like this:

{
    'inputValue':
    {
        'inputValue': 'RandomNameIType',
        'successStatus': True,
        'success': False
    }
}

Hiya. I did a search through your branched versions of main.ts and UserInputModal.ts, but I didn't find any property named success. Is this the raw JSON you get after calling request_user_input()?

@mathe00
Copy link
Owner Author

mathe00 commented Oct 1, 2024

I need someone's help to refactor the JSON parsing returned by Obsidian. Currently, the JSON I receive looks like this:

{
    'inputValue':
    {
        'inputValue': 'RandomNameIType',
        'successStatus': True,
        'success': False
    }
}

Hiya. I did a search through your branched versions of main.ts and UserInputModal.ts, but I didn't find any property named success. Is this the raw JSON you get after calling request_user_input()?

I'll have to take a look. I've never really used git and all that. I've made a bit of a mess between branches and my commits. I think I'll take a look at a tutorial on all this and get it all back up to scratch today or in the next few days. And I'm also thinking of publishing a clean release within the week too. But I'm getting too tangled up by not really knowing what I'm doing. I really need REAL developers who know their stuff 😅. Sorry again for all this mess. I'm really going to try to get everything back in order and not touch it too much anymore.

@mathe00
Copy link
Owner Author

mathe00 commented Oct 1, 2024

Update on Branch Management and Project Organization

Hi everyone,

First of all, I want to apologize for the confusion and the current mess in the branch management of the project. I’ve realized that I should have done more research beforehand, but this has been a valuable learning experience. I’m not a developer by trade, but I’m learning a lot about TypeScript, Git, GitHub, socket communication, and overall project management as I go.

After some research and feedback, I’ve learned the importance of structuring branches properly (main, develop, feature/, release/, hotfix/), and I’ll be reorganizing everything in the next few days. This will allow the project to evolve more smoothly moving forward, and I’ll ensure the commits and branches are more structured.

I also plan to publish a clean and stable release soon, without touching the branches unnecessarily again. Thanks for your understanding, and again, sorry for any inconvenience caused.

Feel free to reach out if you have any suggestions or can provide further help!

@gfxholo

@gfxholo
Copy link

gfxholo commented Oct 2, 2024

No worries, take your time learning! Remember you can test git commands offline where nothing is permanent 💚

mathe00 added a commit that referenced this issue Oct 2, 2024
Merged the `unstable` branch into `main` to integrate the latest features, including support for user input via the Obsidian interface, as described in issue #8.
@mathe00
Copy link
Owner Author

mathe00 commented Oct 2, 2024

Hello again @gfxholo !

I've just reorganized everything properly normally and I've set up Github Desktop on my local computer so everything should be ready. I've just tested again for a text/str input and it works. Except that for other types like a number, a Boolean (checkbox), or a date to enter (with the graphical date selector in Obsidian's GUI), it doesn't work. However, I believe the issue with the number input is due to the fact that we need to select the number using an increment/decrement bar (see screenshot).

Would it be possible for you to take a quick look and fix it for me? I think it's because I've coded some lines incorrectly and they're not making sense. Thanks so much in advance if you can do this!

I'm attaching some screenshots and console logs for you to see the problem.

NOTE: you can develop in the develop branch now to do everything right. (Except for this exit, because I've created a special branch to do it as cleanly as possible : hotfix/8-feature-request-add-user-input-feature-to-obsidian-plugin )


Tests for user input with simple text

With the following python code :

import sys
import os

# -----------------------------------------
# Set up library path and import module
# -----------------------------------------

# Use the absolute path to the library
library_path = "/home/me/Document/Obsidian/MyVault/.obsidian/plugins/obsidian-python-bridge/" My full path to ObsidianPluginDevPythonToJS.py
sys.path.append(library_path)

# Import the Python-Obsidian bridge module
from ObsidianPluginDevPythonToJS import ObsidianPluginDevPythonToJS

# -----------------------------------------
# Initialize the class with socket path
# -----------------------------------------

# Define the path to the Unix socket for communication with Obsidian
socket_path = "/tmp/obsidian-python.sock"

# Create an instance of the ObsidianPluginDevPythonToJS class
obsidian = ObsidianPluginDevPythonToJS(socket_path=socket_path)

# -----------------------------------------
# Request and handle user input (text)
# -----------------------------------------

# Request text input from the user
response = obsidian.request_user_input(
    script_name="Text Input", 
    input_type="text", 
    message="Please enter your name:"
)

# Send a notification with the user's input
obsidian.send_notification(content=f"The first name is: {response['userInput']}")

And here are the screenshots to show you the procedure, which works perfectly for this test :

image

image

image

image

Tests for user input with Boolean

With the following python code :

import sys
import os

# -----------------------------------------
# Set up library path and import module
# -----------------------------------------

# Use the absolute path to the library
library_path = "/home/me/Document/Obsidian/MyVault/.obsidian/plugins/obsidian-python-bridge/" My full path to ObsidianPluginDevPythonToJS.py
sys.path.append(library_path)

# Import the Python-Obsidian bridge module
from ObsidianPluginDevPythonToJS import ObsidianPluginDevPythonToJS

# -----------------------------------------
# Initialize the class with socket path
# -----------------------------------------

# Define the path to the Unix socket for communication with Obsidian
socket_path = "/tmp/obsidian-python.sock"

# Create an instance of the ObsidianPluginDevPythonToJS class
obsidian = ObsidianPluginDevPythonToJS(socket_path=socket_path)

# -----------------------------------------
# Request and handle user input (boolean)
# -----------------------------------------

# Request boolean input from the user (Yes/No confirmation)
response = obsidian.request_user_input(
    script_name="Boolean Input", 
    input_type="boolean", 
    message="Are you sure you want to proceed?"
)

if response['userInput'] == 'True' or True:
    response = True

obsidian.send_notification(content=f"The response is: {response}")

image

image

Tests with date

With the following python code :

import sys
import os

# -----------------------------------------
# Set up library path and import module
# -----------------------------------------

# Use the absolute path to the library
library_path = "/home/me/Document/Obsidian/MyVault/.obsidian/plugins/obsidian-python-bridge/" My full path to ObsidianPluginDevPythonToJS.py
sys.path.append(library_path)

# Import the Python-Obsidian bridge module
from ObsidianPluginDevPythonToJS import ObsidianPluginDevPythonToJS

# -----------------------------------------
# Initialize the class with socket path
# -----------------------------------------

# Define the path to the Unix socket for communication with Obsidian
socket_path = "/tmp/obsidian-python.sock"

# Create an instance of the ObsidianPluginDevPythonToJS class
obsidian = ObsidianPluginDevPythonToJS(socket_path=socket_path)

# -----------------------------------------
# Request and handle user input (date)
# -----------------------------------------

# Request date input from the user
response = obsidian.request_user_input(
    script_name="Date Input", 
    input_type="date", 
    message="Please enter your birthday:"
)

obsidian.send_notification(content=f"The date is : {response['userInput']}")

image

image

Tests with number (error/bugs)

With the following python code :

import sys
import os

# -----------------------------------------
# Set up library path and import module
# -----------------------------------------

# Use the absolute path to the library
library_path = "/home/me/Document/Obsidian/MyVault/.obsidian/plugins/obsidian-python-bridge/" My full path to ObsidianPluginDevPythonToJS.py
sys.path.append(library_path)

# Import the Python-Obsidian bridge module
from ObsidianPluginDevPythonToJS import ObsidianPluginDevPythonToJS

# -----------------------------------------
# Initialize the class with socket path
# -----------------------------------------

# Define the path to the Unix socket for communication with Obsidian
socket_path = "/tmp/obsidian-python.sock"

# Create an instance of the ObsidianPluginDevPythonToJS class
obsidian = ObsidianPluginDevPythonToJS(socket_path=socket_path)

# -----------------------------------------
# Request and handle user input (number with validation)
# -----------------------------------------

# Request number input from the user with validation regex
response = obsidian.request_user_input(
    script_name="Number Input", 
    input_type="number", 
    message="Please enter a number between 1 and 10:", 
    validation_regex="^[1-9]$|^10$", 
    min_value=1, 
    max_value=10
)

# Send a notification with the user's input
obsidian.send_notification(content=f"The number is: {response['userInput']}")

image

It's impossible to validate the number at the same time, so... In fact, there's a bar, because I'd like the python script dev to be able to choose between typing the number in a field or incrementing/decrementing a bar (which could be user-friendly depending on the situation).

The error doesn't come from the regex, I'm going to make another issue, because the regex doesn't work at all, even on simple text input.

@mathe00
Copy link
Owner Author

mathe00 commented Oct 2, 2024

The error doesn't come from the regex, I'm going to make another issue, because the regex doesn't work at all, even on simple text input.

✅ Issue created : #9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants