Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import plotly.express as px
- import plotly.graph_objects as go
- from plotly.subplots import make_subplots
- import time
- import datetime
- import dash
- from dash import dcc
- from dash import html
- from dash.dependencies import Input, Output, State
- import dash_bootstrap_components as dbc
- from PyQt5 import QtCore, QtWidgets, uic
- from PyQt5.QtWebEngineWidgets import QWebEngineView
- from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout,QSpacerItem, QSizePolicy,QSizeGrip, QLabel
- from PyQt5.QtCore import QUrl, Qt, QPoint, QObject, QEvent, QRect, pyqtSignal
- from PyQt5.QtGui import QCursor
- import sys
- import threading
- import math
- import os
- def string_to_number(df, column):
- if isinstance(df.iloc[0, df.columns.get_loc(column)], str):
- df[column] = df[column].str.replace(',','')
- df[column] = df[column].astype(float)
- return df
- def read_data(filename, stock_code, usecols):
- df = pd.read_csv(filename, header=None, usecols=usecols,
- names=['time', stock_code, 'price change', 'volume'],
- index_col= 'time', parse_dates=['time'])
- index_with_nan = df.index[df.isnull().any(axis=1)]
- df.drop(index_with_nan, axis=0, inplace=True)
- df = string_to_number(df, stock_code)
- df = string_to_number(df, 'volume')
- latest_info = df.iloc[-1, :]
- latest_price = str(round(latest_info.iloc[0], 2))
- latest_price_change = str(latest_info.iloc[1])
- dfvol = df['volume'].resample('1Min').mean()
- data = df[stock_code].resample('1Min').ohlc()
- data['time'] = data.index
- data['time'] = pd.to_datetime(data['time'], format='%Y-%m-%d %H:%M:%S')
- data['MA5'] = data['close'].rolling(5).mean()
- data['MA10'] = data['close'].rolling(10).mean()
- data['MA20'] = data['close'].rolling(20).mean()
- data['volume_diff'] = dfvol.diff()
- data[data['volume_diff'] < 0] = None
- index_with_nan = data.index[data.isnull().any(axis=1)]
- data.reset_index(drop=True, inplace=True)
- #print("data was added")
- return data, latest_price, latest_price_change, df['volume'][-1]
- def run_dash():
- app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
- colors = {
- 'background': '#121416',
- 'text': 'white'
- }
- stock = ['BOIL', 'PYPL', 'INTC', 'AAPL', 'AMZN', 'MSFT', 'TSLA']
- filename = 'stock_data.csv'
- data, latest_price, latest_price_change, volume = read_data(filename, stock[0], [1, 2, 3, 4])
- d2, latest_price1, latest_price_change1, volume1 = read_data(filename, stock[1], [1, 5, 6, 7])
- d3, latest_price2, latest_price_change2, volume2 = read_data(filename, stock[2], [1, 8, 9, 10])
- d4, latest_price3, latest_price_change3, volume3 = read_data(filename, stock[3], [1, 11, 12, 13])
- d5, latest_price4, latest_price_change4, volume4 = read_data(filename, stock[4], [1, 14, 15, 16])
- d6, latest_price5, latest_price_change5, volume5 = read_data(filename, stock[5], [1, 17, 18, 19])
- d7, latest_price6, latest_price_change6, volume6 = read_data(filename, stock[6], [1, 20, 21, 22])
- app.layout = html.Div(style={'backgroundColor': colors['background']}, children=[ html.H1( children='DashBoard v1.0', style={ 'textAlign': 'center', 'color': colors['text']
- }
- ),
- html.Div(children='DashBoard v1.0: An application for your daily trade.', style={
- 'textAlign': 'center',
- 'backgroundColor': colors['background'],
- 'color': colors['text']
- }),
- dbc.Row(style={
- 'textAlign': 'center',
- 'height': '100vh',
- 'backgroundColor': colors['background'],
- 'color': colors['text']}, children=[
- dbc.Col( style={'content-align': 'center','padding-left': '20px' , 'padding-right': '50px','padding-top': '40px'}, children=[
- dbc.Row([
- dbc.Col(id='stock_name',align='center', width={'size': 1},style={'font-weight': 'bold','fontSize': '16','background-color': '#FFBF00','color': 'black', 'margin-left': '45px'}),
- dbc.Col(id='latest_price',align='center', width={'size': 2}, style={'color': 'white'}),
- dbc.Col(id='latest_price_change',align='center', width={'size': 2})
- ] ),
- dbc.Row([dbc.Col(
- dcc.Graph(id="candles"), align='center',
- ),],),
- dbc.Row([
- dbc.Col('Volume',align='center', width={'size': 1},style={'font-weight': 'bold','fontSize': '16','width': '15%','background-color': '#FFBF00','color': 'black', 'margin-left': '45px', 'margin-right': '10px'}),
- dbc.Col(id='volume',align='center', width={'size': 1},style={'color': 'white'}),
- ] ),
- dbc.Row([dbc.Col(
- dcc.Graph(id="volume_chart",config={'displayModeBar': False}), align='center',
- ),],),
- dbc.Row([
- dbc.Col("Rsi",align='center', width={'size': 2},style={'font-weight': 'bold','fontSize': '16','width': '10%','background-color': '#FFBF00','color': 'black', 'margin-left': '45px', 'margin-right': '10px'}),
- dbc.Col(volume,align='center', width={'size': 2},style={'color': 'white'}),
- ] ),
- dbc.Row([dbc.Col(
- dcc.Graph(id="Rsi",config={'displayModeBar': False}), align='center',
- ),],),
- ], width={'size': 8},md=8,sm=12,xs=12),
- dbc.Col(style={'content-align': 'center','padding-left': '50px' , 'padding-right': '50px','padding-top': '40px'}, children=[
- dbc.Row([
- dbc.Col(id='stock_name1',align='center', width={'size': 2},style={'width': '20%','text-align': 'center','font-weight': 'bold','fontSize': '16','background-color': '#FFBF00','color': 'black', 'margin-left': '15px', 'margin-right': '15px'}),
- dbc.Col(id='latest_price1',align='center', width={'size': 2},style={'color': 'white'}),
- dbc.Col(id='latest_price_change1',align='center', width={'size': 2})
- ]),
- dbc.Row([dbc.Col(
- dcc.Graph(id="stocks1",config={'displayModeBar': False}) , align='center',
- ),],),
- dbc.Row([
- dbc.Col(id="stock_name2",align='center', width={'size': 2},style={'width': '20%','text-align': 'center','font-weight': 'bold','fontSize': '16','background-color': '#FFBF00','color': 'black', 'margin-left': '15px', 'margin-right': '15px'}),
- dbc.Col(id='latest_price2',align='center', width={'size': 2},style={'color': 'white'}),
- dbc.Col(id='latest_price_change2',align='center', width={'size': 2})
- ]),
- dbc.Row([dbc.Col(
- dcc.Graph(id="stocks2",config={'displayModeBar': False}) , align='center',
- ),],),
- dbc.Row([
- dbc.Col(id="stock_name3",align='center', width={'size': 2},style={'width': '20%','text-align': 'center','font-weight': 'bold','fontSize': '16','background-color': '#FFBF00','color': 'black', 'margin-left': '15px', 'margin-right': '15px'}),
- dbc.Col(id='latest_price3',align='center', width={'size': 2},style={'color': 'white'}),
- dbc.Col(id='latest_price_change3',align='center', width={'size': 2})
- ]),
- dbc.Row([dbc.Col(
- dcc.Graph(id="stocks3",config={'displayModeBar': False}) , align='center',
- ),],),
- dbc.Row([
- dbc.Col(id="stock_name4",align='center', width={'size': 2},style={'width': '20%','text-align': 'center','font-weight': 'bold','fontSize': '16','background-color': '#FFBF00','color': 'black', 'margin-left': '15px', 'margin-right': '15px'}),
- dbc.Col(id='latest_price4',align='center', width={'size': 2},style={'color': 'white'}),
- dbc.Col(id='latest_price_change4',align='center', width={'size': 2})
- ]),
- dbc.Row([dbc.Col(
- dcc.Graph(id="stocks4",config={'displayModeBar': False}) , align='center',
- ),],),
- dbc.Row([
- dbc.Col(id="stock_name5",align='center', width={'size': 2},style={'width': '20%','text-align': 'center','font-weight': 'bold','fontSize': '16','background-color': '#FFBF00','color': 'black', 'margin-left': '15px', 'margin-right': '15px'}),
- dbc.Col(id='latest_price5',align='center', width={'size': 2},style={'color': 'white'}),
- dbc.Col(id='latest_price_change5',align='center', width={'size': 2})
- ]),
- dbc.Row([dbc.Col(
- dcc.Graph(id="stocks5",config={'displayModeBar': False}) , align='center',
- ),],),
- dbc.Row([
- dbc.Col(id="stock_name6",align='left', width={'size': 2},style={'width': '20%','text-align': 'center','font-weight': 'bold','fontSize': '16','background-color': '#FFBF00','color': 'black', 'margin-left': '15px', 'margin-right': '15px'}),
- dbc.Col(id='latest_price6',align='center', width={'size': 2},style={'color': 'white'}),
- dbc.Col(id='latest_price_change6',align='center', width={'size': 2})
- ]),
- dbc.Row([dbc.Col(
- dcc.Graph(id="stocks6",config={'displayModeBar': False}) , align='center',
- ),],),
- ], width={'size': 4},md=4,sm=12,xs=12),
- ]),
- dcc.Store(id='line-visibility-store', data={'MA5': True, 'MA10': True, 'MA20': True}),
- dcc.Store(id='zoom_store'),
- dcc.Interval(id= "interval", interval = 1000)
- ])
- @app.callback(
- Output("candles", "figure"),
- Output("stocks1", "figure"),
- Output("stocks2", "figure"),
- Output("stocks3", "figure"),
- Output("stocks4", "figure"),
- Output("stocks5", "figure"),
- Output("stocks6", "figure"),
- Output("volume_chart", "figure"),
- Output("Rsi", "figure"),
- Output("stock_name", "children"),
- Output("latest_price", "children"),
- Output("latest_price_change", "children"),
- Output("volume", "children"),
- Output("latest_price_change", "style"),
- Output("stock_name1", "children"),
- Output("latest_price1", "children"),
- Output("latest_price_change1", "children"),
- Output("latest_price_change1", "style"),
- Output("stock_name2", "children"),
- Output("latest_price2", "children"),
- Output("latest_price_change2", "children"),
- Output("latest_price_change2", "style"),
- Output("stock_name3", "children"),
- Output("latest_price3", "children"),
- Output("latest_price_change3", "children"),
- Output("latest_price_change3", "style"),
- Output("stock_name4", "children"),
- Output("latest_price4", "children"),
- Output("latest_price_change4", "children"),
- Output("latest_price_change4", "style"),
- Output("stock_name5", "children"),
- Output("latest_price5", "children"),
- Output("latest_price_change5", "children"),
- Output("latest_price_change5", "style"),
- Output("stock_name6", "children"),
- Output("latest_price6", "children"),
- Output("latest_price_change6", "children"),
- Output("latest_price_change6", "style"),
- Input("interval", "n_intervals"),
- Input("zoom_store", "data"),
- Input("line_visibility_store", "data")
- )
- def update_figure(n_intervals,zoom_data, line_visibility):
- external_stylesheets = ['./assets/custom.css']
- #data
- stock = ['BOIL', 'PYPL', 'INTC', 'AAPL', 'AMZN', 'MSFT', 'TSLA']
- filename = 'stock_data.csv'
- data, latest_price, latest_price_change, volume = read_data(filename, stock[0], [1, 2, 3, 4])
- pos = data['open'] - data['close'] < 0
- neg = data['open'] - data['close'] > 0
- data['x_axis'] = list(range(1, len(data['volume_diff']) +1))
- ymax = data['volume_diff'].max()
- ystd = data['volume_diff'].std()
- candles = go.Figure()
- candles.add_trace(go.Candlestick(x=data.index,open=data['open'],high=data['high'],low=data['low'],close=data['close'],name=stock[0],showlegend=False))
- candles.add_trace(go.Scatter(x=data.index, y=data['MA5'],showlegend=True, name="5 minute SMA",marker={'color': 'pink'}, visible=line_visibility['MA5']))
- candles.add_trace(go.Scatter(x=data.index, y=data['MA10'],showlegend=True, name="10 minute SMA",marker={'color': 'orange'}, visible=line_visibility['MA10']))
- candles.add_trace(go.Scatter(x=data.index, y=data['MA20'], name="20 minute SMA",showlegend=True, marker={'color': '#08a0e9'}, visible=line_visibility['M20']))
- candles.update_layout(
- #width= 600,
- height=400,
- legend=dict(x=0.02,y=0.98,bgcolor='#091217',font=dict(color='white', size=10),bordercolor='white', borderwidth=0.5),
- xaxis=dict( zerolinecolor='#091217', showticklabels=False,gridcolor="grey"), #Candle
- yaxis=dict( gridcolor="grey", tickfont=dict(color='white')), #Candle
- margin=dict(l=0, r=0, t=10, b=20),
- xaxis_rangeslider_visible=False,
- plot_bgcolor='#091217',
- paper_bgcolor=colors['background'],
- font_color=colors['text'],
- yaxis_range=[data['low'].min()- data['high'].std()*0.5,data['high'].max()+data['high'].std()*3 if not math.isnan(data['high'].max()) and data['high'].max() !=0 else [] ]
- )
- if zoom_data is not None:
- candles.update_xaxes(range=zoom_data),
- # Add volume chart to second subplot
- volume_chart = go.Figure()
- volume_chart.add_trace(go.Bar(x=data['x_axis'][pos],y=data['volume_diff'][pos],name='Volume',showlegend=False,marker={'color': 'Green'}))
- volume_chart.add_trace(go.Bar(x=data['x_axis'][neg],y=data['volume_diff'][neg],name='Volume',showlegend=False,marker={'color': 'Red'}))
- volume_chart.update_layout(
- #width= 300,
- height=80,
- yaxis_range=[0, ymax+ystd*3 if not math.isnan(ymax)else []],
- xaxis=dict(zerolinecolor='#091217',showticklabels=False, showgrid=True, gridcolor="grey"), #Volume
- yaxis=dict(zerolinecolor='#091217',tickfont=dict(color='#121416'), showgrid=False),
- margin=dict(l=0, r=0, t=10, b=20),
- xaxis_rangeslider_visible=False,
- plot_bgcolor='#091217',
- paper_bgcolor=colors['background'],
- font_color=colors['text']
- )
- # Add volume chart to second subplot
- Rsi = go.Figure()
- Rsi.add_trace(go.Bar(x=data['x_axis'][pos],y=data['volume_diff'][pos],name='Volume2',showlegend=False,marker={'color': 'Green'}))
- Rsi.update_layout(
- #width= 300,
- height=80,
- yaxis_range=[0, ymax+ystd*3 if not math.isnan(ymax)else []],
- xaxis=dict(zerolinecolor='#091217',showticklabels=False, showgrid=True, gridcolor="grey"), #Volume
- yaxis=dict(zerolinecolor='#091217',tickfont=dict(color='#121416'), showgrid=False),
- margin=dict(l=0, r=0, t=10, b=20),
- xaxis_rangeslider_visible=False,
- plot_bgcolor='#091217',
- paper_bgcolor=colors['background'],
- font_color=colors['text']
- )
- stocks1 = go.Figure()
- d2, latest_price1, latest_price_change1, volume = read_data(filename, stock[1], [1, 5, 6, 7])
- stocks1.add_trace(go.Scatter(x=d2.index, y=d2['close'],showlegend=False,marker={'color': 'White'}, name=stock[1]))
- stocks1.update_layout(
- #width= 300,
- height=82,
- yaxis_range=[d2['close'].min()-d2['close'].std()*0.5, d2['close'].max()+d2['close'].std()*3 if not math.isnan(d2['close'].max()) and d2['close'].max() != 0 else []],
- xaxis=dict(zerolinecolor='#091217',showticklabels=False, showgrid=False),
- yaxis=dict(showticklabels=False, showgrid=False),
- margin=dict(l=0, r=0, t=10, b=20),
- xaxis_rangeslider_visible=False,
- plot_bgcolor='#091217',
- paper_bgcolor=colors['background'],
- font_color=colors['text']
- )
- d3, latest_price2, latest_price_change2, volume2 = read_data(filename, stock[2], [1, 8, 9, 10])
- stocks2 = go.Figure()
- stocks2.add_trace(go.Scatter(x=d3.index, y=d3['close'],showlegend=False,marker={'color': 'White'}, name=stock[2]))
- stocks2.update_layout(
- #width= 300,
- height=82,
- yaxis_range=[d3['close'].min()-d3['close'].std()*0.5, d3['close'].max()+d3['close'].std()*3 if not math.isnan(d3['close'].max()) and d3['close'].max() != 0 else []],
- xaxis=dict(zerolinecolor='#091217',showticklabels=False, showgrid=False),
- yaxis=dict(showticklabels=False, showgrid=False),
- margin=dict(l=0, r=0, t=10, b=20),
- xaxis_rangeslider_visible=False,
- plot_bgcolor='#091217',
- paper_bgcolor=colors['background'],
- font_color=colors['text']
- )
- d4, latest_price3, latest_price_change3, volume3 = read_data(filename, stock[3], [1, 11, 12, 13])
- stocks3 = go.Figure()
- stocks3.add_trace(go.Scatter(x=d4.index, y=d4['close'],showlegend=False,marker={'color': 'White'}, name=stock[3]))
- stocks3.update_layout(
- #width= 300,
- height=82,
- yaxis_range=[d4['close'].min()-d4['close'].std()*0.5, d4['close'].max()+d4['close'].std()*3 if not math.isnan(d4['close'].max()) and d4['close'].max() != 0 else []],
- xaxis=dict(zerolinecolor='#091217',showticklabels=False, showgrid=False),
- yaxis=dict(showticklabels=False, showgrid=False),
- margin=dict(l=0, r=0, t=10, b=20),
- xaxis_rangeslider_visible=False,
- plot_bgcolor='#091217',
- paper_bgcolor=colors['background'],
- font_color=colors['text']
- )
- stocks4 = go.Figure()
- d5, latest_price4, latest_price_change4, volume4 = read_data(filename, stock[4], [1, 14, 15, 16])
- stocks4.add_trace(go.Scatter(x=d5.index, y=d5['close'],showlegend=False,marker={'color': 'White'}, name=stock[4]))
- stocks4.update_layout(
- #width= 300,
- height=82,
- yaxis_range=[d5['close'].min()-d5['close'].std()*0.5, d5['close'].max()+d5['close'].std()*3 if not math.isnan(d5['close'].max()) and d5['close'].max() != 0 else []],
- xaxis=dict(zerolinecolor='#091217',showticklabels=False, showgrid=False),
- yaxis=dict(showticklabels=False, showgrid=False),
- margin=dict(l=0, r=0, t=10, b=20),
- xaxis_rangeslider_visible=False,
- plot_bgcolor='#091217',
- paper_bgcolor=colors['background'],
- font_color=colors['text']
- )
- d6, latest_price5, latest_price_change5, volume5 = read_data(filename, stock[5], [1, 17, 18, 19])
- stocks5 = go.Figure()
- stocks5.add_trace(go.Scatter(x=d6.index, y=d6['close'],showlegend=False,marker={'color': 'White'}, name=stock[5]))
- stocks5.update_layout(
- #width= 300,
- height=82,
- yaxis_range=[d6['close'].min()-d6['close'].std()*0.5, d6['close'].max()+d6['close'].std()*3 if not math.isnan(d6['close'].max()) and d6['close'].max() != 0 else []],
- xaxis=dict(zerolinecolor='#091217',showticklabels=False, showgrid=False),
- yaxis=dict(showticklabels=False, showgrid=False),
- margin=dict(l=0, r=0, t=10, b=20),
- xaxis_rangeslider_visible=False,
- plot_bgcolor='#091217',
- paper_bgcolor=colors['background'],
- font_color=colors['text']
- )
- d7, latest_price6, latest_price_change6, volume6 = read_data(filename, stock[6], [1, 20, 21, 22])
- stocks6 = go.Figure()
- stocks6.add_trace(go.Scatter(x=d7.index, y=d7['close'],showlegend=False,marker={'color': 'White'}, name=stock[2]))
- stocks6.update_layout(
- #width= 300,
- height=82,
- yaxis_range=[d7['close'].min()-d7['close'].std()*0.5, d7['close'].max()+d7['close'].std()*3 if not math.isnan(d7['close'].max()) and d7['close'].max() != 0 else []],
- xaxis=dict(zerolinecolor='#091217',showticklabels=False, showgrid=False),
- yaxis=dict(showticklabels=False, showgrid=False),
- margin=dict(l=0, r=0, t=10, b=20),
- xaxis_rangeslider_visible=False,
- plot_bgcolor='#091217',
- paper_bgcolor=colors['background'],
- font_color=colors['text']
- )
- #{'color': '#18b800' if latest_price_change[0] == '+' else '#ff3503'}
- return candles,stocks1, stocks2, stocks3, stocks4, stocks5, stocks6, volume_chart, Rsi, \
- stock[0], latest_price, latest_price_change, volume ,{'color': '#18b800' if latest_price_change[0] == '+' else '#ff3503'}, stock[1], latest_price1, latest_price_change1,{'color': '#18b800' if latest_price_change1[0] == '+' else '#ff3503'}, \
- stock[2], latest_price2, latest_price_change2,{'color': '#18b800' if latest_price_change2[0] == '+' else '#ff3503'},stock[3], latest_price3, latest_price_change3,{'color': '#18b800' if latest_price_change3[0] == '+' else '#ff3503'}, \
- stock[4], latest_price4, latest_price_change4,{'color': '#18b800' if latest_price_change4[0] == '+' else '#ff3503'}, stock[5], latest_price5, latest_price_change5,{'color': '#18b800' if latest_price_change5[0] == '+' else '#ff3503'}, \
- stock[6], latest_price6, latest_price_change6,{'color': '#18b800' if latest_price_change6[0] == '+' else '#ff3503'}
- @app.callback(
- Output('zoom_store', 'data'),
- Input('candles', 'relayoutData'))
- def update_zoom(relayout_data):
- if relayout_data is not None and 'xaxis.range[0]' in relayout_data and 'xaxis.range[1]' in relayout_data:
- return [relayout_data['xaxis.range[0]'], relayout_data['xaxis.range[1]']]
- return None
- @app.callback(
- Output('line_visibility_store', 'data'),
- Input('candles', 'restyleData'),
- State("line-visibility-store", 'data'))
- def update_line_visibility(restyle_data, current_visibility):
- if restyle_data is not None and 'visible' in restyle_data:
- new_visibility = current_visibility.copy()
- for i, visible in enumerate(restyle_data['visible']):
- if i == 0:
- new_visibility['MA5'] = visible
- elif i == 1:
- new_visibility['MA10'] = visible
- elif i == 2:
- new_visibility['MA20'] = visible
- return new_visibility
- return current_visibility
- app.run_server()
- class XAxisSizeGrip3(QSizeGrip):
- def __init__(self, parent=None):
- super().__init__(parent)
- def mousePressEvent(self, event):
- super().mousePressEvent(event)
- self.window().setMaximumWidth(self.width())
- self.window().setMinimumWidth(self.width())
- self.window().setMaximumHeight(16777215)
- self.window().setMinimumHeight(0)
- def enterEvent(self, event):
- super().enterEvent(event)
- self.setCursor(QCursor(Qt.SizeVerCursor))
- def leaveEvent(self, event):
- super().leaveEvent(event)
- self.unsetCursor()
- def mouseReleaseEvent(self, event):
- super().mouseReleaseEvent(event)
- self.window().setMaximumWidth(16777215) # remove the height limit after the drag is complete
- self.window().setMinimumWidth(0) # remove the height limit after the drag is complete
- class XAxisSizeGrip(QSizeGrip):
- def __init__(self, parent=None):
- super().__init__(parent)
- def mousePressEvent(self, event):
- super().mousePressEvent(event)
- self.window().setMaximumHeight(16777215)
- self.window().setMinimumHeight(0)
- self.window().setMaximumWidth(16777215)
- self.window().setMinimumWidth(0)
- class XAxisSizeGrip2(QSizeGrip):
- def __init__(self, parent=None):
- super().__init__(parent)
- def enterEvent(self, event):
- super().enterEvent(event)
- self.setCursor(QCursor(Qt.SizeHorCursor))
- def leaveEvent(self, event):
- super().leaveEvent(event)
- self.unsetCursor()
- def mousePressEvent(self, event):
- super().mousePressEvent(event)
- #self.setCursor(QCursor(Qt.SizeHorCursor))
- self.window().setMaximumWidth(16777215)
- self.window().setMinimumWidth(0)
- self.window().setMaximumHeight(self.height())
- self.window().setMinimumHeight(self.height())
- def mouseReleaseEvent(self, event):
- super().mouseReleaseEvent(event)
- self.window().setMaximumHeight(16777215) # remove the height limit after the drag is complete
- self.window().setMinimumHeight(0) # remove the height limit after the drag is complete
- class App(QMainWindow):
- window_resized = pyqtSignal()
- def __init__(self):
- super().__init__()
- uic.loadUi('gui2.ui', self)
- self.setAttribute(Qt.WA_TranslucentBackground, True)
- self.setWindowFlag(Qt.FramelessWindowHint)
- self.setGeometry(0, 0, 1250, 900)
- self.groupBox_3_layout = QVBoxLayout(self.groupBox_3)
- web1 = QWebEngineView()
- web1.load(QUrl("http://127.0.0.1:8050"))
- self.groupBox_3_layout.addWidget(web1)
- self.pushButton_6.clicked.connect(self.on_pushButton_6_clicked)
- #button to min
- self.pushButton_9.clicked.connect(self.minimize_window)
- #button to max
- self.pushButton_8.clicked.connect(self.maximize_window)
- #button
- self.pushButton_7.clicked.connect(self.close_window)
- self.is_maximized = False
- self.original_geometry = self.geometry()
- # Set up the drag event for groupBox_23
- self.dragPos = None
- self.groupBox23.mouseMoveEvent = self.mouseMoveEvent1
- self.groupBox23.mousePressEvent = self.mousePressEvent1
- # Top left corner
- size_grip1 = XAxisSizeGrip(self)
- size_grip1.setToolTip("Resize")
- size_grip1.setGeometry(0, 0, 10, 10)
- size_grip1.setStyleSheet("background-color: transparent;")
- # top right corner
- size_grip2 = XAxisSizeGrip(self)
- size_grip2.setToolTip("Resize")
- size_grip2.setGeometry(0, self.height() - 10, 10, 10)
- size_grip2.setStyleSheet("background-color: transparent;")
- # Botom left corner
- size_grip3 = XAxisSizeGrip(self)
- size_grip3.setToolTip("Resize")
- size_grip3.setGeometry(self.width() - 10, 0, 10, 10)
- size_grip3.setStyleSheet("background-color: transparent;")
- # Bottom Right corner
- size_grip4 = XAxisSizeGrip(self)
- size_grip4.setToolTip("Resize")
- size_grip4.setGeometry(-10, self.height() - 10, self.width(), 10)
- size_grip4.setStyleSheet("background-color: transparent;")
- # Right side
- size_grip5 = XAxisSizeGrip2(self)
- size_grip5.setToolTip("Resize")
- size_grip5.setGeometry(0, - 10, 10, self.height()-20)
- size_grip5.setStyleSheet("background-color: transparent;")
- # Left side
- size_grip6 = XAxisSizeGrip2(self)
- size_grip6.setToolTip("Resize")
- size_grip6.setGeometry(0, - 10, 10, self.height()-20)
- size_grip6.setStyleSheet("background-color: transparent;")
- # Bottom side
- size_grip7 = XAxisSizeGrip3(self)
- size_grip7.setToolTip("Resize")
- size_grip7.setGeometry(self.width()-self.width()+10, self.height() - 10, self.width()-20, 10)
- size_grip7.setStyleSheet("background-color: transparent;")
- self.size_grip1 = size_grip1
- self.size_grip2 = size_grip2
- self.size_grip3 = size_grip3
- self.size_grip4 = size_grip4
- self.size_grip5 = size_grip5
- self.size_grip6 = size_grip6
- self.size_grip7 = size_grip7
- self.previous_geometry = None
- self.window_resized.connect(self.update_size_grips)
- def on_pushButton_6_clicked(self):
- if self.groupBox_4.maximumWidth() == 0:
- self.groupBox_4.setMaximumWidth(130)
- else:
- self.groupBox_4.setMaximumWidth(0)
- def minimize_window(self):
- self.showMinimized()
- def maximize_window(self):
- if self.isMaximized():
- self.showNormal()
- self.is_maximized = False
- self.setGeometry(self.original_geometry)
- else:
- self.showMaximized()
- self.is_maximized = True
- def close_window(self):
- self.close()
- # Function to handle the mouse press event
- def mousePressEvent1(self, event):
- if event.button() == Qt.LeftButton:
- self.dragPos = event.globalPos()
- def mouseMoveEvent1(self, event):
- if self.dragPos is not None:
- delta = event.globalPos() - self.dragPos
- new_pos = self.pos() + delta
- self.move(new_pos)
- self.dragPos = event.globalPos()
- screen_geometry = QApplication.desktop().availableGeometry()
- screen_margin = 30 # Adjust this value to change the docking sensitivity
- # Left edge docking
- if event.globalX() <= screen_margin and not self.isMaximized():
- if self.previous_geometry is None:
- self.previous_geometry = self.geometry()
- self.setGeometry(QRect(0, 0, screen_geometry.width() // 2, screen_geometry.height()))
- # Right edge docking
- elif event.globalX() >= screen_geometry.width() - screen_margin and not self.isMaximized():
- if self.previous_geometry is None:
- self.previous_geometry = self.geometry()
- self.setGeometry(QRect(screen_geometry.width() // 2, 0, screen_geometry.width() // 2, screen_geometry.height()))
- # Undock and restore previous geometry
- elif self.previous_geometry is not None:
- new_x = new_pos.x()
- new_y = new_pos.y()
- undocked_geometry = QRect(new_x, new_y, self.previous_geometry.width(), self.previous_geometry.height())
- self.setGeometry(undocked_geometry)
- self.previous_geometry = None
- def mouseReleaseEvent(self, event):
- self.dragPos = None
- def resizeEvent(self, event):
- super().resizeEvent(event)
- self.window_resized.emit()
- def update_size_grips(self):
- self.size_grip2.setGeometry(self.width() - 10, 0, 10, 10)
- self.size_grip3.setGeometry(0, self.height() - 10, 10, 10)
- self.size_grip4.setGeometry(self.width()-10, self.height() - 10, 10, 10)
- self.size_grip5.setGeometry(self.width()-10 , 10, 10, self.height()-20)
- self.size_grip6.setGeometry(0 , 10, 10, self.height()-20)
- self.size_grip7.setGeometry(self.width()-self.width()+10, self.height() - 10, self.width()-20, 10)
- if __name__ == '__main__':
- threading.Thread(target=run_dash, args=(), daemon=True).start()
- app = QApplication(sys.argv)
- win = App()
- win.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement