Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

Commenting System

Hi Guys, I have build a multipage app and I have also built a commenting system like when user write any text from one page and than he clicks on submit than it will be saved as text file. Than this text file is read in both the pages and returned as comment. I have submit button only on one page and the user can view this comments on another page. If I want to delete this comment from one page from where I am submitting this than what should I do?

I want to delete the comment line by line like putting the delete button on each text line and after the text is deleted from the dash it should be deleted from text file too.

How can I do it?

Check out the pattern matching callbacks chapter - there is a “todo app” that illustrates how to delete items: Pattern-Matching Callbacks | Dash for Python Documentation | Plotly

1 Like

I will follow it. Thank you so much :slight_smile:

Hey can you please share your code? I am struggling to create a comments system!

here you go…

html.Div([

  html.H3(' Admin Comment Section'),

  

  dcc.Textarea(

    id='textarea-state-example',

    value='',

    style={'width': '100%', 'height': 30},  

),

            

  html.Button('Submit', id='textarea-state-example-button', n_clicks=0),

   html.Div(id='textarea-state-example-output', style={'whiteSpace': 'pre-line'})

])
@app.callback(

Output('textarea-state-example-output', 'children'),

[Input('textarea-state-example-button', 'n_clicks')],

 

[State('textarea-state-example', 'value')]

)

def output_value(n_clicks,value):

if n_clicks > 0:

    

    f=open("comment.txt", "a+")

    print(value,file=f)

    f.close()



f = open('comment.txt', 'r')

lines = f.read().splitlines()[1:300]

f.close()

return html.Ul([html.Li(x) for x in lines])
1 Like

Hi I successfully write a line in text file on one click
adding = len([1 for i in triggered if i in (“add.n_clicks”, “new-item.n_submit”)])

if adding:

    f=open("comment.txt", "a+")

    print(new_item,file=f)

    f.close()

But I am not able to delete certain line of text

clearing = len([1 for i in triggered if i == “clear-done.n_clicks”])

if clearing:

What shall I code in here so that it can be deleted from text file too…

Thanks

I want to delete the lines by selecting it and it should be removed from text file too… And this should be displayed on dash as well. I am getting little bit of confused on how to do that.

Thanks

The given code is not working. My application index.pyfile looks like:

import dash_core_components as dcc
import dash_bootstrap_components as dbc
import dash_html_components as html
from dash.dependencies import Input, Output

from app import app
from flask_login import logout_user, current_user
from apps import welcome,dashboard,live_twitter_sentiment,login,success,comments,login_fd,logout
from navbar import Navbar
navi=Navbar()


app.layout = html.Div([
    navi,
    dcc.Location(id='url', refresh=False),
    html.Div(id='page-content')
])


@app.callback(Output('page-content', 'children'),
              [Input('url', 'pathname')])
def display_page(pathname):
    if pathname == '/' or pathname=='/welcome':
         return welcome.layout
    elif pathname == '/dashboard':
         return dashboard.layout
    elif pathname == '/live_twitter_sentiment':
         return live_twitter_sentiment.layout
    elif pathname == '/login':
        return login.layout
    elif pathname == '/success':
        if current_user.is_authenticated:
            return comments.layout
        else:
            return login_fd.layout
    elif pathname == '/logout':
        if current_user.is_authenticated:
            logout_user()
            return logout.layout
        else:
            return logout.layout
    else:
        return '404'

external_stylesheets = [dbc.themes.BOOTSTRAP]
                
if __name__ == '__main__':
    app.run_server(debug=True,dev_tools_hot_reload= True,threaded=True)



@app.callback(
    Output('user-name', 'children'),
    [Input('page-content', 'children')])
def cur_user(input1):
    if current_user.is_authenticated:
        return html.Div('Current user: ' + current_user.username)
        # 'User authenticated' return username in get_id()
    else:
        return ''


@app.callback(
    Output('logout', 'children'),
    [Input('page-content', 'children')])
def user_logout(input1):
    if current_user.is_authenticated:
        return html.A('Logout', href='/logout')
    else:
        return ''

You should create multipage app and import layout of that app

http://dash.plotly.com/urls

i tried all that bro.

what exactly is the error? Can u post screenshot?

It says no module comments exist, comments is the python file with ur code

You first create an empty text file and run that program…